disnake icon indicating copy to clipboard operation
disnake copied to clipboard

Roles become misordered after creating/deleting roles

Open jvrring opened this issue 9 months ago • 2 comments

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

  • [x] I have searched the open issues for duplicates.
  • [x] I have shown the entire traceback, if possible.
  • [x] 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.

jvrring avatar Mar 03 '26 07:03 jvrring

this seems more of a race condition IMO. have you tried with delays ?

jaymaart avatar Mar 06 '26 07:03 jaymaart

Hi, I tried to reproduce this bug using the provided code and also by trying other operations (e.g moving the base role to another position then create the roles as well as moving the base role, creating the new roles, deleting some roles and creating the last role incrementing the base role position) but unfortunately couldn't reproduce the issue, are you still able to reproduce this bug?

Snipy7374 avatar Nov 12 '26 19:11 Snipy7374