From b324263a7b183e9752d04ad656183365a854d025 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Sat, 17 Apr 2021 13:37:26 +0530 Subject: [PATCH 1/3] Roles#fetch -> Roles#fetchAll & jsdoc --- src/client/client.ts | 15 ++++++++++----- src/commands/client.ts | 9 ++++++++- src/commands/command.ts | 4 ++++ src/managers/roles.ts | 17 ++++++++++------- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/client/client.ts b/src/client/client.ts index acb1ce7..317265d 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -71,7 +71,7 @@ export interface ClientOptions { } /** - * Discord Client. + * Harmony Client. Provides high-level interface over the REST and WebSocket API. */ export class Client extends HarmonyEventEmitter { /** REST Manager - used to make all requests */ @@ -148,9 +148,9 @@ export class Client extends HarmonyEventEmitter { } } + /** Get Shard 0's Gateway */ get gateway(): Gateway { - // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion - return this.shards.list.get('0') as Gateway + return this.shards.list.get('0')! } applicationID?: string @@ -289,7 +289,10 @@ export class Client extends HarmonyEventEmitter { * @param token Your token. This is required if not given in ClientOptions. * @param intents Gateway intents in array. This is required if not given in ClientOptions. */ - async connect(token?: string, intents?: GatewayIntents[]): Promise { + async connect( + token?: string, + intents?: Array + ): Promise { token ??= this.token if (token === undefined) throw new Error('No Token Provided') this.token = token @@ -301,7 +304,9 @@ export class Client extends HarmonyEventEmitter { } else if (intents === undefined && this.intents !== undefined) { intents = this.intents } else if (intents !== undefined && this.intents === undefined) { - this.intents = intents + this.intents = intents.map((e) => + typeof e === 'string' ? GatewayIntents[e] : e + ) } else throw new Error('No Gateway Intents were provided') this.rest.token = token diff --git a/src/commands/client.ts b/src/commands/client.ts index 824746e..b2dd693 100644 --- a/src/commands/client.ts +++ b/src/commands/client.ts @@ -43,6 +43,11 @@ export interface CommandClientOptions extends ClientOptions { caseSensitive?: boolean } +/** + * Harmony Client with extended functionality for Message based Commands parsing and handling. + * + * See SlashClient (`Client#slash`) for more info about Slash Commands. + */ export class CommandClient extends Client implements CommandClientOptions { prefix: string | string[] mentionPrefix: boolean @@ -372,7 +377,9 @@ export class CommandClient extends Client implements CommandClientOptions { } } -/** Command decorator */ +/** + * Command decorator. Decorates the function with optional metadata as a Command registered upon constructing class. + */ export function command(options?: CommandOptions) { return function (target: CommandClient | Extension, name: string) { if (target._decoratedCommands === undefined) target._decoratedCommands = {} diff --git a/src/commands/command.ts b/src/commands/command.ts index b2d5fb1..987eaee 100644 --- a/src/commands/command.ts +++ b/src/commands/command.ts @@ -300,6 +300,8 @@ export class CommandsLoader { /** * Load a Command from file. * + * NOTE: Relative paths resolve from cwd + * * @param filePath Path of Command file. * @param exportName Export name. Default is the "default" export. */ @@ -344,6 +346,8 @@ export class CommandsLoader { /** * Load commands from a Directory. * + * NOTE: Relative paths resolve from cwd + * * @param path Path of the directory. * @param options Options to configure loading. */ diff --git a/src/managers/roles.ts b/src/managers/roles.ts index acf2484..ac38eab 100644 --- a/src/managers/roles.ts +++ b/src/managers/roles.ts @@ -22,14 +22,17 @@ export class RolesManager extends BaseManager { this.guild = guild } - /** Fetch a Guild Role (from API) */ - async fetch(id: string): Promise { + /** Fetch All Guild Roles */ + async fetchAll(): Promise { return await new Promise((resolve, reject) => { - this.client.rest - .get(GUILD_ROLE(this.guild.id, id)) - .then(async (data) => { - await this.set(id, data as RolePayload) - resolve(((await this.get(id)) as unknown) as Role) + this.client.rest.api.guilds[this.guild.id].roles.get + .then(async (data: RolePayload[]) => { + const roles: Role[] = [] + for (const raw of data) { + await this.set(raw.id, raw) + roles.push(new Role(this.client, raw, this.guild)) + } + resolve(roles) }) .catch((e) => reject(e)) }) From ea221f8962e98b66e1f974266afc8aa79ba87703 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Sat, 17 Apr 2021 13:47:46 +0530 Subject: [PATCH 2/3] client.ping -> client.gateway.ping --- README.md | 4 ++-- src/client/client.ts | 2 -- src/gateway/mod.ts | 9 ++++----- test/index.ts | 2 +- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 0f20c2e..c987f5c 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ client.on('ready', () => { // Listen for event whenever a Message is sent client.on('messageCreate', (msg: Message): void => { if (msg.content === '!ping') { - msg.channel.send(`Pong! WS Ping: ${client.ping}`) + msg.channel.send(`Pong! WS Ping: ${client.gateway.ping}`) } }) @@ -95,7 +95,7 @@ class PingCommand extends Command { name = 'ping' execute(ctx: CommandContext) { - ctx.message.reply(`pong! Ping: ${ctx.client.ping}ms`) + ctx.message.reply(`pong! Ping: ${ctx.client.gateway.ping}ms`) } } diff --git a/src/client/client.ts b/src/client/client.ts index 317265d..457d77b 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -78,8 +78,6 @@ export class Client extends HarmonyEventEmitter { rest: RESTManager /** User which Client logs in to, undefined until logs in */ user?: User - /** WebSocket ping of Client */ - ping = 0 /** Token of the Bot/User */ token?: string /** Cache Adapter */ diff --git a/src/gateway/mod.ts b/src/gateway/mod.ts index 8cb7f34..6764095 100644 --- a/src/gateway/mod.ts +++ b/src/gateway/mod.ts @@ -66,6 +66,7 @@ export class Gateway extends HarmonyEventEmitter { cache: GatewayCache private timedIdentify: number | null = null shards?: number[] + ping: number = 0 constructor(client: Client, shards?: number[]) { super() @@ -115,11 +116,9 @@ export class Gateway extends HarmonyEventEmitter { case GatewayOpcodes.HEARTBEAT_ACK: this.heartbeatServerResponded = true - this.client.ping = Date.now() - this.lastPingTimestamp - this.emit('ping', this.client.ping) - this.debug( - `Received Heartbeat Ack. Ping Recognized: ${this.client.ping}ms` - ) + this.ping = Date.now() - this.lastPingTimestamp + this.emit('ping', this.ping) + this.debug(`Received Heartbeat Ack. Ping Recognized: ${this.ping}ms`) break case GatewayOpcodes.INVALID_SESSION: diff --git a/test/index.ts b/test/index.ts index a2cdd18..872f457 100644 --- a/test/index.ts +++ b/test/index.ts @@ -60,7 +60,7 @@ client.on('messageCreate', async (msg: Message) => { console.log(`${msg.author.tag}: ${msg.content}`) } if (msg.content === '!ping') { - msg.reply(`Pong! Ping: ${client.ping}ms`) + msg.reply(`Pong! Ping: ${client.gateway.ping}ms`) } else if (msg.content === '!members') { const col = await msg.guild?.members.array() const data = col From 30cd8e10dc90cac353ccd627dfc46d9a992ab159 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Sat, 17 Apr 2021 13:49:26 +0530 Subject: [PATCH 3/3] fix .ping usage --- test/cmds/ping.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cmds/ping.ts b/test/cmds/ping.ts index 852941c..4b3db95 100644 --- a/test/cmds/ping.ts +++ b/test/cmds/ping.ts @@ -5,6 +5,6 @@ export default class PingCommand extends Command { execute(ctx: CommandContext): void { console.log(ctx.args, ctx.argString) - ctx.message.reply(`Pong! Latency: ${ctx.client.ping}ms`) + ctx.message.reply(`Pong! Latency: ${ctx.client.gateway.ping}ms`) } }