Issue
Hello, I’m having an issue with aliases in discord.js the aliases don’t work, I don’t know why?
client.on('message', message =>{
// Exit when the message from the bot
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
if (!client.commands.has(commandName)) return;
const command = client.commands.get(commandName) || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
try {
command.execute(message, args, commandName, client, Discord);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
});
Solution
You can’t use aliases because you put
if(!client.commands.has(commandName)) return;
This returns false if you try an alias, or anything else that isn’t in your client.commands
keys. Remove this line and use this instead:
const args = message.content.slice(prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName) || client.commands.find(cmd => cmd.aliases?.includes(commandName));
if(!command) return;
//...
This looks for the command first and then if no command is found, name or alias, it returns
Answered By – MrMythical
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0