简体   繁体   English

为不和谐成员添加角色

[英]add roles to discord member

it was not giving an error a few days ago, now it is giving an error strangely.几天前它没有给出错误,现在它奇怪地给出了错误。

when i add code to add or remove a role, I get this error.当我添加代码以添加或删除角色时,出现此错误。

My code:我的代码:

let member = message.mentions.members.first();
let role = message.guild.roles.get("707628035498836069");
let role1 = message.guild.roles.get("769919065551929385");

member.addRole(role);
member.removeRole(role1);

Error code:错误代码:

(node:16668) UnhandledPromiseRejectionWarning: TypeError: Supplied parameter was neither a Role nor a Snowflake.
    at GuildMember.removeRole (C:\Users\user\Desktop\discordjssss\node_modules\discord.js\src\structures\GuildMember.js:516:38)
    at Object.module.exports.baslat (C:\Users\user\Desktop\discordjssss\komutlar\genel\jaildenm.js:22:10)
    at AdvancedClient.<anonymous> (C:\Users\user\Desktop\discordjssss\node_modules\discordjs-advanced\src\client.js:549:5)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:16668) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:16668) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

In discord.js v.12 you need to use a slighty different syntax for finding and adding roles.在 discord.js v.12 中,您需要使用稍微不同的语法来查找和添加角色。

You find the roles by accessing the new cache object.您可以通过访问新的cache对象来查找角色。

message.guild.roles.cache.get('your ID here');

You now add or remove the roles within the GuildMemberRoleManager .您现在在GuildMemberRoleManager添加或删除角色。

member.roles.add(role here);
member.roles.remove(role here);

It needs to be said that you don't need to get the role object first.需要说明的是,不需要先获取角色对象。 You can simply use the ID.您可以简单地使用 ID。 But if you get the role first you can check if it actually exists.但是,如果您先获得该角色,则可以检查它是否确实存在。 But that is neither here nor there.但这既不在这里也不在那里。

Note: Your original problem was that your role wasn't found.注意:您最初的问题是找不到您的角色。 Either because the role doesn't exist or the ID was wrong.要么是因为角色不存在,要么是 ID 错误。

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

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