You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.py创建角色层级问题:新角色位于Bot角色上方无法调整

Discord.py创建角色层级权限问题解决方案

问题说明

使用Discord.py批量创建服务器角色时,新角色默认被创建在Bot角色的上方,导致后续无法调整角色位置——因为Discord的权限规则限制,Bot只能操作层级比自己低的角色。尝试过创建后移动角色,但因权限不足失败,尝试代码如下:

bot_role = guild.me.top_role
await new_role.edit(position=bot_role.position - 1)

原始角色生成代码:

async def generate_role(guild):
    with open("cogs/ServerGenerator/patterns/default.yml", 'r', encoding='utf-8') as rFile:
        data = yaml.safe_load(rFile)

    for role_name, role_data in data['roles'].items():
        if role_name == "everyone":
            await guild.default_role.edit(permissions=discord.Permissions(**role_data.get('permissions', {})))
        else:
            new_role = await guild.create_role(
                name=role_data.get('name', None),
                color=int(role_data.get('color', None), 16),
                hoist=role_data.get('hoist', None),
                mentionable=role_data.get('mentionable', None),
                permissions=discord.Permissions(**role_data.get('permissions', {}))
            )

对应的YAML配置文件:

roles:
  #Server Roles
  owner:
    name: "⋞ᴏᴡɴᴇʀ⋡"
    color: "e5e357"
    hoist: true
    mentionable: false
  administrator:
    name: "⋞ᴀᴅᴍɪɴ⋡"
    color: "ed0041"
    hoist: true
    mentionable: true
  moderator:
    name: "⋞ᴍᴏᴅ⋡"
    color: "73c65a"
    hoist: true
    mentionable: true
  booster:
    name: "⋞ʙᴏᴏsᴛᴇʀ⋡"
    color: "fb0399"
    hoist: true
    mentionable: true
  member:
    name: "⋞ᴍᴇᴍʙᴇʀ⋡"
    color: "03d998"
    hoist: true
    mentionable: true
  newbie:
    name: "⋞ɴᴇᴡʙɪᴇ⋡"
    color: "ccc7a"
    hoist: true
    mentionable: true
  #Gender
  male:
    name: "ᴍᴀʟᴇ"
    color: "007fff"
    hoist: false
    mentionable: true
  female:
    name: "ғᴇᴍ"
    color: "c601e2"
    hoist: false
    mentionable: true
  other:
    name: "ᴏᴛʜᴇʀ〳ɴᴏɴ ʙɪ〳ᴘʀᴇғᴇʀ ɴᴏᴛ ᴛᴏ sᴀʏ"
    color: "03d998"
    hoist: false
    mentionable: true
  #Pronouns
  he:
    name: "ʜᴇ〳ʜɪᴍ"
    color: "007fff"
    hoist: false
    mentionable: true
  she:
    name: "sʜᴇ〳ʜᴇʀ"
    color: "c601e2"
    hoist: false
    mentionable: true
  they:
    name: "ᴛʜᴇʏ〳ᴛʜᴇᴍ﹝+ᴏᴛʜᴇʀs﹞"
    color: "03d998"
    hoist: false
    mentionable: true
  #Age
  18:
    name: "⊰ᴀɢᴇ⊱﹝𝟷𝟾+﹞"
    color: "9659b6"
    hoist: false
    mentionable: true
  15:
    name: "⊰ᴀɢᴇ⊱﹝𝟷𝟻+﹞"
    color: "a060d0"
    hoist: false
    mentionable: true
  13:
    name: "⊰ᴀɢᴇ⊱﹝𝟷𝟹+﹞"
    color: "a77dc2"
    hoist: false
    mentionable: true

原因分析

Discord角色权限的核心规则:

  • 角色列表从上到下对应层级从高到低,角色的position数值越小,层级越高
  • Bot只能操作层级低于自身最高角色的角色
  • 默认调用create_role()时,Discord会将新角色插入到Bot角色的上方(层级更高),导致Bot后续无法修改该角色

解决方案

1. 创建角色时直接指定位置

修改角色生成代码,在创建角色时通过position参数指定新角色的位置在Bot最高角色的下方,确保新角色层级低于Bot。同时批量创建时依次递增位置,避免角色重叠:

async def generate_role(guild):
    with open("cogs/ServerGenerator/patterns/default.yml", 'r', encoding='utf-8') as rFile:
        data = yaml.safe_load(rFile)

    bot_top_role = guild.me.top_role
    # 从Bot角色下方的位置开始创建新角色
    current_position = bot_top_role.position + 1

    for role_name, role_data in data['roles'].items():
        if role_name == "everyone":
            await guild.default_role.edit(permissions=discord.Permissions(**role_data.get('permissions', {})))
        else:
            new_role = await guild.create_role(
                name=role_data.get('name', None),
                color=int(role_data.get('color', None), 16),
                hoist=role_data.get('hoist', None),
                mentionable=role_data.get('mentionable', None),
                permissions=discord.Permissions(**role_data.get('permissions', {})),
                position=current_position  # 指定创建位置
            )
            current_position += 1  # 每个新角色依次往下排列

2. 确保Bot角色层级足够高

在服务器设置的「角色」页面,手动将Bot的角色拖动到所有要创建的角色的上方(列表中更靠上的位置),确保Bot的层级高于所有待创建角色。这是代码生效的前提,否则即使指定position参数,Discord也会因权限限制无法创建到Bot下方。

3. 检查Bot权限

确认Bot拥有Manage Roles(管理角色)权限,这是创建和修改角色的基础权限。


内容的提问来源于stack exchange,提问作者Tweedle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 00:39:53