Skip to content

Commit

Permalink
Merge pull request #4 from OfficialSirH/main
Browse files Browse the repository at this point in the history
rewrite del.js
  • Loading branch information
carolinaisslaying authored Nov 11, 2022
2 parents d322914 + a1f679a commit 923e5fc
Show file tree
Hide file tree
Showing 8 changed files with 2,700 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
node-version: [14.x, 16.x, 18.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
bot
config.json
dist
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This is a JavaScript API wrapper for Discord Extreme List's API.
#### Importing and "initialising" the package
```js
const DEL = require("del.js"); // Importing DEL.js
const del = new DEL("API auth token", "Discord client ID"); // Initialising it
const del = new DEL.Del("API auth token", "Discord client ID"); // Initialising it
```

###### Arguments
Expand All @@ -24,9 +24,8 @@ id | Snowflake | false | Your bot's client ID.
```js
const Discord = require("discord.js");
const client = new Discord.Client();
const prefix = "del.";
const DEL = require("del.js");
const del = new DEL("API auth token", client.user.id);
const del = new DEL.Del("API auth token", client.user.id);

client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}.`);
Expand Down
21 changes: 0 additions & 21 deletions del.js

This file was deleted.

262 changes: 262 additions & 0 deletions del.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
import type { APIChannel, APIRole } from "discord-api-types/v10";
import { request } from "undici";
import type { HttpMethod } from "undici/types/dispatcher";
import { version } from "./package.json";

/**
* The client for interacting with DEL's API
* @param {string} token - Your DEL API token
* @param {string} id - Your Discord Application's ID
*/
export class Del {
public static baseUrl = "https://api.discordextremelist.xyz/v2";

constructor();
constructor(public token?: string, public id?: string) {}

private async _request(method: HttpMethod, route: string, body?: object) {
let headers: {
"User-Agent": string;
"Content-Type"?: string;
Authorization?: string;
} = {
"User-Agent": `DEL.js/${version}`,
};
if (body) {
if (!this.token) throw new Error("Del not constructed with a token");
headers["Content-Type"] = "application/json";
headers["Authorization"] = this.token;
}

const response = await request(Del.baseUrl + route, {
method,
body,
headers,
});

return response.body.json();
}

/**
* Get Website Statistics
*/
async getWebsiteStats(): Promise<WebsiteStatistics> {
return this._request("GET", "/stats");
}

/**
* Get Website Health
*/
async getWebsiteHealth(): Promise<WebsiteHealth> {
return this._request("GET", "/health");
}

/**
* Get Bot Information
* @param {string} id - The bot's ID
*/
async getBotInfo(id: string): Promise<BotInformation> {
return this._request("GET", `/bot/${id}`);
}

/**
* Update your bot's stats on DEL
* @param {number} guildCount - The amount of guilds your bot is in
* @param {number} shardCount - The amount of shards your bot is using
*/
async postStats(
guildCount: number,
shardCount?: number
): Promise<BotStatistics> {
if (!this.id) throw new Error("Del not constructed with an id");

const body = shardCount
? { guildCount, shardCount }
: { guildCount, shardCount: 0 };

return this._request("POST", `/bot/${this.id}/stats`, body);
}

/**
* Get Server Information
* @param {string} id - The server's ID
*/
async getServerInfo(id: string): Promise<ServerInformation> {
return this._request("GET", `/server/${id}`);
}

/**
* Get Template Information
* @param {string} id - The template's ID
*/
async getTemplateInfo(id: string): Promise<TemplateInformation> {
return this._request("GET", `/template/${id}`);
}

/**
* Get User Information
* @param {string} id - The user's ID
*/
async getUserInfo(id: string): Promise<UserInformation> {
return this._request("GET", `/user/${id}`);
}
}

export interface BaseResponse {
error: boolean;
status: number;
}

export interface WebsiteStatistics extends BaseResponse {
servers: {
total: number;
};
bots: {
total: number;
approved: number;
premium: number;
};
users: {
total: number;
premium: number;
staff: {
total: number;
mods: number;
assistants: number;
admins: number;
};
};
templates: number;
}

export interface WebsiteHealth extends BaseResponse {
redis_ok: boolean;
mongo_ok: boolean;
}

export interface BotInformation extends BaseResponse {
bot: {
id: string;
name: string;
prefix: string;
tags: string[];
vanityUrl: string;
serverCount: number;
shardCount: number;
shortDesc: string;
longDesc: string;
editors: string[];
owner: {
id: string;
};
avatar: {
hash: string;
url: string;
};
links: {
invite: string;
support: string;
website: string;
donation: string;
repo: string;
};
status: {
approved: boolean;
siteBot: boolean;
archived: boolean;
};
};
}

export interface BotStatistics extends BaseResponse {
guildCount: number;
shardCount?: number;
}

export interface ServerInformation extends BaseResponse {
server: {
id: string;
name: string;
shortDesc: string;
longDesc: string;
tags: string[];
owner: {
id: string;
};
icon: {
hash: string;
url: string;
};
links: {
website: string;
donation: string;
};
};
}

export interface TemplateInformation extends BaseResponse {
template: {
id: string;
name: string;
region: string;
locale: string;
afkTimeout: number;
verificationLevel: number;
defaultMessageNotifications: number;
explicitContent: number;
roles: APIRole[];
channels: APIChannel[];
usageCount: number;
shortDesc: string;
longDesc: string;
tags: string[];
fromGuild: string;
owner: {
id: string;
};
icon: {
hash: string;
url: string;
};
links: {
template: string;
};
};
}

export interface UserInformation extends BaseResponse {
user: {
id: string;
name: string;
discrim: string;
fullUsername: string;
avatar: {
hash: string;
url: string;
};
profile: {
bio: string;
links: {
website: string;
github: string;
gitlab: string;
twitter: string;
instagram: string;
snapchat: string;
};
};
game: {
snakes: {
maxScore: number;
};
};
rank: {
admin: boolean;
mod: boolean;
assistant: boolean;
tester: boolean;
translator: boolean;
covid: boolean;
};
};
}
Loading

0 comments on commit 923e5fc

Please sign in to comment.