Deleting all messages in discord.js text channel

后端 未结 7 1579
梦如初夏
梦如初夏 2021-02-09 18:09

Ok, so I searched for a while, but I couldn\'t find any information on how to delete all messages in a discord channel. And by all messages I mean every single message ever writ

7条回答
  •  爱一瞬间的悲伤
    2021-02-09 19:05

    Discord does not allow bots to delete more than 100 messages, so you can't delete every message in a channel. You can delete less then 100 messages, using BulkDelete.

    Example:

    const Discord = require("discord.js");
    const client = new Discord.Client();
    const prefix = "!";
    
    client.on("ready" () => {
        console.log("Successfully logged into client.");
    });
    
    client.on("message", msg => {
        if (msg.content.toLowerCase().startsWith(prefix + "clearchat")) {
            async function clear() {
                msg.delete();
                const fetched = await msg.channel.fetchMessages({limit: 99});
                msg.channel.bulkDelete(fetched);
            }
            clear();
        }
    });
    
    client.login("BOT_TOKEN");
    

    Note, it has to be in a async function for the await to work.

提交回复
热议问题