-
-
Notifications
You must be signed in to change notification settings - Fork 40
@Command
discord.ts provides a decorator allowing the implementation of command systems very simply by essentially using only two decorators @Command(commandName?: string) and @CommandNotFound().
We will also use @Discord(prefix: string) to specify a prefix for our commands within the class.
You can specify a regex expression for your command names
For advanced usage use the @Rules decorator, you can also specify aliases using that
Notice that the first arguments do not use
ArgsOf, the first payload is aCommandMessage.
import {
Discord,
On,
Client,
Command,
CommandMessage,
CommandNotFound
} from "@typeit/discord";
// Specify your prefix
@Discord("!")
abstract class AppDiscord {
// Reachable with the command: !hello
@Command("hello")
private hello(message: CommandMessage) {
}
// !bye
// !yo
@CommandNotFound()
private notFound(message: CommandMessage) {
}
}The CommandMessage is the first argument injected into a method using @Command or @CommandNotFound, it has exactly the same structure as the Message object in discord.js except that it includes useful information about the command that was executed such as:
-
prefix:string
The prefix that is applied to your command. -
commandName:string
The command name -
commandContent:string
The message content without the prefix (-cmd hello therebecomeshello there) -
description:string
The command description -
infos:InfoType(any)
The command infos -
args:ArgsType(any)
The command arguments -
discord:DiscordInfos:
The linked@Discordclass infos -
argsRules: (ArgsRulesFunction<Expression>[])
The rules that are applied to execute the command (advanced)
You have the ability to specify arguments for your command, as express.js does in it's routing system. So by using ":" (or the value specified in variablesChar when your Client intializes) in the name of your @Command in front of the dynamic values, discord.ts will extract these informations when a command is executed and inject it into the args property of your CommandMessage with the correct name that you indicated in the command name.
If the argument value is a number the value will be casted automaticaly
@Discord("!")
abstract class AppDiscord {
@Command("args :slug :number")
private hello(message: CommandMessage) {
const mySlug = message.args.slug;
const myNumber = message.args.number;
// Using js destructuring:
const { slug, number } = message.args;
}
}