自动清除Discord(discord.js)中发布的最后10张图像 [英] Automatically purge last 10 images posted in Discord (discord.js)

查看:172
本文介绍了自动清除Discord(discord.js)中发布的最后10张图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a system for my bot in discord that'll purge the last 10 images posted & uploaded (links & uploads) in the chat, does anyone know how I can go about coding this? This was my idea (sort of)

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


推荐答案

您可以使用 fetchMessages 方法来检索承诺,它由最后10个消息的集合,通过使用limit 设置为10 nofollow noreferrer> ChannelLogsQueryOptions

You can use the 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 存在于所有消息中。或者,您可以检查整个邮件集合的附件< Collection> .size 是否等于10,如果可以,则可以继续下一步。

Using this collection of messages, as @Chris Satchell mentioned in the comments you loop through it and check if message.attatchments is present for all of messages. Alternatively you can check if the <Collection>.size of the attachments for the entire collection of messages is equivalent to 10, and if it does you can go ahead with the next step.

现在,您已经拥有要删除的邮件集合,只需将此集合传递到方法messages 参数rel = nofollow noreferrer> bulkDelete

Now that you have a collection of messages that you would like to delete, simply pass this collection into the messages parameter of the method bulkDelete.

因此,您获取了最后一个 TextChannel 中的10条消息,然后检查<$获取的邮件集合的c $ c> attachments 属性,然后在此集合上调用 bulkDelete 方法。

So as a result, you fetch the last 10 messages in a TextChannel, and then check the attachments property of the fetched collection of messages and then call the bulkDelete method on this collection.

这篇关于自动清除Discord(discord.js)中发布的最后10张图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆