Deleting all messages in discord.js text channel

后端 未结 7 1578
梦如初夏
梦如初夏 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 18:43

    Another approach could be cloning the channel and deleting the one with the messages you want deleted:

    // Clears all messages from a channel by cloning channel and deleting old channel
    async function clearAllMessagesByCloning(channel) {
        // Clone channel
        const newChannel = await channel.clone()
        console.log(newChannel.id) // Do with this new channel ID what you want
    
        // Delete old channel
        channel.delete()
    }
    

    I prefer this method rather than the ones listed on this thread because it most likely takes less time to process and (I'm guessing) puts the Discord API under less stress. Also, channel.bulkDelete() is only able to delete messages that are newer than two weeks, which means you won't be able to delete every channel message in case your channel has messages that are older than two weeks.

    The possible downside is the channel changing id. In case you rely on storing ids in a database or such, don't forget to update those documents with the id of the newly cloned channel!

提交回复
热议问题