separated registering process to a new method

This commit is contained in:
mmacierzanka committed 2021-08-27 14:31:50 +02:00
1 parent 796e27f54c
commit a8efeae9cc
1 file changed
+36 -28
+36 -28
View File
@@ -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<void>*
*/
async register(): Promise<void> {
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;