Commands - Paultje52/SupaBotBase GitHub Wiki

Commands are triggered when there is a valid chat message or when a slash command is triggered. You can disable one of these triggers in the Config.

Constructor

To make things easy, it's recommended to extend your class with the CommandConstructor of SupaBotBase: require("SupaBotBase").CommandConstructor.

const CommandConstructor = require(`SupaBotBase`).CommandConstructor;
module.exports = class Ping extends CommandConstructor {
  constructor() {
    super();
    // Configure your command
  }
}

In your constructor, you can use the following functions to configure your command.

this.setHelp()

First, the sethelp. This is used to set your command name, description, usage and category. The name is required. If you're registering slash commands, the description is also required. The parameter is an object with these keys. For usage, you can use %PREFIX% for the prefix and %CMD% for the command name (or alias).

Example

this.setHelp({
  name: "command_name",
  description: "Does something!"",
  usage: "%PREFIX%%CMD%",
  category: "SomeCategory"
});

this.setExamples()

With this method, you can set your command examples. Each new example, is a new parameter in this method. You can use %PREFIX% for the prefix and %CMD% for the command name (or alias).

Example

this.setExamples(
  "%PREFIX%%CMD% something",
  "%PREFIX%%CMD% lol"
)

this.setAliases()

This method allowes you to set aliases for normal text command calls. Each new example, is a new parameter in this method.

Example

this.setAliases("alias1", "alias2", "alias3");

this.setSlashCommandsEnabled()

If the parameter in this method is false, slash commands are disabled for this command. The default value is true.

Example

this.setSlashCommandsEnabled(false);

this.setSlashCommandType()

You can set the type of message response to a slash command. To use this, you need to use message.answerCommand("ANSWER"). There are two options.

  • Shown (default)
  • Hidden

There are many limitations to the hidden command response.

  • You can't send embeds or files, just normal text
  • You can't edit the message and when the user reloads their discord client, the message is deleted.

Example

this.setSlashCommandType("hidden");

this.setRequiredPermissions()

This method has one parameters: An object with two optional keys: bot and user. The value needs to be an array with strings, the strings are the permission names from discord.

Example

this.setRequiredPermissions({
  bot: ["MANAGE_MESSAGES"],
  user: ["MANAGE_MESSAGES"]
});

this.setRestriction()

This method allowes you to set a channel, user or guild restriction. This restriction can be specific in the command or can be a key from the database, so you can update your restriction in your code.

Examples

this.setRestriction("user", "specific", ["ID1", "ID2"]);
this.setRestriction("channel", "specific", "ID3");
this.setRestriction("guild", "database", "db-key");
// db-key (key in database) = ["ID4", "ID5"]

this.setPermissionChecks()

You can add permission checks in the main bot file, click here to see how.

Example

this.setPermissionChecks("test1", "test2");

setArgs()

With this function, you can set your command arguments. For a message, the arguments get automaticly checked and parsed!

Warning: When setting the arguments, SupaBaseBot will try to parse the arguments, even for normal messages. When arguments aren't correct the bot won't run the command. When they are correct, the correct type is assigned. When you, for example, set the type as a member, this is automatically parsed, so the argument is a member object.

For more information about how to use the setArgs, please look at the discord docs.

V1.2

This is how it's going to be in the newest release, v1.2. It's currently still in the beta face.

Each new argument is a new parameter in this function. An argument can be constructed with CommandArgument. Here is an example.

this.setArgs(
  // First argument
  new CommandArgument(types.user)
    .setName("user")
    .setDescription("The target user")
    .setRequired(true),
  // Second argument
  new CommandArgument(types.string)
    .setName("action")
    .setDescription("An action for the user")
    .setRequired(true)
    .setOptions(
      // First choice
      new CommandArgumentChoice()
        .setName("Ban the user")
        .setValue("ban"),
      // Second choice
      new CommandArgumentChoice()
        .setName("Kick the user")
        .setValue("kick")
    )
);

Whole command

const CommandConstructor = require(`../../src/SupaBotBase`).CommandConstructor;

module.exports = class Ping extends CommandConstructor {

  constructor() {
    super();

    this.setHelp({
      name: "ping",
      description: "Test my ping",
      category: "Main",
      usage: "%PREFIX%%CMD%"
    });
    this.setAliases("p", "pingpong");

    this.setSlashCommandsEnabled(true);
    this.setSlashCommandType("hidden");

    this.setPermissionChecks("testCheck");
    this.setRestriction("user", "specific"; "ID");
    this.setRequiredPermissions({
      bot: ["EMBED_LINKS"]
    });
  }

  async onExecute(message) {
    if (message.isSlashCommand) {
      if (this.last) message.answerCommand(`**Last ping**\nšŸ“ ${this.last.ping}ms\nšŸ’™ ${this.last.ws}ms`)
      else message.answerCommand(`Pong! Use \`${message.prefix}ping\` to calculate my ping!`);
      return;
    }

    let start = Date.now();
    let msg = await message.channel.send(message.embed().setDescription("šŸ“"));

    let ping = Math.round(Date.now()-start-this.client.ws.ping);
    let ws = Math.round(this.client.ws.ping);

    msg.edit(message.embed()
      .setDescription(`šŸ“ ${ping}ms\nšŸ’™ ${ws}ms`)
    );

    this.last = {
      ping,
      ws
    };
  }

}