反应事件discord.js [英] Reaction event discord.js

查看:60
本文介绍了反应事件discord.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用我的机器人制作右舷代码,其他所有东西都运行良好.但是我正在尝试使其在机器人忽略实际消息作者的反应的地方.

I'm trying to make a starboard code with my bot, and everything else is working good. But I'm trying to make it to where the bot ignores reactions from the author of the actual message.

这是我当前的代码:

client.on('messageReactionAdd', (reaction_orig, message, user) => {
  if (message.author.id === reaction_orig.users.id) return

  manageBoard(reaction_orig)
})

它返回以下错误:

if (message.author.id === reaction_orig.users.id) return;
                   ^
TypeError: Cannot read property 'id' of undefined

推荐答案

问题是

The problem is that messageReactionAdd takes two parameters; the message reaction as the first one, and the user that applied the emoji as the second one. When you write reaction_orig, message, user, reaction_orig is the reaction (which is correct), but message is the user who reacted as it's the second parameter. The user variable will be undefined.

另一个问题是 reaction_orig.users 返回

Another issue is that reaction_orig.users returns a ReactionUserManager that doesn't have an id property. Luckily, the user is already passed down to your callback so you can use its ID.

此外, reaction_orig 具有 message 属性,该属性是此反应所引用的原始消息,因此您可以从中获取其作者ID.

Also, reaction_orig has a message property, the original message that this reaction refers to so you can get its authors' ID from it.

您可以将代码更改为此起作用:

You can change your code to this to work:

client.on('messageReactionAdd', (reaction_orig, user) => {
  if (reaction_orig.message.author.id === user.id) {
    // the reaction is coming from the same user who posted the message
    return;
  }

  manageBoard(reaction_orig);
});

但是,以上代码仅适用于已缓存的消息,这些消息是在连接了僵尸程序后发布的消息.对较旧的消息做出反应不会触发 messageReactionAdd 事件.如果您还想听取旧邮件的反应,则需要在实例化客户端时为 MESSAGE CHANNEL REACTION 启用部分结构,例如这个:

However, the code above only works on cached messages, ones posted after the bot is connected. Reacting on older messages won't fire the messageReactionAdd event. If you also want to listen to reactions on old messages you need to enable partial structures for MESSAGE, CHANNEL and REACTION when instantiating your client, like this:

const client = new Discord.Client({
  partials: ['MESSAGE', 'CHANNEL', 'REACTION'],
});

例如,您可以检查邮件是否已缓存检查其 author 属性是否不是 null .如果它为 null ,则可以获取邮件.现在,您既有消息作者,也有做出反应的用户,因此您可以比较他们的ID:

You can check if the message is cached by e.g. checking if its author property is not null. If it's null, you can fetch the message. Now, you have both the message author and the user who reacted, so you can compare their IDs:

// make sure it's an async function
client.on('messageReactionAdd', async (reaction_orig, user) => {
  // fetch the message if it's not cached
  const message = !reaction_orig.message.author
    ? await reaction_orig.message.fetch()
    : reaction_orig.message;

  if (message.author.id === user.id) {
    // the reaction is coming from the same user who posted the message
    return;
  }
  
  // the reaction is coming from a different user
  manageBoard(reaction_orig);
});

这篇关于反应事件discord.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆