首页 > 解决方案 > 删除所有角色

问题描述

我正在尝试从公会中删除每个角色,但我不知道该怎么做。这是我尝试过的:

const guild = message.guild;
guild.roles.cache.forEach(role => role.delete());

当我运行命令时,我收到一个未处理的承诺拒绝错误。

标签: node.jsdiscord.js

解决方案


The roles of a guild include @everyone (which is a role for permission purposes but can't be removed) and your bot may not have the correct permissions to delete the roles. A bot can only remove roles if it has the MANAGE_ROLES or ADMINISTRATOR role. Also, bots cannot remove managed roles (i.e. roles for a bot) or roles that are above its highest role.

This will delete every role that the bot is above:

if (server.me.permissions.has('MANAGE_ROLES') {
  await Promise.all(
    server.roles.cache
      .filter(role =>
        role.name !== '@everyone' &&
        !role.managed &&
        server.me.roles.highest.comparePositionTo(role) > 0
      )
      .map(role => role.delete())
  )
} else {
  // do whatever you want if the bot doesn't have the permissions
}

推荐阅读