Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 495
Triad Avatar asked Oct 14 '18 13:10

Triad


People also ask

How do I see message links in Discord?

To get the Message Link, click on the 3 dots to the far right of the message. You will see an option to Copy Link.

Can you put links in Discord chat?

Scroll down to the Embeds section and click the + icon to create your hyperlink. In the Embeds description text area write your message in this format, prefix [ text ] ( link ) just like we did with the Carl Bot description.

What is Discord message link?

Basically a link that takes the person directly to the direct messages with a certain user, similar to a server invitation link.


2 Answers

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**'))
  }
})

like image 95
Tobyhn Avatar answered Oct 07 '22 22:10

Tobyhn


I find this is the best:

let regx = /^((?:https?:)?\/\/)?((?:www|m)\.)? ((?:discord\.gg|discordapp\.com))/g
let cdu = regx.test(message.content.toLowerCase().replace(/\s+/g, ''))

Tell me if it works!

like image 1
Dan5312 Dan5312 Avatar answered Oct 07 '22 23:10

Dan5312 Dan5312