I’m working on a Discord bot using Node.js and the discord.js-commando framework. When I type !clear
command, the bot responds with “Unknown command, do !help to view a list of command” but then it actually deletes the messages anyway. So the functionality works correctly but it keeps showing that error message first. I can’t figure out why this happens. Can someone help me understand what’s going wrong?
const commando = require('discord.js-commando');
const client = new commando.Client();
const cmdPrefix = '!';
client.on('message', msg => {
let content = msg.content.toUpperCase();
let author = msg.author;
let command = msg.content.slice(cmdPrefix.length).split(" ");
let parameters = command.slice(1);
if (content.startsWith(cmdPrefix + 'CLEAR')) {
async function deleteMessages() {
msg.delete();
if (isNaN(parameters[0])) {
msg.channel.send('Please provide a valid number of messages to delete \n Usage: ' + cmdPrefix + 'clear <count>');
return;
}
const messageList = await msg.channel.fetchMessages({limit: parameters[0]});
console.log(messageList.size + ' messages located, removing...');
msg.channel.bulkDelete(messageList)
.catch(err => msg.channel.send(`Error occurred: ${err}`));
}
deleteMessages();
}
});
client.login('BOT TOKEN GOES HERE');