说出某些单词时如何使Discord Bot不发送消息(Node.js)

时间:2020-09-05 02:33:07

标签: javascript node.js discord discord.js

我正在尝试创建一个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);

2 个答案:

答案 0 :(得分:2)

尝试一下:

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

答案 1 :(得分:-1)

创建一个函数来检查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;
}

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

最终示例:

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