自动清除在 Discord 中发布的最后 10 张图片 (discord.js)

Automatically purge last 10 images posted in Discord (discord.js)

我正尝试在 discord 中为我的机器人创建一个系统,该系统将清除聊天中发布和上传(链接和上传)的最后 10 张图片,有人知道我该如何编写代码吗?这是我的想法(有点)

if (message.channel.type == 'general') {
    if (*the last 10 messages that have been sent are images*) {
         message.delete(); // delete last 10 images
    }
}

您可以使用 fetchMessages method to retrieve a promise that consists of a collection of the the last 10 messages, by setting the limit to 10 using the ChannelLogsQueryOptions.

使用此消息集合,正如@Chris Satchell 在评论中提到的那样,您循环遍历它并检查所有消息是否存在 message.attatchments。或者,您可以检查整个消息集合的 attachments<Collection>.size 是否等于 10,如果是,您可以继续下一步。

现在您有了要删除的邮件集合,只需将此集合传递到方法 bulkDeletemessages 参数即可。

因此,您在 TextChannel 中获取最后 10 条消息,然后检查获取的消息集合的 attachments 属性,然后调用 bulkDelete 这个集合的方法。