简体   繁体   English

反应事件 discord.js

[英]Reaction event 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.但我正试图让机器人忽略实际消息作者的反应。

This is my current code:这是我当前的代码:

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

  manageBoard(reaction_orig)
})

It returns the following error:它返回以下错误:

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

The problem is that messageReactionAdd takes two parameters;问题是messageReactionAdd有两个参数; 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.当您编写reaction_orig, message, user时, reaction_orig是反应(这是正确的),但message是做出反应的用户,因为它是第二个参数。 The user variable will be undefined . user变量将是undefined的。

Another issue is that reaction_orig.users returns a ReactionUserManager that doesn't have an id property.另一个问题是reaction_orig.users返回一个没有id属性的ReactionUserManager Luckily, the user is already passed down to your callback so you can use its ID.幸运的是, user已经传递给您的回调,因此您可以使用它的 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.此外, reaction_orig有一个message属性,即该反应所引用的原始消息,因此您可以从中获取其作者的 ID。

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

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.对旧消息做出反应不会触发messageReactionAdd事件。 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:如果您还想收听对旧消息的反应,则需要在实例化客户端时为MESSAGECHANNELREACTION启用部分结构,如下所示:

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

You can check if the message is cached by eg checking if its author property is not null .您可以检查消息是否被缓存,例如检查其author属性是否不是null If it's null , you can fetch the message .如果是null ,则可以获取消息 Now, you have both the message author and the user who reacted, so you can compare their IDs:现在,您拥有消息作者和做出反应的用户,因此您可以比较他们的 ID:

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

Try doing this:尝试这样做:

client.on('messageReactionAdd', (reaction, user) => {
    if (!reaction.message.author.id === user.id){
        //Do whatever you like with it
        console.log(reaction.name)
    }
});

Note: The message must be cached.注意:消息必须被缓存。 For that you'll need to do this为此,您需要这样做

Client.channels.cache.get("ChannelID").messages.fetch("MessageID");

I'm guessing you're using discord.js v12我猜你正在使用 discord.js v12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM