机器人回复消息作者

机器人回复消息作者

本文介绍了机器人回复消息作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

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

I've created my own Node.js bot to work in my discord server.

我的机器人名为 mybot.

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

I've seen numerous examples of responding to incoming messages - they look like this (and work just fine).

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

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

The code above will have the bot reply with "pong" whenever anyone writes just "ping" in the channel.

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

As with most bots, generally you speak to them and ask them for something like @mybot blahblahblah - and then they reply.

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

I want to do this. I want mybot to only reply when he is spoken to. There must be a msg.recipientList or msg.recipients that captures the @mybot. I've looked through Discord.js's docs and I'm having a hard time finding this result.

推荐答案

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

There are a few different ways to do this, but I think the most "elegant" way is to use Message.isMentioned which takes as argument an object (of type User, GuildChannel, Role, string) to check the message for an @reference to the object. All you need to do is provide your bot's User object (the base class's stored object is a ClientUser instance but User is its superclass).

// 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');
  }
});

这篇关于机器人回复消息作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:41