簡體   English   中英

在 Discord.js 中檢測消息中的鏈接

[英]Detect links in messages in Discord.js

我是 discord.js 的新手。 我正在嘗試檢查消息是否包含“嗨,我來自 discord.gg/xxxxx,現在我將發送我的鏈接”之類的鏈接。
如何檢查消息是否包含鏈接?

我不確定您是要專門檢查不和諧邀請鏈接,還是要檢查所有鏈接。 無論哪種方式,您都可以使用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**'))
  }
})

你可以試試這個:

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

(缺少一個“)”)

你正在做的工作,但你不需要.then()只需保持message.channel.send()原樣。

您可以使用正則表達式(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 }

我覺得這是最好的:

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

告訴我它是否有效!

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

我認為這對你有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM