简体   繁体   中英

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.

You find the roles by accessing the new cache object.

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

You now add or remove the roles within the 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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