如何阻止不和谐的机器人回复自身或其他机器人? (discord.js)

时间:2020-06-21 01:08:26

标签: javascript node.js bots discord discord.js

我是整个编码领域的新手。 几天前,我开始为我的第一个Disord机器人工作,我和我的朋友们都在搞混。 现在,假设我希望这个bot能够检测消息中的单词并在有人提到该单词时(无论消息的哪个部分)都进行回复。我能够做到这一点,但现在有一个问题。 假设我要寻找的单词是“ hello”。 如果有人说“哦你好”,“你好那里”,带有“你好”一词的消息,则机器人将回复“你好”。 但是该漫游器还会在自己的消息中检测到您好,并一遍又一遍地回复自己,直到我将其关闭。 这是代码:

bot.on("message", message => {
    const hello = ["hello"];
    if( hello.some(word => message.content.includes(word)) ) {
        message.channel.send("Hello!");
}} ) 

因此,我无法弄清楚如何使机器人在自己的消息中看到“你好”,或者如果更容易的话,则无法看到任何机器人的消息,但能够分析“你好” ”,这样就不会陷入回复自己的无限循环中。 我怎样才能做到这一点??预先谢谢你(:

2 个答案:

答案 0 :(得分:4)

如果要阻止漫游器自行回复:

if (message.author == client.user)
    return;

您还可以阻止某个漫游器回复另一个漫游器:

if (message.author.bot)
   return;

答案 1 :(得分:1)

只需使用message.author.bot

检查消息作者是否是机器人。
bot.on("message", message => {
    if (message.author.bot) return
    const hello = ["hello"];
    if( hello.some(word => message.content.includes(word)) ) {
        message.channel.send("Hello!");
}
})