discord.js node.js – 机器人回复收件人

我创build了自己的Node.js机器人,在我的不一致的服务器上工作。

我的机器人被命名为mybot

我已经看到了很多响应传入消息的例子 – 它们看起来像这样(而且工作得很好)。

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

只要有人在通道中写入“ping”,上面的代码就会得到“pong”的回复。

和大多数机器人一样,一般你跟他们说话,然后向他们询问@mybot blahblahblah – 然后他们回答。

我想做这个。 我希望mybot只有在与他交谈时才会回复。 必须有一个msg.recipientListmsg.recipients捕获@mybot 。 我浏览过Discord.js的文档,我很难find这个结果。

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

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