Why is my Discord bot only banning users without roles?

I’m working on a Discord bot that’s supposed to ban all users, but I’ve run into a problem. Here’s my code:

import discord

BOT_TOKEN = 'your_token_here'
IGNORE_BOTS = True
offline_members_included = True

bot = discord.Client()

@bot.event
async def on_ready():
    print('Bot is online!')
    for user in bot.get_all_members():
        if user.bot and IGNORE_BOTS:
            continue
        try:
            await user.ban(reason='Mass ban initiated', delete_message_days=3)
            print(f'{user.name} has been banned.')
        except:
            print(f'Failed to ban {user.name}')
    print('Ban process completed!')

bot.run(BOT_TOKEN)

The bot has admin permissions and a high role, but it’s only banning users without roles. Any idea why it’s not banning everyone as intended? I’m pretty confused about this. Is there something I’m missing in my code?

I’d suggest checking your bot’s role position in the server hierarchy. Even with admin permissions, Discord prevents bots from banning users with equal or higher roles. Ensure your bot’s role is above all others.

Additionally, consider implementing a safeguard mechanism. Mass banning can be risky and potentially misused. You might want to add a confirmation step or limit the ban functionality to specific users or roles.

For debugging, you could modify your code to log more detailed information about each ban attempt. This would help identify which users are causing issues and why. Remember, with great power comes great responsibility - use this functionality cautiously.

hey, i think ur bot might b having trouble with role hierachy. even with admin perms, it cant ban users with higher roles. try moving the bot’s role above everyone else’s in server settings. also, u could add some error handling to see why bans r failing. good luck!

I’ve encountered a similar issue before, and it’s likely related to role hierarchy. Discord’s permission system prevents users (including bots) from modifying or banning members with roles higher than or equal to their own, even if they have admin permissions.

To resolve this, make sure your bot’s role is positioned higher in the server’s role hierarchy than all the roles you want it to be able to ban. You can do this in your server settings under the ‘Roles’ section.

Also, consider adding error handling to your code to get more specific information about why certain bans are failing. You could modify your except block like this:

except discord.errors.Forbidden as e:
    print(f'Failed to ban {user.name}. Error: {e}')

This will give you more detailed error messages, which can help pinpoint the exact cause of the issue. Remember to use this power responsibly, as mass banning can be quite disruptive!