FeelingMellow
FeelingMellow

Reputation: 11

Is there a way to override "blacklisted" words if there is a certain variable that's allowed?

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

This is my anti-link code for my Discord bot, but I want to allow links like Twitch.tv and cdn.discordapp.com, but I'm struggling to find right functions.

Upvotes: 0

Views: 59

Answers (1)

Cipher
Cipher

Reputation: 2722

You can use array.prototype.some method.

Discord solution

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

Online snippet

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

Upvotes: 1

Related Questions