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
- Create/move/remove some roles around in a guild
- Try to move roles using
role.edit(..) or in the client
- 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
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.
Summary
After role create/delete (and sometimes client reorders), the cached role positions drift from the API, so
guild.rolescan end up misordered and any position-setting logic may act on bad data.Reproduction Steps
role.edit(..)or in the clientguild.rolesordering/positions withawait guild.fetch_roles()Minimal Reproducible Code
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
Checklist
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.