Javascript Discord JS//试图通过响应消息来添加角色 bot.on('messageReactionAdd',异步(反应,用户)=>{ //定义用户添加的表情符号 让role=message.guild.roles.find(role=>role.name=='Alerts'); 如果(message.channel.name!=='alerts')返回message.reply(':x:您必须转到频道#alerts'); message.member.addRole(角色); });
这是我的bot.js的一部分。我希望用户在某个频道中做出反应并接收角色警报您还没有真正说明问题是什么,什么有效,什么无效,但我会在一些引起我注意的地方尝试一下 对于初学者,您正在调用变量Javascript Discord JS//试图通过响应消息来添加角色 bot.on('messageReactionAdd',异步(反应,用户)=>{ //定义用户添加的表情符号 让role=message.guild.roles.find(role=>role.name=='Alerts'); 如果(message.channel.name!=='alerts')返回message.reply(':x:您必须转到频道#alerts'); message.member.addRole(角色); });,javascript,bots,discord.js,args,Javascript,Bots,Discord.js,Args,这是我的bot.js的一部分。我希望用户在某个频道中做出反应并接收角色警报您还没有真正说明问题是什么,什么有效,什么无效,但我会在一些引起我注意的地方尝试一下 对于初学者,您正在调用变量message的属性,而在您提供的代码中,您没有创建/设置名为message的变量。我的猜测是,您想要添加了反应的消息。为此,必须使用messageReactionAdd事件中提供的参数作为reaction 从那里,您可以将消息。替换为reaction.message。您提供的代码中的任何地方 还需要注意的是,
message
的属性,而在您提供的代码中,您没有创建/设置名为message
的变量。我的猜测是,您想要添加了反应的消息。为此,必须使用messageReactionAdd
事件中提供的参数作为reaction
从那里,您可以将消息。
替换为reaction.message。
您提供的代码中的任何地方
还需要注意的是,您将角色警报添加到message.member
。这将无法按您希望的方式工作,因为它会将警报角色授予原始邮件的作者
(我认为)你要做的是,找到刚刚对表情符号做出反应的用户,并为他们分配警报角色。你必须先在公会中找到成员,然后给他们分配警报角色。要做到这一点,您必须使用参数并找到正确的,因为您不能向用户对象添加角色,但可以向成员对象添加角色。下面是一些代码,希望能让您走上正轨
//获取并存储发送消息的公会(服务器)。
const guild=reaction.message.guild;
const memberWhoReacted=guild.members.find(member=>member.id==user.id);
memberWhoReacted.addRole(角色);
您还没有真正说明问题所在,哪些有效,哪些无效,但我将大胆尝试一下吸引我眼球的部分
对于初学者,您正在调用变量message
的属性,而在您提供的代码中,您没有创建/设置名为message
的变量。我的猜测是,您想要添加了反应的消息。为此,必须使用messageReactionAdd
事件中提供的参数作为reaction
从那里,您可以将消息。
替换为reaction.message。
您提供的代码中的任何地方
还需要注意的是,您将角色警报添加到message.member
。这将无法按您希望的方式工作,因为它会将警报角色授予原始邮件的作者
(我认为)你要做的是,找到刚刚对表情符号做出反应的用户,并为他们分配警报角色。你必须先在公会中找到成员,然后给他们分配警报角色。要做到这一点,您必须使用参数并找到正确的,因为您不能向用户对象添加角色,但可以向成员对象添加角色。下面是一些代码,希望能让您走上正轨
//获取并存储发送消息的公会(服务器)。
const guild=reaction.message.guild;
const memberWhoReacted=guild.members.find(member=>member.id==user.id);
memberWhoReacted.addRole(角色);
这里有一个快速的答案,尽管为时已晚。因此,我将使用Discord.js v.12.x(或与Discord.js Master相同)更新答案
bot.on('messageReactionAdd',异步(反应,用户)=>{
//过滤反应
如果(reaction.id==''){
//定义用户添加的表情符号
让role=message.guild.roles.cache.find((role)=>role.name=='Alerts');
如果(message.channel.name!=“警报”){
message.reply(':x:您必须转到频道“警报”);
}否则{
message.member.addRole(role.id);
}
}
});
这里有一个快速的答案,尽管为时已晚。因此,我将使用Discord.js v.12.x(或与Discord.js Master相同)更新答案
bot.on('messageReactionAdd',异步(反应,用户)=>{
//过滤反应
如果(reaction.id==''){
//定义用户添加的表情符号
让role=message.guild.roles.cache.find((role)=>role.name=='Alerts');
如果(message.channel.name!=“警报”){
message.reply(':x:您必须转到频道“警报”);
}否则{
message.member.addRole(role.id);
}
}
});
您正在使用消息。member
变量,尽管未定义消息您正在使用消息。member
变量,尽管未定义消息
这些方法中的任何一个都无法在v12中真正工作,因此我更新了它,以便其他人搜索。
如果你发现任何错误,一定要让我知道
const Discord = require('discord.js');
const client = new Discord.Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] }); //partials arent really needed but I woudld reccomend using them because not every reaction is stored in the cache (it's new in v12)
const prefix = "-";
client.on('messageReactionAdd', async (reaction, user) => {
if (reaction.partial) { //this whole section just checks if the reaction is partial
try {
await reaction.fetch(); //fetches reaction because not every reaction is stored in the cache
} catch (error) {
console.error('Fetching message failed: ', error);
return;
}
}
if (!user.bot) {
if (reaction.emoji.id == yourEmojID) { //if the user reacted with the right emoji
const role = reaction.message.guild.roles.cache.find(r => r.id === yourRoleID); //finds role you want to assign (you could also user .name instead of .id)
const { guild } = reaction.message //store the guild of the reaction in variable
const member = guild.members.cache.find(member => member.id === user.id); //find the member who reacted (because user and member are seperate things)
member.roles.add(role); //assign selected role to member
}
}
})
这些方法中的任何一种都不会在v12中真正起作用,所以我更新了它,以便其他人搜索。
如果你发现任何错误,一定要让我知道
const Discord = require('discord.js');
const client = new Discord.Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] }); //partials arent really needed but I woudld reccomend using them because not every reaction is stored in the cache (it's new in v12)
const prefix = "-";
client.on('messageReactionAdd', async (reaction, user) => {
if (reaction.partial) { //this whole section just checks if the reaction is partial
try {
await reaction.fetch(); //fetches reaction because not every reaction is stored in the cache
} catch (error) {
console.error('Fetching message failed: ', error);
return;
}
}
if (!user.bot) {
if (reaction.emoji.id == yourEmojID) { //if the user reacted with the right emoji
const role = reaction.message.guild.roles.cache.find(r => r.id === yourRoleID); //finds role you want to assign (you could also user .name instead of .id)
const { guild } = reaction.message //store the guild of the reaction in variable
const member = guild.members.cache.find(member => member.id === user.id); //find the member who reacted (because user and member are seperate things)
member.roles.add(role); //assign selected role to member
}
}
})
注意:非常简短的回答和/或回复到海报的问题可能应该是评论。你只需要50个代表点就可以在一个问题下发表评论-你能把这个移到那里吗?注意:非常简短和/或是发帖问题的答案可能应该是评论。你只需要50个代表点就可以在一个问题下发表评论-你能把这个移到那里吗?