Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

javascript - Discord JS //通过响应消息来尝试添加角色(Discord JS // Trying to add role by reacting to the message)

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

Thats the part of my bot.js.(那就是我的bot.js的一部分。)

I want the user to react in a certain channel and receive role Alerts(我希望用户在某个频道中做出反应并接收角色警报)   ask by Bixi translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.(您还没有真正说明问题是什么,什么有效,什么无效,但是我会在某些引起我注意的地方大刀阔斧。)

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 .(对于初学者,您在变量message上调用属性,而在提供的代码中,您没有创建/设置名为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 .(要做到这一点,你必须使用MessageReaction这是在提供的参数messageReactionAdd事件的reaction 。) From there you can replace message.<something> with reaction.message.<something> everywhere in the code you supplied.(在此处,您可以在提供的代码中的任何位置将message.<something>替换为reaction.message.<something> 。) Something also to note is that you add the role Alerts to message.member .(还需要注意的是,您将角色Alerts添加到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.(这将无法正常工作,因为它将警报角色赋予原始消息的作者。) 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.(为此,您必须使用User参数并找到正确的Member因为您不能向User对象添加角色,但可以向Member对象添加角色。) 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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...