Search code examples
javascriptdiscorddiscord.js

Detect links in messages in Discord.js


I'm new to discord.js. I'm trying to check if a message contains a link like "Hi, I'm from discord.gg/xxxxx and now I'll spam my link".
How can I check if the message contain the link?


Solution

  • I am unsure if you want to check for discord invite links specifically, or if you want to check for all links. Either way, you can use message.content.includes.

    Example:

    bot.on('message', (message) => { //whenever a message is sent
      if (message.content.includes('discord.gg/'||'discordapp.com/invite/')) { //if it contains an invite link
        message.delete() //delete the message
          .then(message.channel.send('Link Deleted:\n**Invite links are not permitted on this server**'))
      }
    })