Skip to content

Clear all messages that are not images or links by using discord.js and Node.js [closed]

I have a bot in discord in JavaScript and I need to start a timer in 24 hours and clear all messages in a certain channel if they are not pictures or links, since I do not know how timers work here and how to distinguish messages with text from messages with pictures, please help (please do not write a ready-made script, but at least explain how it all works 🙂 )

Answer

To check if a message contains an image you can check the Collection <Message>.attachments.first(), this will return falsey if no image was attached.

const image = <Message>.attachments.first();
if (!image) <Message>.delete();

One way to check for a link is to see if <Message>.content starts with 'http'

const hasLink = <Message>.content.startsWith('http');
if (!hasLink) <Message>.delete();

However it’s pretty easy to bypass this. You may want to check out Regular Expressions (Regex)

<Message> is a placeholder for your message object.