我用discord.js制作了一个机器人,以检测何时blacklisted
数组的任何元素被说出。它删除该消息并转发警告。我的朋友们尝试变通办法,并尝试编辑该消息以说一个不好的词,(它不会读)。因此,我问一些朋友,他们建议将检测功能放入函数中,并在我使用client.on时检测消息更新。目前,无论何时运行,我都会收到错误消息。
这是相关代码:
function find(msg) {
var blacklisted = [
'words',
'that',
'I',
'cant',
'say',
'on',
'this',
'website',
];
let foundInText = false;
for (var i in blacklisted) {
if (
msg.content
.replace(/\s/g, '')
.replace(/[^A-Za-z0-9]/g, '')
.toLowerCase()
.includes(blacklisted[i].toLowerCase())
)
foundInText = true;
}
if (foundInText) {
msg.delete();
msg.reply('Watch your language, dont be a bad boy');
}
}
client.on('messageUpdate', (oldMessage, newMessage) => {
find();
});
答案 0 :(得分:0)
正如注释中指出的那样,您需要将消息对象传递给函数。此外,无论您是否blacklisted
中需要的单词都在里面,您检查单词的方法并不能真正起作用,并且使机器人仅删除消息。
尝试一下
function find(msg) {
var blacklisted = [
'words',
'that',
'I',
'cant',
'say',
'on',
'this',
'website',
];
let foundInText = false;
// Convert the content of the message into an array
// Check if blacklisted contains the element
msg.content.toLowerCase().split(/ +/g).forEach((element) => {
if (blacklisted.includes(element)) {
return (foundInText = true);
}
});
if (foundInText) {
msg.delete();
msg.reply('Watch your language, dont be a bad boy');
}
}
client.on('messageUpdate', (oldMessage, newMessage) => {
// Return here so the bot doesn't react to it's own messages
if (message.author.bot) return;
// Pass the newMessage because we need the message after
// it has been modified
find(newMessage);
});
client.on('message', (message) => {
// Return here so the bot doesn't react to it's own messages
if (message.author.bot) return;
// Pass the message
find(message);
// The rest of your code
});