【发布时间】:2021-01-24 13:06:31
【问题描述】:
我用 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();
});
【问题讨论】:
-
我之前没有使用过不和谐,但我认为你没有将任何东西传递给 find()。他抱怨味精未定义。
-
那你觉得我应该怎么做?
-
您只需要传递
msg参数。将find()更改为find(newMessage)。另外,为什么你有这么多String.prototype.replace()函数?据我所知,你不需要它们中的任何一个, -
尝试 client.on('messageUpdate',(oldMessage,newMessage) => { find(oldMessage) });
-
@NomairGhanem 我试过了,这次它说“msg is undefined”参考 msg.delete()
标签: javascript node.js discord discord.js