From a8efeae9ccc7d70403f71bb496e7f8c4e61adeb6 Mon Sep 17 00:00:00 2001 From: GRZ4NA Date: Fri, 27 Aug 2021 14:31:50 +0200 Subject: [PATCH] separated registering process to a new method --- src/Bot.ts | 64 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/src/Bot.ts b/src/Bot.ts index e919dcd..f5a70cb 100644 --- a/src/Bot.ts +++ b/src/Bot.ts @@ -105,34 +105,7 @@ export class Bot extends EventEmitter { this.client.on("ready", async () => { if (register === undefined || register === true) { console.log("✔"); - process.stdout.write("Registering commands... "); - await axios.put( - `https://discord.com/api/v8/applications/${this.applicationId}/commands`, - this.commands.list.filter((c) => !Array.isArray(c.guilds) && c.slash).map((c) => c.toCommandObject()), - { headers: { Authorization: `Bot ${this.token}` } } - ); - const guilds: any = {}; - this.commands.list - .filter((c) => Array.isArray(c.guilds) && c.guilds.length > 0) - .map((c) => { - c.guilds?.map(async (g) => { - if (!(await this.client.guilds.fetch(g))) { - throw new Error(`"${g}" for "${c.name}" command is not a valid guild ID`); - } - if (guilds[g]) { - guilds[g].push(c.toCommandObject()); - } else { - guilds[g] = []; - guilds[g].push(c.toCommandObject()); - } - }); - }); - for (let i in guilds) { - await axios.put(`https://discord.com/api/v8/applications/${this.applicationId}/guilds/${i}/commands`, guilds[i], { - headers: { Authorization: `Bot ${this.token}` }, - }); - } - console.log("✔\n"); + this.register(); } else { console.log("✔\n"); } @@ -232,6 +205,41 @@ export class Bot extends EventEmitter { return false; } } + + /** + * Registers commands in Discord API + * @returns *Promise* + */ + async register(): Promise { + process.stdout.write("Registering commands... "); + await axios.put( + `https://discord.com/api/v8/applications/${this.applicationId}/commands`, + this.commands.list.filter((c) => !Array.isArray(c.guilds) && c.slash).map((c) => c.toCommandObject()), + { headers: { Authorization: `Bot ${this.token}` } } + ); + const guilds: any = {}; + this.commands.list + .filter((c) => Array.isArray(c.guilds) && c.guilds.length > 0) + .map((c) => { + c.guilds?.map(async (g) => { + if (!(await this.client.guilds.fetch(g))) { + throw new Error(`"${g}" for "${c.name}" command is not a valid guild ID`); + } + if (guilds[g]) { + guilds[g].push(c.toCommandObject()); + } else { + guilds[g] = []; + guilds[g].push(c.toCommandObject()); + } + }); + }); + for (let i in guilds) { + await axios.put(`https://discord.com/api/v8/applications/${this.applicationId}/guilds/${i}/commands`, guilds[i], { + headers: { Authorization: `Bot ${this.token}` }, + }); + } + console.log("✔\n"); + } } export default Bot;