Slash Commands - Paultje52/SupaBotBase GitHub Wiki

SupaBotBase has support for discord's slash command. You can read the Discord slash commands documentation here. SupaBotBase automatically handles slash command calls, parses the args, creates an internal message object and calls the command.

Command settings

There are two command settings for slash commands, setSlashCommandsEnabled() and setSlashCommandType(). Click on these functions for more information.

Args

You can also add arguments to commands. This is a bit complicated, but it's explained very well in the discord documentation. There are just two differences.

  1. You can set a normal javascript object, no need for json.
  2. What you put in the setArgs() function, is the objects you put into options in the Discord documentation. Don't use an array, just use each option as a new argument.

When setting a sub-command, you can just put options in that object. For the value of options, put an array with the arguments for that sub-command in there.

Guild support

Currently, SupaBaseBot only supports global slash commands and the same commands in one guild for testing.

Enabling slash commands

It's recommended to enable slash commands after your bot is logged in. The bot.start() method is asynchronous. Await it and then call bot.registerSlashCommands() method. When in development, set the argument as your development guild id, so the slash commands are applied immediately.

Removing all the slash commands

You can remove all the slash commands using bot.removeSlashCommands(). Just like registering slash commands, you can set the guildID so it removes the slash commands within that guild.

Limitations

There are a couple limitations while using slash commands. When receiving a slash command, you have up to 15 minutes to respond. You can respond with message.answerCommand(). After that, you can edit the message with .edit and remove it with .delete. Setting a timeout works just like discord.js' message#delete: {timeout: 1000} for a timeout of one second.

When the slash command is hidden, you can't send embeds. When it's show you can.

Example

First, in your bot.js, wait until the bot is ready and then register the slash commands. You can add a guildID to the method bot#registerSlashCommands while in development, so your slash commands get registered instantly. When registering globally, discord takes about an hour to update every client!

bot.start().then(() => {
  console.log("Bot logged in!");
  bot.registerSlashCommands();
});

After that, be sure that you use message#answerCommand in your commands so the bot gives a valid response to slash-commands. In the class Ping in commands/Ping.js

onExecute(message) {
  // Don't use message.channel.send!!!
  message.answerCommand("Pong!");
}

You can also hide your slash commands so only the executor sees it in each command constructor.

this.setSlashCommandType("hidden");

Warning: You can't edit messages when the type is hidden!

Editing slash-command responses is very easy. message#answerCommand returns a response, and when it's resolved, you can edit it with the edit method and delete it with the delete method, just like normal messages.

let msg = await message.answerCommand("Pinging...");
msg.edit("Pong!");

setTimeout(() => {
  msg.delete();
}, 5000);

Working with arguments is a bit different when using slash commands. SupaBotBase takes care of almost everything. You just have to give the command arguments in the command constructor. Click here to see how. As an added bonus, when setting the args, SupaBotBase automatically parses channels, users and roles, even for normal messages. When a response isn't valid, it automatically lets the user know where it went wrong!

Why wouldn't you implement slash commands when it's this easy!?