Discord.js-如何删除漫游器发送的所有消息?

时间:2020-07-06 05:36:12

标签: javascript node.js discord discord.js

我只希望我的机器人只有一条消息,因此,每当它发送一条消息时,其上一条消息就会被删除。我想做的是遍历所有消息,看看ID是否与漫游器匹配。这是命令:

if(message.content.startsWith(`${PREFIX}update`)){
    const channel = message.channel
    const messages = await channel.messages.fetch({ limit: 100 });
    messages.forEach(msg => **IF MESSAGE IS SENT BY BOT, DELETE IT**); 
    message.channel.send("update");
}

如何查看邮件是否由漫游器发送并删除?我只看到人们以前删除过一定数量的消息。

1 个答案:

答案 0 :(得分:1)

User具有一个名为bot的属性,该属性为true / false取决于该帐户是否是漫游器。您可以使用它来检查您要删除的邮件是否是由漫游器发送的。

此外,您可以使用messageauthorid来检查作者的ID。 (以防万一您想通过某个漫游器删除邮件。)

示例:

    if (message.content.startsWith(`${PREFIX}update`)) {
        const Channel = message.channel;
        const Messages = await Channel.messages.fetch({limit: 100});

        Messages.forEach(msg => { // Checking if the message author has a certain ID.
            if (msg.author.id == "ID") msg.delete()
        });

        message.channel.send("Updated");
    };
});
    if (message.content.startsWith(`${PREFIX}update`)) {
        const Channel = message.channel;
        const Messages = await Channel.messages.fetch({limit: 100});

        Messages.forEach(msg => { // Checking if the message author is a bot.
            if (msg.author.bot) msg.delete()
            // This will delete messages from any bot.
        });

        message.channel.send("Updated");
    };

按ID删除邮件

const Channel = client.channels.cache.get("ChannelID");
Channel.messages.fetch("MessageID").then(message => message.delete())