Discord bot only removes first role instead of all assigned roles

I’m having trouble with my Discord bot command that’s supposed to remove multiple roles from a user. The code works fine for removing the first role it finds, but then it stops working and doesn’t continue removing the other roles. I can’t figure out what’s causing it to stop early.

I have a list called user_roles that contains role names as strings. When someone uses the !clear command, it should go through all the roles and remove any that the user currently has.

if message.content.startswith("!clear"):
    all_removed = True

    for role_name in user_roles:
        # Loop through each role name
        target_role = discord.utils.get(message.guild.roles, name=role_name)
        if target_role in message.author.roles:
            # Remove the role if user has it
            try:
                await bot.remove_roles(message.author, target_role)
            except discord.Forbidden:
                await bot.send_message(message.author, "Missing permissions to modify roles.")
                all_removed = False
                break

    if all_removed:
        await bot.send_message(message.author, "All roles have been removed successfully.")

What could be causing this behavior where only one role gets removed?

i think ur using an old version of discord.py. bot.remove_roles and bot.send_message are no longer in use - you should use message.author.remove_roles(target_role) and message.channel.send(). that might be why ur bot is acting strange.

Your code’s crashing because you’re only catching discord.Forbidden errors, but Discord throws way more than just permission errors. I’ve hit this same issue - you’ll get discord.HTTPException, network timeouts, and other random stuff that kills your entire loop. Catch discord.DiscordException instead, or just use a broad exception handler so the loop keeps running. Also check your bot’s role position in server settings - if it’s below the roles you’re trying to remove, operations fail silently sometimes. Your bot needs to be higher up in the hierarchy than the target roles.

This is probably Discord’s rate limiting kicking in. When you remove roles too fast without any delay, Discord’s API throttles your requests or some operations just fail silently.

I ran into the same thing with my moderation bot. Adding a small delay between removals fixed it completely. Try putting await asyncio.sleep(0.5) after each remove_roles call. Discord’s servers need time to process each role change.

Also, role operations take a moment to propagate through Discord’s systems. I’d check if the role actually got removed before moving to the next one - makes everything more reliable.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.