javascript - Bulk delete messages by user in Discord.js - Stack Overflow

I want to delete all the messages posted by a particular user. So far I have:async function clear() {le

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
        }))
    });
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745262612a4619280.html

相关推荐

  • javascript - Bulk delete messages by user in Discord.js - Stack Overflow

    I want to delete all the messages posted by a particular user. So far I have:async function clear() {le

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信