问题
I was trying to make a command in discord.js in which the bot finds the highest role it can give to a person and gives it to the person.
const myrole = message.guild.me.roles.highest.rawPosition
const therole = message.guild.roles.cache.find(r => r.rawPosition = myrole-1)
const person = message.guild.member(client.users.cache.get("the id"))
person.roles.add(therole.id);
And I get the following error:
(node:18926) UnhandledPromiseRejectionWarning: DiscordAPIError: Unknown Role
at RequestHandler.execute (/rbd/pnpm-volume/d8568466-4a2a-4c3a-ac47-cee06cded9bb/node_modules/.registry.npmjs.org/discord.js/12.2.0/node_modules/discord.js/src/rest/RequestHandler.js:170:25)
at processTicksAndRejections (internal/process/task_queues.js:88:5)
Can anyone help me out with this?
回答1:
Your current code is not checking if r.rawPosition
is equal to myrole - 1
it is defining r.rawPosition
as myrole - 1
const myrole = message.guild.me.roles.highest.rawPosition
const therole = message.guild.roles.cache.find(r => r.rawPosition === myrole-1)
const person = message.guild.member(client.users.cache.get("the id"))
person.roles.add(therole.id);
来源:https://stackoverflow.com/questions/62091857/discordapierror-unknown-role