简体   繁体   中英

Block messages in certain channels discord.js

I want to block certain messages in certain channels but with the code I have right now, the words are blocked in every channel on the Discord. I only want to block this words in channels that are in the database. Is there a way to do this?

  if (message.content.includes('discord.gg/'||'discordapp.com/invite/'||'discord.me/'||'discord.com/')) 
    return blacklist.set(message.author.id, 'blacklisted');

Here is something you could do.

let forbiddenWords = ["discord.gg", "discordapp.com/invite"];
let forbiddenChannels = ["a channel id", "another one"];

for (var i = 0; i < forbiddenWords.length; i++) {
    if (message.content.includes(forbiddenWords[i])) {
        if (forbiddenChannels.includes(message.channel.id)) {
            return blacklist.set(message.author.id, 'blacklisted');
        }
    }
}

What you need to do is check if the channel the message came from is actually the channel where you want to block the words. You can do that with the channel.id parameter.

if (message.channel.id === "your id here") {
    // the rest of your code
}

EDIT:

If you want to block multiple channels you should use

let YourDatabaseForChannels = ["Your channel", "Your other channel"];

if (YourDatabaseForChannels.includes(message.channel.id)) {
    // the rest of your code
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM