我用于我的 discord 机器人的代码根据单词列表删除特定消息会删除所有内容。我做错了什么?



以下是代码:

const bannedwords = ["example word", "another example"]
client.on("message", message => {
if (message.content === bannedwords);
message.delete(message);
message.author.send("That was inappropriate!")

抛出的错误只是一个API说不能向该用户发送消息,没有其他错误。

从粘贴的内容来看,代码的格式有点不稳定。我添加了一个函数,它将检查您的列表中是否有任何子字符串在message.content中,然后如果检测到它,它将删除并向作者发送消息。

const bannedwords = ["example word", "another example"]
client.on("message", message => {
// Checks if any banned words in the list are in message.content
if (bannedwords.some((word) => message.content.includes(word))) {
message.delete(message);
message.author.send("That was inappropriate!");
}
});

我修改了代码以修复被禁止的单词检查,添加了bot检查,并用大括号替换了if语句后的分号:

const bannedwords = ["example word", "another example"];
client.on("message", message => {
if (message.author.bot) return;
if (new RegExp(bannedwords.join("|")).test(message.content)) {
message.delete();
message.author.send("That was inappropriate!");
}
});

你收到的说你不能DM用户的错误可能是由机器人试图DM自己引起的,你不能DM机器人。

相关内容

最新更新