DiscordAPIError: Unknown role

半世苍凉 提交于 2021-01-29 15:18:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!