Skip to main content

๐Ÿ–ฒ๏ธ 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, or InteractionCommand)
  • cmd โ†’ name of the command
  • args โ†’ 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 command
  • name โ†’ name of the command
  • permissions โ†’ list of methods to check ifinteraction.member has the right permissions
  • builder โ†’ a SlashCommandBuilder
  • any_guild โ†’ if false, the command can be executed only on the main guild
  • dm โ†’ if true, we can use this command in direct messages