Built-in MessageCreate Event
This event file essentially handles your command, check the permission level set, and arguments set for you. Once it pass the requirements you set it will execute the command.
This is the built-in MessageCreate event that you can use to customize your bot.
module.exports = {
name: "messageCreate",
execute(message, client) {
//the prefix(s) we set in our config.js file
const configPrefix = client.config.prefix;
//we're not listening for other bots that may ran our commands
if (message.author.bot) return;
// checks if prefix is a string and message start with the prefix
if (
typeof configPrefix === String &&
!message.content.startsWith(configPrefix)
)
return;
// checks if prefix is a array and message start with the prefix element
if (
Array.isArray(configPrefix) &&
!configPrefix.some((prefix) => message.content.startsWith(prefix))
)
return;
const prefix = Array.isArray(configPrefix)
? configPrefix.find((prefix) => message.content.startsWith(prefix))
: configPrefix;
//get the arguments of the command
const args = message.content.slice(prefix.length).trim().split(/ +/);
// the command name used to initate the command call
const command = args.shift().toLowerCase();
// check if the command is an aliases of a command or the command name
const cmd =
client.commands.get(command) ||
client.commands.get(client.aliases.get(command));
// get the user's permission level
// we use the built-in getPermissionLevel handler provided by DiscordeFeaturesHandler
const level = client.getPermissionsLevel({
author: message.author,
channel: message.channel,
guild: message.guild,
guildMember: message.member,
});
// if not a valid command then return
if (!cmd) return;
// if command is guildOnly and channel is a DM then return.
if (cmd.guildOnly && message.channel.type === "DM") {
return message.reply("I can't execute that command inside DMs!");
}
// get the permission level required to execute the command
const cmdPermissions = isNaN(cmd.permissions)
? client.levelCache[cmd.permissions]
: cmd.permissions;
// return a response if command is deny by permission level requirement
if (level < cmdPermissions) {
// don't show response if Bot Support or higher
if (cmdPermissions > 7 && client.config.hideDeniedBotAdminCommandsUsage) {
return console.log(
`[CMD DENIED]`,
`${message.content}`,
`${message.author.tag}`
);
}
return message.channel
.send(`You do not have permission to use this command.
Your permission level is ${level} (${
client.config.permissions.find((l) => l.level === level).name
})
This command requires level ${
client.config.permissions.find((l) => l.level === cmd.permissions).name
}} (${cmd.permissions})`);
}
//Check if cmd usage does NOT meet the usage criteria then return
if (
((!cmd.maxArgs || cmd.maxArgs === -1) && args.length < cmd.minArgs) ||
(cmd.maxArgs &&
cmd.maxArgs != -1 &&
(args.length < cmd.minArgs || args.length > cmd.maxArgs))
) {
return message.channel.send(
`Incorrect syntax usage! ${prefix}${command} ${cmd.usage}`
);
}
// now we try and execute the command.
try {
cmd.execute(message, args, client, level);
} catch (e) {
console.error(e, `Executing CMD: ${cmd.name}`);
message.reply("There was an error trying to execute that command!");
}
},
};
Last updated