[英]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.