Slash commands
Slash commands are a new system for commands within Discord bots. NYXBCommands aims to help developers implement slash commands in the easiest way possible. Here is a basic ping pong slash command example:
TypeScript
import { CommandObject, CommandType } from '@nyxb/commands'
export default {
description: 'Ping pong command',
// Only create a slash command, not a legacy command
// You can also use "CommandType.BOTH" to create a
// legacy and slash command
type: CommandType.SLASH,
callback: () => {
return {
content: 'Pong!',
ephemeral: true,
}
},
} as CommandObject
When running /ping
it will send "pong" in the same channel.
All of the normal NYXBCommands callback arguments are available aside from message. A full list can be found here.
Using your own Options for Slash Commands
Sometimes you may want to pass in your own options object to your slash command. You can easily do so by passing in an options property to your command like so:
TypeScript
export default {
category: 'test', // Required for the slash commands
description: 'Adds two numbers', // Required for the slash commands
slash: true, // If options are given then slash must be either true or 'both'
testOnly: true, // Ensure you have test servers setup
options: [
{
name: 'num1', // Must be lower case
description: 'The first number.',
required: true,
type: OptionType.STRING, // Replace 'STRING' with OptionType.STRING if applicable
},
{
name: 'num2', // Must be lower case
description: 'The second number.',
required: true,
type: OptionType.STRING, // Replace 'STRING' with OptionType.STRING if applicable
},
],
callback: ({ interaction, args }) => {
const num1 = Number.parseInt(args[0])
const num2 = Number.parseInt(args[1])
if (interaction) {
interaction.reply({
content: String(num1 + num2),
})
}
},
} as CommandObject
CommandsRequired permissionsYou may want your commands to only be ran by users who have specific Discord permissions. This is often useful for administration commands.
Using NYXBCommands you can easily specify what Discord permissions users need to run the command:CommandsInferred slash command argumentsSlash commands handle arguments differently, however NYXBCommands will allow you to specify your slash command arguments in the same way as normal commands.