【问题标题】:Bot reply to message author机器人回复消息作者
【发布时间】:2017-07-10 12:24:08
【问题描述】:

我创建了自己的 Node.js 机器人来在我的 discord 服务器中工作。

我的机器人名为mybot

我见过很多响应传入消息的示例 - 它们看起来像这样(并且工作正常)。

chatroom.on('message', function(msg){
    if(msg.content === 'ping'){
        msg.reply('pong');
    }
});

只要有人在频道中写“ping”,上面的代码就会让机器人回复“pong”。

与大多数机器人一样,通常您与他们交谈并询问他们类似@mybot blahblahblah 的内容 - 然后他们会回复。

我想这样做。我希望mybot 仅在与他交谈时回复。必须存在捕获@mybotmsg.recipientListmsg.recipients。我查看了 Discord.js 的文档,但很难找到这个结果。

【问题讨论】:

    标签: node.js discord discord.js


    【解决方案1】:

    有几种不同的方法可以做到这一点,但我认为最“优雅”的方法是使用Message.isMentioned,它将一个对象(类型为 User、GuildChannel、Role、string)作为参数来检查消息对象的@reference。您需要做的就是提供您的机器人的 User 对象(基类的存储对象是一个 ClientUser 实例,但 User 是它的超类)。

    // I'm assuming chatroom is your bot's DiscordClient instance,
    // if it isn't then replace the "chatroom" in chatroom.user with the bot's 
    // DiscordClient.
    chatroom.on('message', msg => {
      if (msg.isMentioned(chatroom.user)) {
        msg.reply('pong');
      }
    });
    

    【讨论】:

    • 是的,聊天室是客户端。谢谢你。这个答案奏效了!
    【解决方案2】:

    您也可以使用if(message.content.startsWith(prefix))if(message.content.indexOf("what you are looking in message") != -1)。第一个是当您希望消息以前缀开头时,第二个是您希望某事在消息中时。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-19
      • 2018-06-17
      • 2023-03-21
      • 2020-12-20
      • 2019-06-27
      相关资源
      最近更新 更多