Discord.js:reaction.message.guild.members.find不是函数

编程入门 行业动态 更新时间:2024-10-21 03:01:22
本文介绍了Discord.js:reaction.message.guild.members.find不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试制作一个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不是函数

本文发布于:2023-10-30 04:18:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1541742.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   reaction   js   Discord   message

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!