用户在Discord.js中批量删除消息

时间:2020-05-09 03:49:47

标签: javascript node.js discord.js

我想删除特定用户发布的所有消息。到目前为止,我有:

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

将botMessages传递给bulkDelete()似乎存在问题,它需要一个数组或集合,但显然botMessages不是数组或集合。

我该如何将botMessages赋予bulkDelete,还是我要解决这个完全错误的问题?

1 个答案:

答案 0 :(得分:0)

message.channel.fetch()获取消息发送到的通道,而不是该通道中的消息。

您需要提取一定数量的消息并对其进行过滤,以便仅接收由您的漫游器发送的消息,然后将其传递到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
        }))
    });
})