最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Detect links in messages in Discord.js - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Oct 14, 2018 at 19:51 Ken Kinder 13.1k6 gold badges53 silver badges72 bronze badges asked Oct 14, 2018 at 13:17 TriadTriad 1611 gold badge4 silver badges12 bronze badges 0
Add a comment  | 

6 Answers 6

Reset to default 8

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

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!

What you are doing works but you don't need .then() just leave the message.channel.send() as it is.

You can check this using Regular Expressions (RegEX)

Example:

// The message to check for a Discord link
var message = "Hi, please join discord.gg/a2dsc for cool conversations";

// The message will be tested on "discord.gg/{any character or digit}"
var containsDiscordUrl = message.test(/discord.gg\/\w*\d*);

// If the test has found a URL..
if (containsDiscordUrl) { // ... Do something }

You can try this:

bot.on(`message`, async message => {
    const bannedWords = [`discord.gg`, `.gg/`, `.gg /`, `. gg /`, `. gg/`, `discord .gg /`, `discord.gg /`, `discord .gg/`, `discord .gg`, `discord . gg`, `discord. gg`, `discord gg`, `discordgg`, `discord gg /`]
    try {
        if (bannedWords.some(word => message.content.toLowerCase().includes(word))) {
            if (message.author.id === message.guild.ownerID) return;
            await message.delete();
            await message.channel.send(`You cannot send invites to other Discord servers`);
        }
    } catch (e) {
        console.log(e);
    }
});

(There was a missing ")")

let regexp = /^(?:(?:https?|ftp):\/\/)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/;

I think this is helping you.

Edit:

(?:(?:https?|ftp):\/\/)? - http, https, or ftp

(?:(?! ... )(?! ... )(?! ... )) - These are negative lookup statements, used to ensure that the IP address does not fall into specific reserved ranges.

(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])| (?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2} | (?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4])) - check IPv4

(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+) - check domain name

(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)* - check sub domain

(?:\.(?:[a-z\u00a1-\uffff]{2,})) - check top-level domain

(?::\d{2,5})? - cehck port number

(?:\/\S*)?$ - check optional path

Use this regex for discord invite check

\b(?:https?://)?(?:www\.)?(?:discord\.gg|discord\.com/invite)/([a-zA-Z0-9-]+)\b
发布评论

评论列表(0)

  1. 暂无评论