I’m working on a Discord bot and trying to create a purge command that deletes multiple messages at once. However, I keep getting a ReferenceError saying that ‘message is not defined’. I’m still learning JavaScript so any help would be great.
Main bot file:
client.on('ready', () => {
console.log('Bot is now online and ready!')
const arguments = msg.content.slice(botPrefix.length).split(/ +/);
const cmd = arguments.shift().toLowerCase();
if (!msg.content.startsWith(botPrefix) || msg.author.bot) return;
if (cmd === 'purge') {
client.commands.get('purge').execute(msg, arguments);
}
});
Purge command file:
module.exports = {
name: 'purge',
description: 'Delete multiple messages',
async execute(msg, arguments) {
if (!arguments[0]) return msg.reply('Please specify the number of messages to delete');
if (isNaN(arguments[0])) return msg.reply('Please enter a valid number');
if (arguments[0] > 100) return msg.reply('Maximum 100 messages can be deleted at once');
if (arguments[0] < 1) return msg.reply('Please delete at least 1 message');
await msg.channel.messages.fetch({ limit: arguments[0] }).then(msgs => {
msg.channel.bulkDelete(msgs);
})
}
}
The error occurs when I try to use the purge command. What am I doing wrong here?