Moved deployment code to class
This commit is contained in:
parent
aef26b1cc3
commit
83209f642c
6 changed files with 61 additions and 39 deletions
37
source/Discord/CommandDeployer.ts
Normal file
37
source/Discord/CommandDeployer.ts
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import {DiscordClient} from "./DiscordClient";
|
||||
import {Logger} from "log4js";
|
||||
import {Routes, Snowflake} from "discord.js";
|
||||
|
||||
export class CommandDeployer {
|
||||
constructor(
|
||||
private readonly client: DiscordClient,
|
||||
private readonly logger: Logger
|
||||
) {
|
||||
}
|
||||
|
||||
public async deployAvailableServers() {
|
||||
const commandInfos = [];
|
||||
this.client.Commands.allCommands.forEach((command) => {
|
||||
commandInfos.push(command.definition().toJSON())
|
||||
})
|
||||
|
||||
const guilds = await this.client.RESTClient.get(Routes.userGuilds());
|
||||
const deployments = guilds.map(guild => {
|
||||
return this.deployServer(commandInfos, guild.id)
|
||||
})
|
||||
|
||||
await Promise.all(deployments);
|
||||
}
|
||||
|
||||
private async deployServer(commandInfos: object[], serverId: Snowflake) {
|
||||
this.logger.log(`Started refreshing ${commandInfos.length} application (/) commands for ${serverId}.`);
|
||||
|
||||
// The put method is used to fully refresh all commands in the guild with the current set
|
||||
const data = await this.client.RESTClient.put(
|
||||
Routes.applicationGuildCommands(this.client.ApplicationId, serverId),
|
||||
{ body: commandInfos },
|
||||
);
|
||||
|
||||
this.logger.log(`Successfully reloaded ${commandInfos.length} application (/) commands for ${serverId}.`);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue