From dcce53dc8b4759265469983b42e278fa4bd45364 Mon Sep 17 00:00:00 2001 From: Will Hoskings Date: Sun, 9 Feb 2020 15:47:02 +0000 Subject: [PATCH 1/5] Add Discord data types --- types/discord.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/types/discord.ts b/types/discord.ts index 2d5f01e7c..740aa2b7c 100644 --- a/types/discord.ts +++ b/types/discord.ts @@ -24,3 +24,31 @@ export interface DiscordBotGateway { reset_after: number } } + +export enum OpCode { + Dispatch = 0, + Heartbeat, + Identify, + StatusUpdate, + VoiceStateUpdate, + Resume, + Reconnect, + RequestGuildMembers, + InvalidSession, + Hello, + HeartbeatACK +} + +export enum GatewayCloseEvent { + UnknownError = 4000, + UnknownOpcode, + DecodeError, + NotAuthenticated, + AuthenticationFailed, + AlreadyAuthenticated, + InvalidSeq = 4007, + RateLimited, + SessionTimeout, + InvalidShard, + ShardingRequired +} \ No newline at end of file From 6a3ca3c14a8a0d43e16232b23df8400fe7a0a895 Mon Sep 17 00:00:00 2001 From: Will Hoskings Date: Sun, 9 Feb 2020 15:47:17 +0000 Subject: [PATCH 2/5] Hack around VSCode's import suggestions --- tsconfig.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index 1853cc842..21514e078 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -40,8 +40,8 @@ /* Module Resolution Options */ // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ - // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ - // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ + // Hack to stop VSCode from fsuggestions imports without ./ + "baseUrl": "../../", /* Base directory to resolve non-absolute module names. */ // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ // "typeRoots": [], /* List of folders to include type definitions from. */ // "types": [], /* Type declaration files to be included in compilation. */ From 1720e13f6714ebe5811a3a7d23d4f28b8bb59f1f Mon Sep 17 00:00:00 2001 From: Will Hoskings Date: Sun, 9 Feb 2020 16:00:14 +0000 Subject: [PATCH 3/5] Type more Discord events --- types/discord.ts | 102 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 2 deletions(-) diff --git a/types/discord.ts b/types/discord.ts index 740aa2b7c..714ef575a 100644 --- a/types/discord.ts +++ b/types/discord.ts @@ -25,7 +25,7 @@ export interface DiscordBotGateway { } } -export enum OpCode { +export enum GatewayOpcode { Dispatch = 0, Heartbeat, Identify, @@ -39,7 +39,7 @@ export enum OpCode { HeartbeatACK } -export enum GatewayCloseEvent { +export enum GatewayCloseEventCode { UnknownError = 4000, UnknownOpcode, DecodeError, @@ -51,4 +51,102 @@ export enum GatewayCloseEvent { SessionTimeout, InvalidShard, ShardingRequired +} + +export enum VoiceOpcode { + Identify, + SelectProtocol, + Ready, + Heartbeat, + SessionDescription, + Speaking, + HeartbeatACK, + Resume, + Hello, + Resumed, + ClientDisconnect = 13 +} + +export enum VoiceCloseEventCode { + UnknownOpcode = 4001, + NotAuthenticated = 4003, + AuthenticationFailed, + AlreadyAuthenticated, + SessionNoLongerValid, + SessionTimeout = 4009, + ServerNotFound = 4011, + UnknownProtocol, + Disconnected = 4014, + VoiceServerCrashed, + UnknownEncryptionMode +} + +export enum HttpResponseCode { + Ok = 200, + Created, + NoContent = 204, + NotModified = 304, + BadRequest = 400, + Unauthorized = 401, + Forbidden = 403, + NotFound, + MethodNotAllowed, + TooManyRequests = 429, + GatewayUnavailable = 502, + // ServerError left untyped because it's 5xx. +} + +export enum JSONErrorCode { + UnknownAccount = 10001, + UnknownApplication, + UnknownChannel, + UnknownGuild, + UnknownIntegration, + UnknownInvite, + UnknownMember, + UnknownMessge, + UnknownOverwrite, + UnknownProvider, + UnknownRole, + UnknownToken = 10012, + UnknownUser, + UnknownEmoji, + UnknownWebhook, + BotsCannotUse = 20001, + OnlyBotsCanUse, + MaxGuildsReached = 30001, + MaxFriendsReached, + MaxPinsReached, + MaxGuildRolesReached = 30005, + MaxReactionsReached = 30010, + MaxGuildChannelsReached = 30013, + MaxInvitesReached = 30016, + Unathorized = 40001, + UserIsBannedFromGuild = 40007, + MissingAccess = 50001, + InvalidAccountType = 50002, + CannotExecuteOnDMChannel, + WidgetDisabled, + CannotEditMessageByAnotherUser, + CannotSendEmptyMessage, + CannotSendMessageToUser, + CannotSendMessageInVoiceChannel, + ChannelVerificationTooHigh, + OAuth2ApplicationNoBot, + OAuth2ApplicationLimitReached, + InvalidOAuthState, + MissingPermissions, + InvalidAuthenticationToken, + NoteIsTooLong, + TooFewOrTooManyMessagesToDelete, + MessageCanOnlyBePinnedInParentChannel = 50019, + InviteCodeTakenOrInvalid, + CannotExecuteOnSystemMessage, + InvalidOAuth2AccessToken, + MessageProvidedTooOldToBulkDelet = 50034, + InvalidFormBody, + InviteAcceptedToGuildApplicationBotNotIn, + InvalidAPIVersion = 50041, + ReactionBlocked = 90001, + ResourceOverloaded = 130000 } \ No newline at end of file From b7d9c85318ba05e177b7b7db3f9c2520a1dca633 Mon Sep 17 00:00:00 2001 From: Will Hoskings Date: Sun, 9 Feb 2020 16:01:51 +0000 Subject: [PATCH 4/5] Not sure what happened there. --- tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 21514e078..19db3c704 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -40,7 +40,7 @@ /* Module Resolution Options */ // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ - // Hack to stop VSCode from fsuggestions imports without ./ + // Hack to stop VSCode from suggesting imports without ./ or ../ as a prefix. "baseUrl": "../../", /* Base directory to resolve non-absolute module names. */ // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ // "typeRoots": [], /* List of folders to include type definitions from. */ From e9d120da22ebeb7d7e26f5631b315c3d5c092763 Mon Sep 17 00:00:00 2001 From: Will Hoskings Date: Sun, 9 Feb 2020 16:08:29 +0000 Subject: [PATCH 5/5] RequestHandler -> RequestManager --- module/Client.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/module/Client.ts b/module/Client.ts index 8208bfc68..e96af8f3f 100644 --- a/module/Client.ts +++ b/module/Client.ts @@ -1,21 +1,22 @@ import { endpoints } from "../constants/discord.ts" -import RequestHandler from "../services/RequestHandler.ts" +import RequestManager from "../managers/RequestManager.ts"; class Client { /** The bot's token. This should never be used by end users. It is meant to be used internally to make requests to the Discord API. */ token: string + /** The Rate limit manager to handle all outgoing requests to discord. Not meant to be used by users. */ - RequestHandler: RequestHandler + protected requestManager: RequestManager; constructor(token: string) { this.token = `Bot ${token}` - this.RequestHandler = new RequestHandler(this, this.token) + this.requestManager = new RequestManager(this, this.token); } async connect() { // const data = await fetch(endpoints.GATEWAY_BOT).then(res => res.json()) // console.log(data) - console.log(await this.RequestHandler.get(endpoints.GATEWAY_BOT)) + console.log(await this.requestManager.get(endpoints.GATEWAY_BOT)) } }