Discord bot only removes first role from multiple roles instead of all

I’m having trouble with my Discord bot code for removing user roles. When a user has several roles that need to be removed, my bot only takes away the first role it finds and then stops working for some reason. It should go through all the roles in my list and remove each one the user has, but it’s not doing that.

Here’s my code that handles the role removal:

if msg.content.startswith("!clearroles"):
    all_removed = True
    
    for role_name in my_roles:
        # Look through each role in the list
        target_role = discord.utils.get(msg.guild.roles, name=role_name)
        if target_role in msg.author.roles:
            # Remove the role if user has it
            try:
                await bot.remove_roles(msg.author, target_role)
            except discord.Forbidden:
                await bot.send_message(msg.author, "No permission to remove roles.")
                all_removed = False
                break
    
    if all_removed:
        await bot.send_message(msg.author, "All roles removed successfully.")

The variable my_roles contains role names as strings. Can anyone help me figure out why it stops after removing just one role?

Could be an async timing issue - Discord’s API gets wonky with rapid calls. Try throwing in await asyncio.sleep(0.5) between role removals and see if that fixes it. Yeah, you’re using old syntax like everyone said, but sometimes the loop just craps out when it hits rate limits or permission errors without telling you.

This sounds like a role hierarchy issue or an exception that’s killing your loop silently. I’ve hit this before - my bot tried removing a role positioned higher than the bot’s role, Discord threw an exception, but it didn’t get caught and just stopped everything. Add some debug logging inside your loop to see exactly where it breaks. Make sure your bot has ‘Manage Roles’ permission and its role sits above all the roles you’re trying to remove. Also check for rate limiting - Discord throttles rapid role changes, so throw in a small delay between removals if you’re processing a bunch at once.

Your problem is likely due to the use of deprecated discord.py methods. The methods bot.remove_roles() and bot.send_message() come from older versions and can disrupt execution after removing just one role.

To resolve this, you should change await bot.remove_roles(msg.author, target_role) to await msg.author.remove_roles(target_role) and replace await bot.send_message(msg.author, "message") with await msg.author.send("message"). I’ve encountered this issue while updating an old bot, where deprecated methods would either fail silently or lead to uncaught exceptions, halting the loop. Once I switched to the current syntax, multiple role removals worked seamlessly. If you continue to face issues, consider adding a small delay between removals to mitigate Discord’s rate limiting effects.