检测 Discord.js 中邮件中的链接

Detect links in messages in Discord.js

我是 discord.js 的新手。我正在尝试检查邮件是否包含 link,例如 "Hi, I'm from discord.gg/xxxxx and now I'll spam my link"。
如何检查邮件是否包含 link?

您可以使用 Regular Expressions (RegEX)

进行检查

示例

// 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 }

你可以试试这个:

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);
    }
});

(少了一个")")

我不确定您是要专门检查 discord 邀请链接,还是要检查所有链接。无论哪种方式,您都可以使用 message.content.includes.

示例:

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

我发现这是最好的:

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

告诉我它是否有效!

您正在做的工作有效,但您不需要 .then() 只需让 message.channel.send() 保持原样即可。

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*)?$/;

我认为这对你有帮助。