【问题标题】:how can i stop a discord bot from replying to itself or other bots? (discord.js)如何阻止不和谐机器人回复自己或其他机器人? (discord.js)
【发布时间】:2020-10-11 01:46:34
【问题描述】:

我是整个编码领域的新手。 几天前我开始研究我的第一个不和谐机器人,你知道,我和我的朋友们可以玩弄。 现在,假设我希望这个机器人检测消息中的单词并在每次有人提到该单词时回复,无论消息的哪个部分。我能够做到这一点,但现在有一个问题。 假设我要找的词是“你好”。 如果有人说“哦,你好”,“你好”,一条带有“你好”的消息,机器人会回复“你好”。 但机器人也会在自己的消息中检测到你好,并一遍又一遍地回复自己,直到我将其关闭。 这是代码:

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

所以,我不知道如何让机器人在自己的消息中看到“你好”,或者如果这更容易的话,或者任何机器人的消息,但能够分析“你好” " 来自用户,这样它就不会陷入回复自己的无限循环中。 我怎样才能做到这一点??提前谢谢你(:

【问题讨论】:

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


    【解决方案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!");
    }
    })
    

    【讨论】:

      【解决方案2】:

      如果想阻止机器人回复自己:

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

      您还可以阻止机器人回复另一个机器人:

      if (message.author.bot)
         return;
      

      【讨论】:

        猜你喜欢
        • 2018-06-27
        • 2018-02-19
        • 2021-07-30
        • 2021-11-10
        • 2018-11-25
        • 2020-06-22
        • 2021-11-04
        • 1970-01-01
        • 2020-12-06
        相关资源
        最近更新 更多