我正在尝试制作一个Discord.js机器人,该机器人向使用✅表情符号做出反应的用户添加"Joueur"角色.我是JS的新手,我在Internet上找到了 reaction.message.guild.members.find 函数,但我却以某种方式收到错误 TypeError:不是功能,并且不会添加角色.这是我的代码的一部分:
I am trying to make a Discord.js bot that add the "Joueur" role to the user who reacted with the ✅ emoji. I am new to JS and I found the reaction.message.guild.members.find function on the Internet but I somehow get the error TypeError: reaction.message.guild.members.find is not a function and the role is not added. Here is the part of my code :
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#members 是 GuildMemberManager ,而不是像v11中那样的 Collection .
If you're using Discord.js v12 (the latest version), Guild#members is a GuildMemberManager, not a Collection like in v11.
要访问集合,请使用 cache 属性.
To access the collection use the cache property.
另一个区别是 Collection 不支持通过键和值来查找某些东西.您将需要使用以下内容:
Another difference is that Collection does not support finding something by key and value like that. You would need to use this:
reaction.message.guild.members.cache.find(member => member.id === user.id)您可能还希望检查反应是否在行会中完成(除非您使用的是Intent,并且没有使用 DIRECT_MESSAGE_REACTIONS 的Intent).人们也可以在DM中向消息添加响应,因此 reaction.message.guild 可能是 undefined .
You might also want to check that the reaction was done in a guild (unless you're using intents and aren't using the DIRECT_MESSAGE_REACTIONS intent). People can add reactions to messages in DMs as well, so reaction.message.guild may be undefined.
更多推荐
Discord.js:reaction.message.guild.members.find不是函数
发布评论