如何在 discord.js 中通过 id 查找用户 [英] How to find user by their id in discord.js
问题描述
我希望我的机器人能够为使用其 ID 声明的特定用户赋予特定角色.我试过 const user = bot.users.cache.get(args[2]);user.roles.add("角色ID")
I want my bot to be able to give specific roles to specific users declared with their id. I tried const user = bot.users.cache.get(args[2]); user.roles.add("role ID")
推荐答案
你遇到的问题是你得到了一个 User 对象而不是 GuildMember 对象.用户不与公会关联,因此不能拥有角色.当用户不涉及公会特定操作(如直接消息)时,您通常希望使用用户.
The problem you have here is you are getting a User object instead of a GuildMember object. Users are not associated with a guild and therefore cannot have roles. You typically want to use a user when it doesn't involve guild specific actions (like Direct Messaging).
您不能直接从 Client 对象获取 Member,但是您可以获取 Client.guilds.cache
然后检索公会,然后使用 Guild 从 Guild 对象获取成员.members.cache
.
You cannot get a Member from the Client object directly, however you can get Client.guilds.cache
and then retrieve the guild, followed by the member from the Guild object using Guild.members.cache
.
但是,像这样的权限系统的大多数实现都需要一些更高权限的用户来运行命令来向新用户授予权限.这意味着您可以直接从调用您的机器人命令的 Message 对象中使用 Message.guild.members.cache.get()
.
However, most implementations of permission systems like this require some higher permission user to run a command to grant a permission to a new user. This means you can use the Message.guild.members.cache.get()
straight from the Message object that invoked your bot command.
这篇关于如何在 discord.js 中通过 id 查找用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!