pnp-scheduler/source/Discord/CommandDeployer.ts

36 lines
No EOL
1.3 KiB
TypeScript

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: object[] = [];
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
await this.client.RESTClient.put(
Routes.applicationGuildCommands(this.client.ApplicationId, serverId),
{ body: commandInfos },
);
this.logger.log(`Successfully reloaded ${commandInfos.length} application (/) commands for ${serverId}.`);
}
}