admin管理员组

文章数量:1417054

I want to delete all the messages posted by a particular user. So far I have:

async function clear() {
    let botMessages;
    botMessages = await message.channel.fetch(708292930925756447);
    message.channel.bulkDelete(botMessages).then(() => {
        message.channel.send("Cleared bot messages").then(msg => msg.delete({timeout: 3000}))
    });
}
clear();

There seems to be an issue with passing botMessages to bulkDelete(), it wants an array or collection but apparantly botMessages isn't an array or collection.

How would I give botMessages to bulkDelete, or am I going about this totally wrong?

I want to delete all the messages posted by a particular user. So far I have:

async function clear() {
    let botMessages;
    botMessages = await message.channel.fetch(708292930925756447);
    message.channel.bulkDelete(botMessages).then(() => {
        message.channel.send("Cleared bot messages").then(msg => msg.delete({timeout: 3000}))
    });
}
clear();

There seems to be an issue with passing botMessages to bulkDelete(), it wants an array or collection but apparantly botMessages isn't an array or collection.

How would I give botMessages to bulkDelete, or am I going about this totally wrong?

Share Improve this question asked May 9, 2020 at 3:49 JohnJohn 591 silver badge6 bronze badges 1
  • If an answer solves your question then accept the answer to let others know it worked – Syntle Commented May 9, 2020 at 4:59
Add a ment  | 

1 Answer 1

Reset to default 5

message.channel.fetch() fetches the channel the message is sent to, not the messages in that channel.

You need to fetch a certain amount of messages and filter it so you're only getting messages sent by your bot then pass them to bulkDelete()

message.channel.messages.fetch({
    limit: 100 // Change `100` to however many messages you want to fetch
}).then((messages) => { 
    const botMessages = [];
    messages.filter(m => m.author.id === BOT_ID_HERE).forEach(msg => botMessages.push(msg))
    message.channel.bulkDelete(botMessages).then(() => {
        message.channel.send("Cleared bot messages").then(msg => msg.delete({
            timeout: 3000
        }))
    });
})

本文标签: javascriptBulk delete messages by user in DiscordjsStack Overflow