【问题标题】:How do you make a Discord Bot not send a message when certain words are said (Node.js)当说出某些单词时,如何让 Discord Bot 不发送消息(Node.js)
【发布时间】:2020-12-24 05:50:30
【问题描述】:

我正在尝试创建一个 Discord Bot,它会在说出所选单词后发布特定图像。到目前为止,一切都很好,但我遇到的问题是我需要列出一个单词列表,如果其中任何一个单词被声明,它将不会发送消息。但是我在实现这个功能时遇到了麻烦,而且我似乎找不到任何答案。这是我第一次使用 JavaScript 并制作 Discord Bot,所以这并不是我的强项(我使用了一个指南来设置其中的一些内容)。所以如果有人知道如何解决这个问题,请告诉我。谢谢!

const Discord = require('discord.js');
const config = require('./config.json');

const client = new Discord.Client();

const prefix = '!';

client.on('message', function(message) {
 if (message.author.bot) return;
 if (message.content.match('Temple') || message.content.match('temple')) {
  message.channel.send(
   'https://cdn.discordapp.com/attachments/278000731125186560/750883416744001606/A9a0P768eBgtgAAAABJRU5ErkJggg_1.png'
  );
 }

 const commandBody = message.content.slice(prefix.length);
 const args = commandBody.split(' ');
 const command = args.shift().toLowerCase();
});

client.login(config.BOT_TOKEN);

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    试试这个:

    let triggers = ['word', 'word2', 'word3'];
    client.on('message', (message) => {
     let isContain = triggers.some(checkInclude);
     if (isContain) message.channel.send('YES');
    
     function checkInclude(element, index, array) {
      return message.content.toUpperCase().includes(element.toUpperCase());
     }
    });
    

    【讨论】:

      【解决方案2】:

      创建一个函数来检查content 是否包含禁用词。在发送消息之前使用该函数检查message.content

      const isIncludeBanWords = (content) => {
        const bannedWords = ["aaa", "bbb", "ccc"];
        for (const bannedWord of bannedWords) {
          if (content.indexOf(bannedWord) >= 0) {
            return true;
          }
        }
        return false;
      }
      

      如果content 至少包含一个banned word,该函数将返回true

      最后一个例子:

      const config = require("./config.json");
      
      const client = new Discord.Client();
      
      const prefix = "!";
      
      const isIncludeBanWords = (content) => {
        const bannedWords = ["aaa", "bbb", "ccc"];
        for (const bannedWord of bannedWords) {
          if (content.indexOf(bannedWord) >= 0) {
            return true;
          }
        }
        return false;
      }
      
      client.on("message", function (message) {
        if (message.author.bot) return;
        const isMatchKeyword = message.content.match("Temple") || message.content.match("temple");
        if (isMatchKeyword && !isIncludeBanWords(message.content)) { // match keyword and NOT include banned word
          message.channel.send("https://cdn.discordapp.com/attachments/278000731125186560/750883416744001606/A9a0P768eBgtgAAAABJRU5ErkJggg_1.png")
        }
      
        const commandBody = message.content.slice(prefix.length);
        const args = commandBody.split(' ');
        const command = args.shift().toLowerCase();
      });
      
      client.login(config.BOT_TOKEN);
      

      【讨论】:

      • 我实现了您在我的代码中发布的功能并插入了禁用词,但是在测试机器人时,无论是否声明了禁用词,它似乎都会发送图像。
      • 没关系,我让它工作了!显然我的禁用词没有保存。感谢您的帮助!
      猜你喜欢
      • 2018-07-28
      • 2021-09-17
      • 2020-07-31
      • 2018-05-03
      • 1970-01-01
      • 2020-12-01
      • 2021-03-02
      • 2021-01-12
      • 2021-06-05
      相关资源
      最近更新 更多