Commands

Slash commands were implemented with the aim of replacing messages considered as commands with a start-of-line character, often defined as a message prefix like !.

Important

Please see the dedicated section for more information.


Introduction

The Discord API allows users to interact with bots in a more intuitive and structured way, using predefined syntax to make it easier to execute specific commands without having to remember prefixes or complex message formats.

They were created to replace message-based commands with prefixes, making the user experience smoother and reducing syntax errors.

Note

When you change the structure of your commands, please restart your entire application process so that the changes take effect even if the hmr is active.

Builder approach

From the Client, we can chain commands listeners using the following pattern :

Future<void> main(_, port) async {
  final client = ClientBuilder()
    .setHmrDevPort(port)
    .build();

  client.commands.declare((command) {
    command
      ..setName('foo')
      ..setDescription('This is a command description')
      ..setHandler((ctx) => ctx.interaction.reply('Hello, world!'));
  });

  await client.init();
}

Object-oriented approach

For benchmarking purposes, we recommend using the functional approach, but when developing applications requiring more than 2 events or commands, we recommend using the object-oriented approach.

final class MyCommand implements CommandDeclaration {
  void handle(CommandContext ctx) {
    print('Hello, world!');
  }

  @override
  CommandDeclarationBuilder build() {
    return CommandDeclarationBuilder()
      ..setName('foo')
      ..setDescription('This is a command description')
      ..setHandler(handle);
  }
}