Note: This framework is not yet stable, although it does have enough functionality to let you create a Discord bot that uses slash commands.
I've wanted to re-write my bots to use slash commands and haven't been able to find a good framework to simplify the repetitive work. In the past, I've really liked slash-create, but I've found it hard to integrate with Discord.js and to leverage the type-safety of TypeScript.
// index.ts
import { Client, Intents } from 'discord.js';
import { SlasherClient } from 'slasher.js';
import { PingCommand } from './commands/Ping.command';
const client = new Client({
intents: [
Intents.FLAGS.DIRECT_MESSAGES
],
});
const slasher = new SlasherClient(client);
client.on('ready', async () => {
await slasher.commands.registerCommands([
PingCommand
]);
});
client.login(process.env.TOKEN);
// commands/Ping.command.ts
import { BooleanOption, SlasherCommand, SlasherContext, ChannelOption, SlasherClient } from 'slasher.js';
type PingProps = {
pong?: BooleanOption,
}
export class PingCommand extends SlasherCommand<PingProps> {
async run(context: SlasherContext<PingProps>) {
context.interaction.reply({
content: context.props.pong ? 'Pong!' : 'Yeah, I\'m working fine.'
});
}
constructor(client: SlasherClient) {
super(client, {
name: 'ping',
description: 'It responds for you!',
options: [{
type: 'BOOLEAN',
name: 'pong',
description: 'Whether it should pong or just... respond.',
required: false
}],
guildId: '12345678'
});
}
}
- [ ] change the constructor override to a decorator, because it would look nicer and be less scary
- [ ] add support for subcommands