node.js - 删除所有角色
问题描述
我正在尝试从公会中删除每个角色,但我不知道该怎么做。这是我尝试过的:
const guild = message.guild;
guild.roles.cache.forEach(role => role.delete());
当我运行命令时,我收到一个未处理的承诺拒绝错误。
解决方案
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
}
推荐阅读
- java - 为什么我的闹钟无法使用 BOOT_COMPLETED
- kubernetes - 如何在 IBM Kubernetes 服务中启用 IPV6 支持
- c# - 当多个实体派生自一个公共实体并保存在同一个表中时,如何按实体查询对象
- php - Composer:如何忽略某些依赖项的安装/更新?
- angularjs - 在 angularjs 中使用搜索创建下拉树
- javascript - 如何在 Node.js 中基于 Jade 的视图中向 head 部分添加额外的 javascript
- python - 如何使用 pygame.midi 发送“延音踏板”midi 信号?
- javascript - 计算数字空间以逼近一行文本
- r - 在 R,dplyr 中过滤单个日期
- python - ImportError:没有名为 src.utils tensorflow 的模块