(discord.js) 添加角色到 "user"

(discord.js) Add role to "user"

有没有办法从“用户”获取“成员”? 我使用斜杠命令,我想为成员添加角色。但是没有办法在斜杠命令中找到成员。这是我如何找到用户的方式。

const user = interaction.options.getUser('user')

当我尝试时

user.roles.add(role id)

出现一些错误:

user.roles.add(role id)
           ^
"add" is not a function

当您通过 interaction.options 获取用户时,您会收到一个 User object. But to add roles, you need a GuildMember 对象。所以你必须将用户更改为:

const user = interaction.guild.members.cache.get(interaction.options.getUser('user').id)