Skip to content

Commands

Danny SMc edited this page Dec 24, 2021 · 3 revisions

To work with commands, there are 6 decorators in total, all explained below.

Please be aware, that if you setup a middleware, all auth decorators can still be used with these commands.

As standard, the translation library is also automatically included, so you can call on translation if required for your project.


Class Decorators

@Command

This command is used for defining a class as a Discord command controller, and expects a slash command builder instance, this allows you to define your command using the builder, which has a really nice interface to work with and full type hinting.


Example

import { Command, AbstractCommand } from '@symbux/turbo-discord';
import { SlashCommandBuilder } from 'discord.js';

@Command(
	new SlashCommandBuilder()
		.setName('confirm')
		.setDescription('Will simply ask you a random question.'),
)
export default class UsersCommand extends AbstractCommand {
	// ...
}



Method Decorators

@On.Command

The on command decorator is a method decorator to define a controller method as an event listener for the main command incoming event, so when you call the /confirm command it will go to this method, this decorator has no arguments.

Note: like the WS plugin, the method does not need to return anything, as all responses can be interacted with from the context class.


Example

import { Command, AbstractCommand, On, Context } from '@symbux/turbo-discord';
import { SlashCommandBuilder } from '@discordjs/builders';

@Command(
	new SlashCommandBuilder()
		.setName('confirm')
		.setDescription('Will simply ask you a random question.'),
)
export default class UsersCommand extends AbstractCommand {

	@On.Command()
	public async command(context: Context): Promise<void> {
		// ...
	}
}



@On.Button

The on button command is the event listener for any button events that are created by this class, this is done by using a unique ID generated by each class at runtime, this ID then can be passed with a custom string to the button as it's customId, allowing it to route back to this controller.

Note the this.getUniqueKey() in the below example.


Example

import { Command, AbstractCommand, On, Context } from '@symbux/turbo-discord';
import { SlashCommandBuilder } from '@discordjs/builders';
import { CommandInteraction, MessageActionRow, MessageButton, MessageEmbed } from 'discord.js';

@Command(
	new SlashCommandBuilder()
		.setName('example')
		.setDescription('Will simply ask you a random question.'),
)
export default class ExampleCommand extends AbstractCommand {

	@On.Command()
	public async command(context: Context): Promise<void> {

		// Defer response.
		await context.defer();

		// Get the interaction.
		const interaction = context.getInteraction<CommandInteraction>();

		// Create an embed.
		const embed = new MessageEmbed()
			.setTitle('Answer this question...')
			.setDescription('Are you cool?');

		// Create the buttons.
		const buttonRow = new MessageActionRow()
			.addComponents(
				new MessageButton()
					.setCustomId(`${this.getUniqueKey()}:yes`)
					.setLabel('Yes')
					.setStyle('SUCCESS'),
				new MessageButton()
					.setCustomId(`${this.getUniqueKey()}:no`)
					.setLabel('No')
					.setStyle('DANGER'),
			);

		// Send the response.
		await interaction.editReply({
			embeds: [embed],
			components: [buttonRow],
		});
	}

	@On.Button('yes')
	public async buttonYes(context: Context): Promise<void> {
		console.log(context, 'ExampleCommand::buttonYes');
	}

	@On.Button('no')
	public async buttonNo(context: Context): Promise<void> {
		console.log(context, 'ExampleCommand::buttonNo');
	}
}



@On.SelectMenu

The on select menu command is the event listener for any select menu events that are created by this class, this is done by using a unique ID generated by each class at runtime, this ID then can be passed with a custom string to the menu select as it's customId, allowing it to route back to this controller.

Note the this.getUniqueKey() in the below example.


Example

import { Command, AbstractCommand, On, Context } from '@symbux/turbo-discord';
import { SlashCommandBuilder } from '@discordjs/builders';
import { CommandInteraction, MessageActionRow, MessageEmbed, MessageSelectMenu } from 'discord.js';

@Command(
	new SlashCommandBuilder()
		.setName('example')
		.setDescription('Will simply ask you a random question.'),
)
export default class ExampleCommand extends AbstractCommand {

	@On.Command()
	public async command(context: Context): Promise<void> {

		// Defer response.
		await context.defer();

		// Get the interaction.
		const interaction = context.getInteraction<CommandInteraction>();

		// Create an embed.
		const embed = new MessageEmbed()
			.setTitle('Answer this question...')
			.setDescription('Which animal do you like more?');

		// Create the select menu.
		const selectRow = new MessageActionRow()
			.addComponents(
				new MessageSelectMenu()
					.setCustomId(`${this.getUniqueKey()}:animal`)
					.setPlaceholder('Select an option...')
					.addOptions([
						{
							label: 'Chicken',
							description: 'A chicken is a bird.',
							value: 'chicken',
						},
						{
							label: 'Cat',
							description: 'A cat is a cat.',
							value: 'cat',
						},
					]),
			);

		// Send the response.
		await interaction.editReply({
			embeds: [embed],
			components: [selectRow],
		});
	}

	@On.SelectMenu('animal')
	public async selectAnimal(context: Context): Promise<void> {
		console.log(context, 'ExampleCommand::selectAnimal');
	}
}