-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: feat(framework): slash commands
- Loading branch information
Showing
10 changed files
with
193 additions
and
12,713 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Command } from '../framework'; | ||
|
||
export default new Command({ | ||
name: 'hello', | ||
description: 'Vous répond Hello, world!', | ||
enabled: true, | ||
handle({ interaction }) { | ||
return interaction.reply('Hello, world!'); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { randomUUID } from 'crypto'; | ||
import { Client, CommandInteraction, Interaction, Snowflake } from 'discord.js'; | ||
import { Logger } from 'pino'; | ||
import { Base, BaseConfig } from './Base'; | ||
import { Bot } from './Bot'; | ||
|
||
export class CommandManager { | ||
/** | ||
* Registered slash commands. | ||
*/ | ||
public readonly commands = new Map<Snowflake, Command>(); | ||
|
||
private bot!: Bot; | ||
|
||
public constructor(private holds: Command[]) { | ||
this._interactionHandler = this._interactionHandler.bind(this); | ||
} | ||
|
||
public async _interactionHandler(interaction: Interaction): Promise<void> { | ||
if (!interaction.isCommand()) { | ||
return; | ||
} | ||
|
||
const command = this.commands.get(interaction.commandId); | ||
|
||
if (!command) { | ||
this.bot.logger | ||
.child({ | ||
id: randomUUID(), | ||
type: 'CommandManager', | ||
}) | ||
.error( | ||
'unregistred slash command %s (id: %s)', | ||
interaction.commandName, | ||
interaction.commandId, | ||
); | ||
return; | ||
} | ||
|
||
const logger = this.bot.logger.child({ | ||
id: randomUUID(), | ||
type: 'Command', | ||
commandName: command.name, | ||
}); | ||
|
||
try { | ||
logger.debug('execute command handler'); | ||
await command.handler({ client: this.bot.client, logger, interaction }); | ||
} catch (error) { | ||
logger.error(error, 'command handler error'); | ||
} | ||
} | ||
|
||
public async start(bot: Bot): Promise<void> { | ||
this.bot = bot; | ||
|
||
// The application cannot be null if the Client is ready. | ||
const application = this.bot.client.application!; // eslint-disable-line @typescript-eslint/no-non-null-assertion | ||
|
||
await Promise.all( | ||
this.holds.map(async (hold) => { | ||
const command = await application.commands.create({ | ||
name: hold.name, | ||
description: hold.description, | ||
}); | ||
this.commands.set(command.id, hold); | ||
}), | ||
); | ||
|
||
// We don't need this.holds anymore. | ||
this.holds = []; | ||
|
||
this.bot.client.on('interactionCreate', this._interactionHandler); | ||
} | ||
|
||
public async stop(bot: Bot): Promise<void> { | ||
bot.client.off('interactionCreate', this._interactionHandler); | ||
|
||
// The application cannot be null if the Client is ready. | ||
const application = bot.client.application!; // eslint-disable-line @typescript-eslint/no-non-null-assertion | ||
|
||
// Unregister *all* registered slash commands. | ||
await Promise.all( | ||
[...application.commands.cache.values()].map((command) => | ||
command.delete(), | ||
), | ||
); | ||
} | ||
} | ||
|
||
export type CommandHandler = (context: CommandContext) => Promise<unknown>; | ||
|
||
export interface CommandContext { | ||
/** | ||
* discord.js Client instance. | ||
*/ | ||
client: Client; | ||
/** | ||
* Pino logger. | ||
*/ | ||
logger: Logger; | ||
/** | ||
* CommandInteraction instance. | ||
*/ | ||
interaction: CommandInteraction; | ||
} | ||
|
||
export interface CommandConfig extends BaseConfig { | ||
/** | ||
* Command handler. | ||
*/ | ||
handle: CommandHandler; | ||
} | ||
|
||
export class Command extends Base { | ||
public readonly handler: CommandHandler; | ||
|
||
public constructor(config: CommandConfig) { | ||
super(config); | ||
this.handler = config.handle; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './Bot'; | ||
export * from './Command'; | ||
export * from './Cron'; | ||
export * from './FormatChecker'; | ||
export * from './helpers'; |