为在特定频道中对特定表情符号有反应的人添加角色 [英] Add a role to people that react with a certain emoji in a certain channel

查看:85
本文介绍了为在特定频道中对特定表情符号有反应的人添加角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我为学校里的人们制作的服务器制作discord.js机器人。我试图建立一个 #classes 频道,如果您对某些消息作出反应,它将给您一个角色(使您可以访问该类的文本频道)。 / p>



I'm trying to make a discord.js bot for my server I made for people in my school. I'm trying to make a #classes channel and if you react to certain messages it gives you a role (which gives you access to the text channel for that class).

client.on('messageReactionAdd', (reaction, user) => {
  console.log("client.on completed.")
  if (message.channel.name === 'classes') {
    console.log("if(message) completed.")
    if (reaction.emoji.name === "reminder_ribbon") {
      console.log("emoji test completed.")
      const guildMember = reaction.message.guild.members.get(user.id);
      const role = reaction.message.guild.roles.find(role => role.name === "FACS");
      guildMember.addRole(role);
    }
  }
});

这是我到目前为止尝试过的方法,但是,它没有使我/其他人做出反应角色,也不会返回错误消息。

This is what I have tried so far, however, it does not give me/the other people reacted to it the role nor does it return an error message.

PS另外,当他们不反应时,我怎么能使它删除角色呢?

P.S. Also, how would I be able to make it so when they unreact it removes the role?

编辑:看来,它只会得到机器人发出后缓存的消息/消息的反应启动。另外,在第一条if(message.channel.id)消息上未定义消息。

It seems it only gets reactions from cached messages/messages sent after bot startup. Also, message is not defined on the first if(message.channel.id) message.

推荐答案

尝试使用以下代码:

client.on('messageReactionAdd', (reaction, user) => {
  if (reaction.message.channel.id === '552708152232116224') {
    if (reaction.emoji.name === "reminder_ribbon") {
      const guildMember = reaction.message.guild.members.get(user.id);
      const role = reaction.message.guild.roles.get('552709290427940875');
      guildMember.addRole(role);
    }
  }
});

首先, reaction.users 是一个包含所有内容的对象对消息做出反应的用户,因此您首先必须定义要将角色分配给哪个用户。我使用 user.id 修复了 guildMember 的问题。

First of all, reaction.users is an Object with all the users that reacted on the message, so you first have to define to which user you want to assign the role. I fixed this fetching the guildMember with user.id.

第二个错误是您尝试将角色ID分配给 guildMember ,尽管您首先必须获取角色,然后将角色对象分配给 guildMember

The second mistake was that you tried to assign a role ID to a guildMember although you first have to fetch the role and then assign the role Object to the guildMember.

这篇关于为在特定频道中对特定表情符号有反应的人添加角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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