'Discord.js deleteCommand
I've been trying to write a simple bot for Discord, and I thought of adding a deleteCommand line which will delete the command given from the user and return the answer that I have set to the bot.
Let's say I have this command which is the ping-pong command:
exports.run = function(Aika, message, args) {
message.channel.sendMessage('pong! :P\'${Date.now() - message.createdTimestamp} ms\'');
};
exports.conf = {
enabled: true,
guildOnly: false,
aliases: [],
permLevel: 0
};
exports.help = {
name: 'ping',
description: 'Responds with "pong" and gives current ms rate.',
usage: 'ping'
};
and I send "!ping" in the Discord chat, I want this "!ping" to get deleted and then the bot answer "pong!", I couldn't make it delete the command.
Solution 1:[1]
I actually figured out how to do this. You just need to run the code BEFORE any other code (immediately after the message is received):
message.delete(1000);
Here's it in action on a master (admin) command I have:
client.on('message', message => {
if ((message.author.id === '200659103318540288') && (message.content === '~m help', '~m', '~mhelp')) {
message.delete(1000);
message.reply('I have sent you a list of master commands.')
message.author.sendMessage("Here is the list of master commands.")
console.log('Successfully sent a list of master commands to ' + message.author.id + '.')
}
});
//1000 is the timeout in ms. I recommend don't change it unless you know the effects.
Solution 2:[2]
You can use message.delete() to delete the referenced message. If you put this in your message event, which provides a message object, you can delete the message.
(I have the message event and all my commands in separate files from my main bot file, so your event function may look different)
module.exports = (client, message, args) => {
message.delete(1000);
// The rest of my command here
}
If you have everything stored in a single file, your command may look like this:
client.on('message' (message) => {
if (message.content == '$help') {
message.delete(1000);
// The rest of my command here
}
});
Also note that you should not change the 1000 unless you know what you are doing. This is the time it will wait (ms) before it will delete the message. Again, it is recommended that you do not change this, unless you both know what you are doing, and know what might happen.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kith M. |
| Solution 2 | aer |
