Discord JS//通过响应消息来尝试添加角色 [英] Discord JS // Trying to add role by reacting to the message

查看:102
本文介绍了Discord JS//通过响应消息来尝试添加角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

bot.on('messageReactionAdd', async (reaction, user) => {
  // Define the emoji user add       
  let role = message.guild.roles.find(role => role.name === 'Alerts');
  if (message.channel.name !== 'alerts') return message.reply(':x: You must go to the channel #alerts');
  message.member.addRole(role);
});

那是我的bot.js的一部分.我希望用户在某个频道中做出反应并收到角色警报

Thats the part of my bot.js. I want the user to react in a certain channel and receive role Alerts

推荐答案

您尚未真正说明问题是什么,什么有效,什么无效,但是我会在某些抓住我的地方大刀阔斧眼睛.

You haven't really stated what the problem is, what works and what doesn't work but I'll take a wild stab at some parts which catch my eye.

对于初学者,您正在调用变量message上的属性,而在提供的代码中,您并未创建/设置名为message的变量.我的猜测是您想要添加响应的消息.为此,您必须使用 MessageReaction 参数在messageReactionAdd事件中作为reaction提供.

For starters you are calling properties on the variable message whilst in the code you supplied, you didn't create/set a variable named message. My guess is that you want the message to which a reaction has been added. To do that you have to use the MessageReaction parameter which is supplied in the messageReactionAdd event as reaction.

在此处,您可以在提供的代码中的任何地方用reaction.message.<something>替换message.<something>.

From there you can replace message.<something> with reaction.message.<something> everywhere in the code you supplied.

 

还要注意的一点是,您将角色Alerts添加到message.member.这将无法按您希望的方式工作,因为它将把警报"角色赋予原始邮件的作者.

Something also to note is that you add the role Alerts to message.member. This won't work how you want it to, since it will give the Alerts role to the author of the original message.

您想要做的(是我想做的)是获取刚刚对表情符号做出反应的用户,并为其分配警报角色.您必须首先在行会中找到该成员,然后为他们分配警报"角色.为此,您必须使用 User 参数并找到正确的 Member ,因为您可以不能在User对象中添加角色,但可以在Member对象中添加角色.下面是一些代码,有望使您走上正确的轨道.

What (I think) you want to do, is fetch the user who just reacted with the emoji and assign them the Alerts role. You'll have to find the member in the guild first and then assign them the Alerts role. To do this you'll have to use the User parameter and find the correct Member because you can't add a role to a User object but you can to a Member object. Below is some code which should hopefully put you on the right track.

// Fetch and store the guild (the server) in which the message was send.
const guild = reaction.message.guild;

const memberWhoReacted = guild.members.find(member => member.id === user.id);

memberWhoReacted.addRole(role);

这篇关于Discord JS//通过响应消息来尝试添加角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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