Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

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

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
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...