简体   繁体   English

使用最新的 discord.js 将用户添加到角色

[英]add user to role with newest discord.js

i'm using newest discord.js in node.js and i'm trying to add user to role, but it seems bot.addUserToRole() was removed.我在 node.js 中使用最新的 discord.js 并且我正在尝试将用户添加到角色,但似乎 bot.addUserToRole() 已被删除。

How can I do it when I know only rank name, not it's ID?当我只知道等级名称而不知道 ID 时,我该怎么做?

Here's what worked for me, hope this helps!这对我有用,希望这会有所帮助!

var role= member.guild.roles.cache.find(role => role.name === "role name");
member.roles.add(role);

Here is the official documentation on it. 是关于它的官方文档。

You can do this with:你可以这样做:

var role = message.guild.roles.find(role => role.name === "MyRole");
message.member.addRole(role);

For me (right now) this works only:对我来说(现在)这仅适用于:

var role = message.member.roles.cache.find(role => role.name === "role name");
if (!role) return;
message.member.guild.roles.add(role);

As of now, 2020, this Code works fine, and its easy to implement since its only based on the message object截至 2020 年,此代码运行良好,并且易于实现,因为它仅基于消息对象

let role = message.member.guild.roles.cache.find(role => role.name === "your role");
if (role) message.guild.members.cache.get(message.author.id).roles.add(role);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM