An Awesome Clear Command For Your Discord.JS Bot
Gilles Heinesch
Posted on April 4, 2021
What is our goal with this new command?
My goal with this post is, to show you how to program a well structured clear command. At the end it is able to clear a specific amount of messages posted in a Discord textchannel.
What will the command look like when we are done?
?clear {amount of messages that should be deleted}
Example: ?clear 50
Let’s start with programming
First of all, we start with the basic setup of our new command.
const args = message.content.split(' ').slice(1); // All arguments behind the command name with the prefix
With this line of code we get all the content behind the prefix with the commandname. In this case, everything behind ?clear
.
Example: If you enter the command in a Discord textchannel ?clear 50
, args will be [ 50 ]
(which is an array).
const args = message.content.split(' ').slice(1); // All arguments behind the command name with the prefix
const amount = args.join(' '); // Amount of messages which should be deleted
Now, we’ve added a new line which just makes it simpler to read the amount of messages which should be deleted. For this we simply need to join()
the array. If you don’t know the function of join()
, you can read more here.
const args = message.content.split(' ').slice(1); // All arguments behind the command name with the prefix
const amount = args.join(' '); // Amount of messages which should be deleted
if (!amount) return msg.reply('You haven\'t given an amount of messages which should be deleted!'); // Checks if the `amount` parameter is given
if (isNaN(amount)) return msg.reply('The amount parameter isn`t a number!'); // Checks if the `amount` parameter is a number. If not, the command throws an error
2 new lines. The first one checks if the amount parameter is given. If not, the bot (command) throws an error that this parameter is needed to execute this command.
The next one checks if the amount parameter is even a number because an amount can only be an integer (a number) and can’t include letters.
const args = message.content.split(' ').slice(1); // All arguments behind the command name with the prefix
const amount = args.join(' '); // Amount of messages which should be deleted
if (!amount) return msg.reply('You haven\'t given an amount of messages which should be deleted!'); // Checks if the `amount` parameter is given
if (isNaN(amount)) return msg.reply('The amount parameter isn`t a number!'); // Checks if the `amount` parameter is a number. If not, the command throws an error
if (amount > 100) return msg.reply('You can`t delete more than 100 messages at once!'); // Checks if the `amount` integer is bigger than 100
if (amount < 1) return msg.reply('You have to delete at least 1 message!'); // Checks if the `amount` integer is smaller than 1
Again 2 new lines. These lines should not be complicated at all. The first one checks if the amount integer is bigger than 100, if yes the bot throws an error. The same for the next line, only that it checks if the integer (amount parameter) is smaller than 1. If you don’t know why the maximum can only be 100; this is due to the Discord API, it only allows a bot application to fetch 100 messages at once. More can be found here.
const args = message.content.split(' ').slice(1); // All arguments behind the command name with the prefix
const amount = args.join(' '); // Amount of messages which should be deleted
if (!amount) return msg.reply('You haven\'t given an amount of messages which should be deleted!'); // Checks if the `amount` parameter is given
if (isNaN(amount)) return msg.reply('The amount parameter isn`t a number!'); // Checks if the `amount` parameter is a number. If not, the command throws an error
if (amount > 100) return msg.reply('You can`t delete more than 100 messages at once!'); // Checks if the `amount` integer is bigger than 100
if (amount < 1) return msg.reply('You have to delete at least 1 message!'); // Checks if the `amount` integer is smaller than 1
await msg.channel.messages.fetch({ limit: amount }).then(messages => { // Fetches the messages
msg.channel.bulkDelete(messages // Bulk deletes all messages that have been fetched and are not older than 14 days (due to the Discord API)
)});
The first new line fetches all messages. The limit is the above given amount parameter (To revise: This is the number behind the ?clear
command). Then we continue to the main function of this command; Bulkdelete. The bot now bulkdeletes all messages that have been fetched.
Be careful! The messages can not be older than 14 days (again due to the Discord API).
Conclusion
I hope I could help you a little bit with your bot and the setup of your clear command. If you have any questions, just comment below!
Posted on April 4, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.