๐ฒ๏ธ Commands
A command starts with a prefix (by default: +) or with / (for slash commands).
We are going to create a simple command that will give information about itself.
The first thing to do it's create a file inside ./sources/modules/<module_name>/commands
(replace <module_name> with the name of your module). In this example, we can call the
command itself so let's create ./sources/modules/<module_name>/commands/itself.js
and paste this code :
const { SlashCommandBuilder } = require("discord.js");
async function parse(interaction, cmd, args)
{
if (interaction.options)
{
// Slash command (/itselt)
await interaction.reply(
"๐ฒ๏ธ **Command type:**\n- slash command\n"+
`๐ **Command Name:**\n- ${cmd}\n`+
`๐ข **Number of arguments:**\n- ${args.length}\n`+
`โ๏ธ **Requested by:**\n- ${interaction.member.user.username}\n`
);
}
else
{
// Classic command (+itselt)
await interaction.channel.send(
"๐ฒ๏ธ **Command type:**\n- classic command"+
`๐ **Command Name:**\n- ${cmd}\n`+
`๐ข **Number of arguments:**\n- ${args.length}\n`+
`โ๏ธ **Requested by:**\n- ${interaction.member.user.username}\n`
);
}
}
module.exports = {
parse,
name: "itself",
permissions: [],
builder: new SlashCommandBuilder()
.setName("itself")
.setDescription("I give informations about myselft."),
any_guild: false,
dm: false
}
The method parse is run by system when a message is sent and starts with bot's prefix (+ by default)
or when a slash command is executed.
async function parse(interaction, cmd, args)
As you can see we have tree arguments:
interactionโ corresponding to the event (MessageCreate, orInteractionCommand)cmdโ name of the commandargsโ corresponds to all the elements that follow the command name in a classic command
โฏ e.g of args
+itself arg1 arg2
โฏ Exports
At the bottom of the file we have exports, which includes several important elements.
module.exports = {
parse,
name: "itself",
permissions: [],
builder: new SlashCommandBuilder()
.setName("itself")
.setDescription("I give informations about myselft."),
any_guild: false,
dm: false
}
parseโ method to handle the commandnameโ name of the commandpermissionsโ list of methods to check ifinteraction.memberhas the right permissionsbuilderโ aSlashCommandBuilderany_guildโ if false, the command can be executed only on the main guilddmโ if true, we can use this command in direct messages