我有一个命令 im 测试,当特定的字符串被传递到通道时,它使 Bot 能够向我发送消息。
我的命令工作正常,但机器人似乎也在内容中包含用户名。
我通过使用我的名字作为机器人运行 if 语句的字符串进行测试发现了这一点,并发现即使只输入 Hi 或任何其他消息也会触发 if 语句,因为我的名字“Jerseyetr”包含在内容中消息。
if (message.content.includes('jersey') || ('jerseyetr')) {
try{
console.log('it worked!');
//jersey.send('testing!');
//client.users.get("xxxxxx").send("test");
return;
//guild.member
}
catch (err){
console.log(err);
console.log('it did NOT work. Sad face');
}
}
过滤掉用户名的正确方法是什么?
这里的问题是你总是在 if 中输入,而从不在 else 中(如果有的话)。
为什么?因为你的条件。OR 条件||
将首先测试message.content.includes('jersey')
. 如果是,true
它会进入 if,如果它是假的,它会测试第二个条件。但是您的第二个条件始终为真,因为该字符串'jerseyetr'
不为空并且被 Javascritps 视为真。
此页面列出了所有被认为是虚假的东西。所有其他值都被考虑在内true
。
我猜你想这样做:
if (message.content.includes('jersey') || message.content.includes('jerseyetr')) {
...
}
但是,不需要第二个表达式,因为jersey
包含在 中jerseyetr
,因此如果消息包含jerseyetr
,它也将包含jersey
。
您会看到 Discord 中不包含用户名,message.content
而只包含消息文本
旁注:如果您想在您的消息中测试类似的内容,您可以使用正则表达式:
/jersey/.test(message.content)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句