Javascript 如何使用自定义表情添加反应角色函数?

Javascript 如何使用自定义表情添加反应角色函数?,javascript,discord,discord.js,Javascript,Discord,Discord.js,大家好,Stack Overflow社区的成员们 我一直在开发一个功能,当版主使用Auttaja的gatekeeper批准用户时,该功能就会激活。基本上,新用户可以在两种服务器表情反应中进行选择,这将赋予他/她角色。但我在角色和表情常量方面遇到了困难 这是我的密码: const ApprovalEmbed = new Discord.MessageEmbed() .setColor('RED') .setTitle('Hi there new user!') .setDescription('

大家好,Stack Overflow社区的成员们

我一直在开发一个功能,当版主使用Auttaja的gatekeeper批准用户时,该功能就会激活。基本上,新用户可以在两种服务器表情反应中进行选择,这将赋予他/她角色。但我在角色和表情常量方面遇到了困难

这是我的密码:

 const ApprovalEmbed = new Discord.MessageEmbed()
.setColor('RED')
.setTitle('Hi there new user!')
.setDescription('Please react to this message with either one of the emojis in order to get a role.')
.addFields(
  { name: '\u200B', value: '\u200B' },
  { name: 'The Boomer emoji gives you the "Les boomers normaux" role, and the rat emoji gives you the "The normal Rat Haven dwellers" role.'},
   { name: '\u200B', value: '\u200B' },
  { name: 'Selecting the Boomer emoji will give you access to only the Boomer Haven compartment, and the rat emoji will give you access to the Rat Haven compartment. '},
  { name: '\u200B', value: '\u200B' },
  {name: 'To get access to both of these compartments, please consider using the command "^AccessToBoth" to receive the "Access to both compartments" role.'},
  )
  .setTimestamp()
  .setFooter('Time to pick a role!');
  const roleOne = guild.roles.cache.find((role) => role.name === "The normal Rat Haven dwellers")
  const roleTwo = guild.roles.cache.find((role) => role.name === "Les boomers normaux")
  const BoomerEmoji = message.guild.emojis.cache.find(emoji => emoji.name === 'boomer');
  const RatEmoji = message.guild.emojis.cache.find(emoji => emoji.name === 'robincutmoment');



client.on('message', async message => {
  let AdminRole = message.member.roles.cache.find(role => role.name === "Server Moderators")
  let RulerRole = message.member.roles.cache.find(role => role.name === "The Supreme Boomers")
  let RatRole = message.member.roles.cache.find(role => role.name === "Rat Majesty Robin")
  if (message.member.roles.cache.has(AdminRole)) {
  } else if (message.member.roles.cache.has(RulerRole)) {
  } else if (message.member.roles.cache.has(RatRole)) {}
  if (message.content === `-approve`) {
    message.channel.send(BoomerEmoji)
    const reactionMessage = await message.channel.send(ApprovalEmbed);
    message.channel.send(ApprovalEmbed).then(reactionMessage.react(BoomerEmoji), reactionMessage.react(RatEmoji))
      
    
  }
  const filter = (reaction, user) => {
      return [(BoomerEmoji), (RatEmoji)].includes(reaction.emoji.name) && user.id === message.author.id;
  };
  message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
    .then(collected => {
      const reaction = collected.first();
      
      if (reaction.emoji.name === (BoomerEmoji)) {
        message.member.roles.add(roleOne)
      } else if (reaction.emoji.name === (RatEmoji)) {
              message.member.roles.add(roleTwo)
      }
  })

}))

您的第一个问题出现在这个代码段中:

if(message.member.roles.cache.has(AdminRole)){
// ...
}else if(message.member.roles.cache.has(RulerRole)){
// ...
}else if(message.member.roles.cache.has(RatRole)){
// ...
}
检查集合中是否存在元素。它有一个参数:您希望检查元素是否存在的键。由于是由映射的,因此应该传递,而不是

/`Collection.prototype.has()`与`Map.prototype.has()相同`
//下面是它的工作原理:
常量映射=新映射();
//“foo”是关键,
//“bar”是值
map.set('foo','bar');
console.log(map.has('foo'));//真的

console.log(map.has('bar'));/'酒吧不是关键;false
“有困难”不足以让我们诊断问题并给出解决方案。目前出了什么问题?你有什么错误吗?嗨,Lioness,我有一些错误表明当用户点击反应时角色常量没有发挥作用。同样的道理也可以用在反应上。他们就是不出现。你有吗?是的,我有。所有意图都已启用。嘿!谢谢你的帮助!但是,还有一个小问题,包括脚本的最后一部分,即bot应该赋予用户角色。我仍然无法理解。你能详细说明这个问题是什么吗?基本上,机器人给用户角色(如果(reaction.emoji.name==(BoomerEmoji)){message.member.roles.add(roleOne)}或者如果(reaction.emoji.name==(RatEmoji)){message.member.roles.add(roleTwo)}它什么都不做。只是用户点击了反应,但他或她没有被赋予角色。