简体   繁体   English

当说某些话时,你如何让 Discord Bot 不发送消息(Node.js)

[英]How do you make a Discord Bot not send a message when certain words are said (Node.js)

I'm trying to create a Discord Bot that posts a specific image once a chosen word is said.我正在尝试创建一个 Discord Bot,它会在说出所选单词后发布特定图像。 So far that all works great, but what I'm having trouble with is that I need to make a list of words from which it will not send a message if any of those words are stated.到目前为止,一切都很好,但我遇到的问题是我需要制作一个单词列表,如果其中任何一个单词被陈述,它就不会发送消息。 However I'm having trouble implementing this feature, and I can't seem to find any answers.但是,我在实现此功能时遇到了麻烦,而且我似乎找不到任何答案。 This is my first time using JavaScript and making a Discord Bot, so it's not exactly my forte (I used a guide to set some of this up).这是我第一次使用 JavaScript 并制作 Discord Bot,所以这不是我的强项(我使用指南来设置其中的一些)。 So if anyone knows how to fix this, please let me know.所以如果有人知道如何解决这个问题,请告诉我。 Thanks!谢谢!

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

Try this:尝试这个:

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

Create a function to check if content is include banned words or not.创建一个函数来检查content是否包含禁用词。 Use that function to check message.content before send the message.使用该函数在发送message.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;
}

The function will return true if content includes at least one banned word .如果content包含至少一个banned word该函数将返回true

Final example:最后一个例子:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 你如何让 discord 机器人重新发送你刚刚发送的图像 discord.js - How do you make a discord bot resend a image you just send discord.js 如何让 Discord 机器人将文本发送到 Node.js 中的特定频道? - How to make a Discord bot send text to a specific channel in Node.js? node.js - 有没有办法让 discord 机器人发送从文件中提取的随机图片? node.js - node.js - Is there a way to make a discord bot send random pictures pulled from a file? node.js discord bot 使用 node.js - 如何将消息转换为小写? - discord bot using node.js - how do i convert a message to lowercase? 如何通过命令关闭 discord 机器人? (使用 Javascript、Node.js 和 Discord.js) - How can you make a command shut down a discord bot? (Using Javascript, Node.js and Discord.js) 如何让我的 discord 机器人在特定时间或日期发送消息 - How do I make my discord bot to send a message on a certain time or date 您如何向对机器人发送的消息做出反应的用户发送 DM? (不和谐.JS) - How do you send a DM to users that react to a bot sent message? (Discord.JS) 如何让不和谐机器人回复您所说的内容? - How to make a discord bot reply what you have said? 当您给 Discord 机器人发送表情符号名称时,如何让它发送表情符号? - How do you make a Discord bot send an emoji when you give it the name of the emoji? 如何让 discord 机器人在加入时发送消息 - How do I make a discord bot send a message upon joining
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM