Reputation: 51
I'm currently trying to make a "Clear" command for my discord bot, (!clear number) however I'm obviously doing something wrong; I'm pretty new to coding and I hope someone can see where I've gone wrong.
In my main.js file, I have, amongst all the others, this:
} else if(command == 'clear'){
client.commands.get('clear').execute(message, agrs);
}
and in my clear.js file, I have this code which is probably completely wrong.
module.exports= {
name: 'clear',
description: 'clears an amount of messages.',
execute(message, args){
const args = message.content.split(' ').slice(1);
const amount = args.join(' ');
if (!amount) return msg.reply('You haven\'t given an amount of messages which should be deleted!');
if (isNaN(amount)) return msg.reply('The amount parameter isn`t a number!');
if (amount > 100) return msg.reply('You can`t delete more than 100 messages at once!');
if (amount < 1) return msg.reply('You have to delete at least 1 message!');
await msg.channel.messages.fetch({ limit: amount }).then(messages => {
msg.channel.bulkDelete(messages
)});
}
}
any help would be greatly appreciated. Thank you again!
Upvotes: 0
Views: 300
Reputation: 8402
You should use this instead:
// delete `const args = messsage.content...`, args is already defined via command handler
const amount = args[0]
// code...
const fetched = await msg.channel.messages.fetch({
limit: amount,
});
msg.channel
.bulkDelete(fetched)
.catch((error) => console.log(`Couldn't clear messages because of ${error}`)
Upvotes: 1