Create command
Esta página aún no está disponible en tu idioma.
To start creating a command, follow these steps:
-
Run the following command to create file and boilerplate initial content:
Terminal window npx zumito-cli create command -
It will ask you for the name of the command, and the module where you want to create it.
-
After that, it will create a file in the
src/modules/<module>/commands
folder with the name of the command. The file will contain the following code:import { Command, CommandParameters, ZumitoFramework, CommandType, SelectMenuParameters } from "zumito-framework";export class CommandName extends Command {async execute({ message, interaction, args, guildSettings }: CommandParameters): Promise<void> {// Command code}async selectMenu({ path, interaction, guildSettings }: SelectMenuParameters): Promise<void> {}}
Command code
Here you can write the code that the bot will run when user execute the command
async executeSlashCommand({ interaction, args, guildSettings }: CommandParameters): Promise<void> { // Code here will run when slash command is executed interaction.reply({ content: "Test message", }) }
async executePrefixCommand({ message, args, guildSettings }: CommandParameters): Promise<void> { // Code here will run when prefix command is executed message.reply({ content: "Test message", }) }
async execute({ message, interaction, args, guildSettings }: CommandParameters): Promise<void> { // Code here will run when slash or prefix command is executed (message||interaction).reply({ content: "Test message", }) }
Execute parameters
The execute
function receives a CommandParameters
object, which contains the following properties:
-
message
- Discord.Message object. Undefined if the command is executed from an interaction (slash command). -
interaction
- Discord.Interaction object. Undefined if the command is executed from a message with the prefix. -
args
- Arguments of the command. Contains the arguments user passed to the command.-
You can get the arguments using the
get
method, which receives the name of the argument and the type of the argument.args.get("argumentName"); // Output: "Argument value" -
Output comes in the type you specified, so if you specify a number, the output will be a number and if you specify a user it will be a Discord.User object.
-
-
guildSettings
- Guild settings object. Contains the settings of the guild where the command was executed. The data is loaded from the database on each command execution. -
trans
- A shorthand function to translate strings. It receives the key of the string and the variables to replace in the string.- It does not require language to be specified, it will use the language of the guild where the command was executed.
- The key is relative to current command, so if you have a key named
command.yourCommandName.yourKey
, you can use it like this:trans("yourKey")
. if you want to use a key from anywhere else, you can use$
prefix, like this:trans("$command.yourCommandName.yourKey")
.
Command atributes
In the header of your command class, you can define some atributes. You can find here a little description of each one.
adminOnly
: true/false. Defines if the command can be used only by guild admins or any user. Defaults to any user (false)aliases
: array of strings. Defines alternative alias for the commnad.args
: array of CommandArgDefinition. Defines the parameters that te user can use on the command.binds
: CommandBinds object. Defines event listeners related to command. Here you can listen for select menu interaction, modal form submit, etc.botPermissions
: WIPcategories
: array of strings. Defines the categories of the command. Useful for help command.cooldown
: WIPdm
: true/false. Defines if the command can be runned in a dm channel. Defaults to false.examples
: array of strings. Defines example usages of the bot. So for aclean
command with first argument as a number you can write['clean 100']
. Do not write any prefix.hidden
: true/false. Defines if the command would be hidden from help commands. The command will be still usable.name
: Defines alternative name if you don’t want to use class name.nsfw
: true/false. Defines if the command could be executed out of a nsfw channel. Defaults to false (The command can be runned anywhere)parent
: string. Parent command. Useful for subcommands. WIP.type
: CommandType object. Defines if the command can be runned using slash and/or prefix. CommandType.slash / CommandType.prefix / CommandType.any
Tips
Acces to discord client instance
To get the discord.js client instance you will need to do some simple steps:
-
Import
ServiceContainer
fromzumito-framework
andClient
fromzumito-framework/discord
import { ServiceContainer, Command, CommandParameters, ZumitoFramework, CommandType, SelectMenuParameters } from "zumito-framework";import { Client } from 'zumito-framework/discord';export class CommandName extends Command {...} -
Define variable inside the class
export class CommandName extends Command {private client: Client;} -
Initialize variable by obtaining from ServiceContainer:
export class CommandName extends Command {private client: Client;constructor() {super();this.client = ServiceContainer.get(Client);}} -
Now you can use the client in any part of the command:
export class CommandName extends Command {private client: Client;constructor() {super();this.client = ServiceContainer.get(Client);}async execute(params): Promise<void> {console.log(this.client.status);}}
Read command args
args
parameter of the execute
function contains the arguments user passed to the command.
You can get the arguments using the get
method, which receives the name of the argument and the type of the argument.
async execute({ message, interaction, args, client, framework, guildSettings }: CommandParameters): Promise<void> { const textArg: string = args.get("argumentName"); // Output: "Argument value"}
Output: String
async execute({ message, interaction, args, client, framework, guildSettings }: CommandParameters): Promise<void> { const userArg: User = args.get("argumentName");}
Output: User
async execute({ message, interaction, args, client, framework, guildSettings }: CommandParameters): Promise<void> { const channelArg: GuildChannel = args.get("argumentName");}
Output: GuildChannel