给discord.js用户一个来自客户端类变量的角色



我希望能够从客户端变量const client = new Client({ intents: [GatewayIntentBits.Guilds] });中为用户分配角色

我不能通过交互或类似的方式来完成,因为当我收到服务器的第三方请求时,用户角色需要更新。我试过

const foundUser = client.users.cache.get('userId')
foundUser.addRole('roleId')

但我运气不好

有人知道如何从客户端变量分配/添加角色吗?

它不知道你想在哪个公会中添加角色。foundUserUser,而不是Member。您需要从Guild中获取Member。此外,Member.addRole不存在。首先,将成员意向添加到Client。此外,在discord开发人员门户的bot选项卡中添加意向。

const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers],
});

现在,添加角色:

client.guilds
.fetch("guild id")
.members.fetch("user id")
.roles.add("role id", "reason");

最新更新