Discord JS//尝试通过对消息做出反应来添加角色

2022-01-10 00:00:00 bots args javascript 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.

您可以从那里将 message.<something> 替换为 reaction.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);

相关文章