首页 > 解决方案 > 如果允许某个变量,有没有办法覆盖“列入黑名单”的单词?

问题描述

            const BannedLinks = ["https://", "http://", "www.", ".com", ".net", ".org", ".tv", ".xyz", ".blog"]
            if(BannedLinks.some(word => message.content.includes(word)) ) {
            message.delete();
            message.reply("please dont post links, take that to DM's").then(m => m.delete(3000));
            }

这是我的 Discord 机器人的反链接代码,但我想允许 Twitch.tv 和 cdn.discordapp.com 之类的链接,但我正在努力寻找正确的功能。

标签: discord.js

解决方案


你可以使用array.prototype.some方法。

不和谐的解决方案

client.on('message', message => {
    const whiteListLinks = ['Twitch', 'Youtube']
    const blackListLinks = ['https', 'http']
  
    let isBlacklisted = blackListLinks.some(checkInclude) && !whiteListLinks.some(checkInclude)
    if (isBlacklisted) message.delete()

    function checkInclude(element, index, array) {
      return message.content.toUpperCase().includes(element.toUpperCase());
    }
});

在线片段

  const message = {
       content: 'https:Twitch'
  }
    const whiteListLinks = ['Twitch', 'Youtube']
    const blackListLinks = ['https', 'http']
  
  let isBlacklisted = blackListLinks.some(checkInclude) && !whiteListLinks.some(checkInclude)
  console.log(isBlacklisted)

function checkInclude(element, index, array) {
  return message.content.toUpperCase().includes(element.toUpperCase());
}


推荐阅读