繁体   English   中英

Discord.js:reaction.message.guild.members.find 不是 function

[英]Discord.js : reaction.message.guild.members.find is not a function

我正在尝试制作一个 Discord.js 机器人,为对 ✅ 表情符号做出反应的用户添加“Joueur”角色。 我是 JS 新手,在网上找到了reaction.message.guild.members.find function 但不知何故得到错误TypeError: reaction.message.guild.members.find is not a function并且没有添加角色。 这是我的代码的一部分:

client.on('messageReactionAdd', async (reaction, user) => {
    if (reaction.emoji.name === "✅") {
      try {
        reaction.message.guild.members.find('id', user.id).addRole(reaction.message.guild.roles.find('name', 'Joueur'));
      } catch {
        console.log('Error : can\'t add the role');
      }
   }
});

如果您使用的是 Discord.js v12(最新版本), Guild#membersGuildMemberManager ,而不是像 v11 中的Collection

要访问集合,请使用cache属性。

另一个区别是Collection不支持像这样通过键和值来查找东西。 你需要使用这个:

reaction.message.guild.members.cache.find(member => member.id === user.id)

您可能还想检查反应是否在公会中完成(除非您使用意图并且没有使用DIRECT_MESSAGE_REACTIONS意图)。 人们也可以在 DM 中添加对消息的反应,因此reaction.message.guild可能是undefined

如果您使用的是最新版本,这里是一个示例:

他们还在 v12 中将 addRole 更改为:roles.add

let role = message.guild.roles.cache.find(role => role.name === 'somerolename');
reaction.message.guild.member(user).roles.add(role.id).catch(console.error);

https://discordjs.guide/additional-info/changes-in-v12.html#roles

根据doc ,您可以使用reaction.users来获取对消息做出反应的用户集合 该集合将如下所示:

Collection(n)[Map] {
    'id_of_user_who_reacted' => <ref*1> ClientUser{
        id: 'id_of_user_who_reacted',
        username: String,
        discriminator:String
        ...
    }
}

然后可以使用 Collector 类型的map()方法获取用户id

console.log(reaction.users.map(user => user.id))

这将返回一个如下所示的数组: ['id', 'id', 'id']然后您可以更改这些用户的角色。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM