Command Manager
Currently, you probably have something like this in your code:
const Discord = require('discordeno.js')
// Ideally you should move to an `.env` file
const config = require('./config.json')
const bot = Discord.createBot({
events: {
messageCreate(client, message) {
if (message.content === '!ping') {
client.helpers.sendMessage(message.channelId, { content: 'pong' })
}
},
},
intents: Discord.Intents.Guilds | Discord.Intents.GuildMessages,
token: config.token,
})
const client = Discord.enableCachePlugin(bot, {})
Discord.startBot(client)
Of course, if you add more and more commands and as your code base grows, you can lose track very quickly.
To avoid this, it is recommended to store the commands in separate folders divided into different categories.
Previously, we introduced you to our plugin structure, which has a lot of advantages.
├Plugins/
├── General/
│ ├── commands/
│ │ ├── ping.js
│ │ └── ...
├── Developer/
│ ├── commands/
│ │ ├── eval.js
│ │ └── ...
└── ...
Get this file from the nodejs template
const CommandManager = require('./template/Managers/CommandManager.js')
const manager = new CommandManager({})
manager.load({ plugin: true }) // Load the commands
client.commands = manager
client.commands.cache.get('ping') // Get the `ping` command
The Manager will automatically iterate through all files in the folder and then load them into the cache property, which is mapped on the command name.
Take a look at Create Command to learn how to create a command.
Handle Command
The manager also contains a handler for executing the command when a message is received.
Currently checks for permissions, cooldowns, and rate limits are not covered, but these will be added soon.
Message Create Event:
module.exports = async (client, message) => {
client.commands.isCommand(message)
}
Interaction Create Event:
module.exports = async (client, interaction) => {
client.commands.isInteraction(interaction)
}
You can also customize the isCommand
function to your use case.