用户对特定消息做出反应时如何添加角色

时间:2019-07-06 10:44:22

标签: discord.js

我需要它,以便机器人可以发送消息,并且如果服务器上的任何人对消息做出反应并为之做出响应,它将给他们一个角色。

我已经尝试了多次,使用了不同的代码示例,这些示例允许在行会中的任何地方做出反应,但是我希望它特定于一个通道。

client.on("messageReactionAdd", (reaction, user) => {
  if (user.bot) return;
  const member = reaction.message.member
   switch (reaction.name) {
     case "?":
     member.addRole("597011179545690121").then((res) => {
       reaction.message.channel.send(`You've been given the \`${res.name}\` role!`)
     }).catch(console.error);
     break;
     case "?":
     member.addRole("597011179545690121").then((res) => {
       reaction.message.channel.send(`You've been given the \`${res.name}\` role!`)
     }).catch(console.error);
  };
})

client.on("messageReactionRemove", (reaction, user) => {
  if (user.bot) return;
  const member = reaction.message.member
  switch (reaction.name) {
case "emoji_name_1":
member.removeRole("roleID").then((res) => 
  reaction.message.channel.send(`You've been removed from the \`${res.name}\` role!`)
    }).catch(console.error);
    break;
    case "emoji_name_2":
    member.removeRole("someOtherRole").then((res) => {
      reaction.message.channel.send(`You've been removed from the \`${res.name}\` role!`)
    }).catch(console.error);
  };
})

我希望结果是该人以微笑的表情做出反应,并获得一定的作用,但他们必须对机器人在自己的专用渠道中发送的某些消息做出反应,就像预制机器人一样,例如反应角色机器人。

2 个答案:

答案 0 :(得分:0)

首先,我们必须获取消息。

let channel_id = "ChanelID of message"; 
let message_id = "ID of message";



client.on("ready", (reaction, user) => {

client.channels.get(channel_id).fetchMessage(message_id).then(m => {
        console.log("Cached reaction message.");
    }).catch(e => {
    console.error("Error loading message.");
    console.error(e);
    });

然后,我们将检查是否有人对我们的消息做出了反应并赋予他们适当的角色

client.on("messageReactionAdd", (reaction, user) => {
    if(reaction.emoji.id == "EMOJI ID HERE" && reaction.message.id === message_id) 
        {
            guild.fetchMember(user) // fetch the user that reacted
                .then((member) => 
                {
                    let role = (member.guild.roles.find(role => role.name === "YOUR ROLE NAME HERE"));
                    member.addRole(role)
                    .then(() => 
                    {
                        console.log(`Added the role to ${member.displayName}`);
                    }
                    );
                });
        }
}

我建议不要一次针对多个角色使用此代码,因为这样做效率不高。

答案 1 :(得分:0)

  
const member = reaction.message.member

问题:这是将member定义为发送消息的GuildMember,而不是添加响应的消息。

解决方案:例如,使用Guild.member()方法...

const member = reaction.message.guild.member(user);

  
switch (reaction.name) {...}

问题nameMessageReaction的无效属性。

解决方案:您要查找的是ReactionEmoji.name,可通过reaction.emoji.name访问。


  

... [用户]必须对特定消息做出反应...

问题:您当前的代码没有检查有关已响应消息的任何内容。因此,任何反应都会触发它。

解决方案::检查邮件ID,以确保它是您想要的确切的。如果您希望对某个频道内的任何消息 做出反应,请检查该消息的频道ID。

考虑以下示例:

if (reaction.message.id !== "insert message ID here") return;
if (reaction.message.channel.id !== "insert channel ID here") return;

问题:未发出messageReactionAdd事件是对未缓存邮件的反应。

解决方案::使用raw事件来获取所需的信息并自行发出该事件,如图here所示。或者,使用Caltrop的解决方案,并在客户端准备就绪时通过TextChannel.fetchMessage()获取消息。

相关问题