Reputation: 111
I am creating a Discord bot with discord.js and I'd like to create a command that can clear messages. For now, I have this code (only the interesting part) and I can't figure out why it doesn't work:
// Importing discord.js, creating bot and setting the prefix
const Discord = require('discord.js');
const bot = new Discord.Client();
const prefix = "/";
// Array that stores all messages sent
messages = [];
bot.on('message', (message) => {
// Store the new message in the messages array
messages.push(message);
// Split the command so that "/clear all" becames args["clear", "all"]
var args = message.content.substring(prefix.length).split(" ");
// If the command is "/clear all"
if(args[0] == "clear" && args[1] == "all") {
bot.deleteMessages(messages); // Code that doesn't work
// Resets the array
messages = [];
}
}
// CONNECT !!!
bot.login('LOGING TOKEN HERE');
Can you help me ?
Upvotes: 0
Views: 5098
Reputation: 1
Its not like that. you should fetch the messages first then use bulkDelete
to delete them
, here is a simple example
// Normal Javascript
<Message>.channel.fetchMessages()
.then(messages => {
// Here you can use bulkDelete(101) to delete 100 messages instead of using fetchMessages and deleting only 50
<Message>.channel.bulkDelete(messages);
});
// ES6
let messages = await <Message>.channel.fetchMessages();
// Here you can use bulkDelete(101) to delete 100 messages instead of using fetchMessages and deleting only 50
await <Message>.channel.bulkDelete(messages);
Upvotes: 0
Reputation: 376
You should use <TextChannel>.bulkDelete
instead.
Example:
msg.channel.bulkDelete(100).then(() => {
msg.channel.send("Purged 100 messages.").then(m => m.delete(3000));
});
This would delete 2 - 100
messages in a channel for every call to this method so you would not receive 429 (Too many Requests) Error
frequently which might result in your token being revoked.
Upvotes: 2
Reputation: 1284
You can swap
bot.deleteMessages()
to:
messages.forEach(x => x.delete())
Upvotes: 0
Reputation: 287
Another way to do this, without sweepMessages
is by using fetchMessages
:
let user = message.mentions.users.first();
let amount = !!parseInt(message.content.split(' ')[1]) ? parseInt(message.content.split(' ')[1]) : parseInt(message.content.split(' ')[2])
var prefix = '!'
if (message.content.startsWith(prefix + 'clear') && !amount)
return message.reply('Must specify an amount to clear!');
if (message.content.startsWith(prefix + 'clear') && !amount && !user) return message.reply('Must specify a user and amount, or just an amount, of messages to clear!');
message.channel.fetchMessages({
limit: amount,
}).then((messages) => {
if (user) {
const filterBy = user ? user.id : bot.user.id;
messages = messages.filter(m => m.author.id === filterBy).array().slice(0, amount);
}
message.channel.bulkDelete(messages).catch(error => console.log(error.stack));
});
This will allow users to use the command !clear [#]
to delete that number of messages when sent. If it is run as just !clear
you can set how many get deleted, without a specified number.
discord.js Documentation - TextChannel#fetchMessages
Upvotes: 0
Reputation: 5307
I see two problems:
messages
array is always empty; there is no code that adds items to the array, so the call to bot.deleteMessages
will always get an empty array;deleteMessages
is an available method on Discord.Client
;Based on the documentation, I think what you want is sweepMessages
. The description of that states:
Sweeps all text-based channels' messages and removes the ones older than the max message lifetime. If the message has been edited, the time of the edit is used rather than the time of the original message.
Try changing the code to instead call bot.sweepMessages(1);
, which I think will tell the client to clear all messages older than one second.
Upvotes: 0