1

I want to add a media only channel to my discord.js bot. I want people to be able to send only links and images. I've made the image part of the channel but when users send links my bot delete them. For example prnt.sc/xxxxx.

Here is my code :

bot.on("message", msg => {
    if (msg.channel.id !== "815002698499096616") { 
      return;
    }

    // Checking if the author is a bot.
    if (msg.author.bot) {
        return false;
    }

    // Deleting the message if there are 0 attachments in the message.
    if (msg.attachments.size == 0) {
        msg.delete();
    }
});
zhulien
  • 5,145
  • 3
  • 22
  • 36
omergencw
  • 39
  • 3
  • 1
    Does this answer your question? [What is the best regular expression to check if a string is a valid URL?](https://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url) – Lioness100 Mar 13 '21 at 13:15
  • No.. i want to make photos and links channel – omergencw Mar 13 '21 at 13:34
  • And by the looks of it you've already figured out how to check if the message has an attachment, so the next step would be to use a regex to figure out if the message contains a link. – Lioness100 Mar 13 '21 at 14:10

2 Answers2

1

You can do this with regex like this:

var isLink = /^http/g
console.log(isLink.test("https://example.com")) // returns true

if(msg.attachments.size == 0 || !isLink.test(text content of your message)) {
    msg.delete();
}
Kenji Bailly
  • 107
  • 9
1

Thank you for all helpers <3 I wrote a new code and it's works :) Here is my code:

 bot.on("message", msg => {
    if (msg.channel.id !== "804813461309620316") { 
      return;
    }
    // Checking if the author is a bot.
    if (msg.author.bot) return false;

    if (msg.content.includes('.png')) return false;

    if (msg.content.includes('.jpg')) return false;

    if (msg.content.includes('.jpeg')) return false;

    if (msg.content.includes('prnt.sc')) return false;
   
    // Deleting the message if there are 0 attachments in the message.
    if (msg.attachments.size == 0) msg.delete()
});
omergencw
  • 39
  • 3