問題描述
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);
這篇關于Discord JS//嘗試通過對消息做出反應來添加角色的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!