|
| 1 | +import { randomUUID } from 'crypto'; |
| 2 | +import { Client, CommandInteraction, Interaction, Snowflake } from 'discord.js'; |
| 3 | +import { Logger } from 'pino'; |
| 4 | +import { Base, BaseConfig } from './Base'; |
| 5 | +import { Bot } from './Bot'; |
| 6 | + |
| 7 | +export class CommandManager { |
| 8 | + /** |
| 9 | + * Registered slash commands. |
| 10 | + */ |
| 11 | + public readonly commands = new Map<Snowflake, Command>(); |
| 12 | + |
| 13 | + private bot!: Bot; |
| 14 | + |
| 15 | + public constructor(private holds: Command[]) { |
| 16 | + this._interactionHandler = this._interactionHandler.bind(this); |
| 17 | + } |
| 18 | + |
| 19 | + public async _interactionHandler(interaction: Interaction): Promise<void> { |
| 20 | + if (!interaction.isCommand()) { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + const command = this.commands.get(interaction.commandId); |
| 25 | + |
| 26 | + if (!command) { |
| 27 | + this.bot.logger |
| 28 | + .child({ |
| 29 | + id: randomUUID(), |
| 30 | + type: 'CommandManager', |
| 31 | + }) |
| 32 | + .error( |
| 33 | + 'unregistred slash command %s (id: %s)', |
| 34 | + interaction.commandName, |
| 35 | + interaction.commandId, |
| 36 | + ); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + const logger = this.bot.logger.child({ |
| 41 | + id: randomUUID(), |
| 42 | + type: 'Command', |
| 43 | + commandName: command.name, |
| 44 | + }); |
| 45 | + |
| 46 | + try { |
| 47 | + logger.debug('execute command handler'); |
| 48 | + await command.handler({ client: this.bot.client, logger, interaction }); |
| 49 | + } catch (error) { |
| 50 | + logger.error(error, 'command handler error'); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public async start(bot: Bot): Promise<void> { |
| 55 | + this.bot = bot; |
| 56 | + |
| 57 | + // The application cannot be null if the Client is ready. |
| 58 | + const application = this.bot.client.application!; // eslint-disable-line @typescript-eslint/no-non-null-assertion |
| 59 | + |
| 60 | + await Promise.all( |
| 61 | + this.holds.map(async (hold) => { |
| 62 | + const command = await application.commands.create({ |
| 63 | + name: hold.name, |
| 64 | + description: hold.description, |
| 65 | + }); |
| 66 | + this.commands.set(command.id, hold); |
| 67 | + }), |
| 68 | + ); |
| 69 | + |
| 70 | + // We don't need this.holds anymore. |
| 71 | + this.holds = []; |
| 72 | + |
| 73 | + this.bot.client.on('interactionCreate', this._interactionHandler); |
| 74 | + } |
| 75 | + |
| 76 | + public async stop(bot: Bot): Promise<void> { |
| 77 | + bot.client.off('interactionCreate', this._interactionHandler); |
| 78 | + |
| 79 | + // The application cannot be null if the Client is ready. |
| 80 | + const application = bot.client.application!; // eslint-disable-line @typescript-eslint/no-non-null-assertion |
| 81 | + |
| 82 | + // Unregister *all* registered slash commands. |
| 83 | + await Promise.all( |
| 84 | + [...application.commands.cache.values()].map((command) => |
| 85 | + command.delete(), |
| 86 | + ), |
| 87 | + ); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +export type CommandHandler = (context: CommandContext) => Promise<unknown>; |
| 92 | + |
| 93 | +export interface CommandContext { |
| 94 | + /** |
| 95 | + * discord.js Client instance. |
| 96 | + */ |
| 97 | + client: Client; |
| 98 | + /** |
| 99 | + * Pino logger. |
| 100 | + */ |
| 101 | + logger: Logger; |
| 102 | + /** |
| 103 | + * CommandInteraction instance. |
| 104 | + */ |
| 105 | + interaction: CommandInteraction; |
| 106 | +} |
| 107 | + |
| 108 | +export interface CommandConfig extends BaseConfig { |
| 109 | + /** |
| 110 | + * Command handler. |
| 111 | + */ |
| 112 | + handle: CommandHandler; |
| 113 | +} |
| 114 | + |
| 115 | +export class Command extends Base { |
| 116 | + public readonly handler: CommandHandler; |
| 117 | + |
| 118 | + public constructor(config: CommandConfig) { |
| 119 | + super(config); |
| 120 | + this.handler = config.handle; |
| 121 | + } |
| 122 | +} |
0 commit comments