Skip to content

Roles become misordered after creating/deleting roles #1509

@jvrring

Description

@jvrring

Summary

After role create/delete (and sometimes client reorders), the cached role positions drift from the API, so guild.roles can end up misordered and any position-setting logic may act on bad data.

Reproduction Steps

  1. Create/move/remove some roles around in a guild
  2. Try to move roles using role.edit(..) or in the client
  3. Compare guild.roles ordering/positions with await guild.fetch_roles()

Minimal Reproducible Code

import disnake
from disnake.ext import commands

intents = disnake.Intents.default()
intents.message_content = True

bot = commands.Bot(command_prefix=",", intents=intents)

@bot.command()
async def repro(ctx: commands.Context, amount: int = 5) -> None:
    guild = ctx.guild
    assert guild is not None

    base = await guild.create_role(name="base role")
    for i in range(amount):
        await guild.create_role(name=f"example role {i}")

    custom = await guild.create_role(name="custom role")

    await custom.edit(position=base.position + 1)

    title = "api | cache | name"
    lines: list[str] = []

    api_roles = await guild.fetch_roles()
    for api_role in sorted(api_roles, key=lambda r: r.position, reverse=True):
        cached = disnake.utils.get(guild.roles, id=api_role.id)
        if cached is None:
            continue
        lines.append(f"{api_role.position:>2}  {cached.position:>2}  {api_role.name}")

    await ctx.send("\n" + title + "\n" + "\n".join(lines) + "\n")

if __name__ == "__main__":
    bot.run(TOKEN="...")

Expected Results

Cached role positions to be matching with the API role positions

Actual Results

Cached role positions being mismatched with their API role positions

Intents

.all()

System Information

- Python v3.10.11-final
- disnake v2.12.0-final
    - disnake importlib.metadata: v2.12.0
- aiohttp v3.11.16
- system info: Windows 10 10.0.22631 AMD64

Checklist

  • I have searched the open issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have removed my token from display, if visible.

Additional Context

In prod, whenever I try to move a newly created role above a specific role, this fails to happen and the role stays at the bottom of the list.

Metadata

Metadata

Assignees

No one assigned

    Labels

    unconfirmed bugSomething might not be working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions