Entity2k3
Entity2k3

Reputation: 76

Discord.py - Ban everyone in the server without a role

I was messing around with the ban everyone command, trying to fix it so it actually does something instead of giving me the error:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Forbidden: 403 Forbidden (error code: 50013): Missing Permissions

The problem is that I don't know how to exclude the administrator roles and ban members without a role (only pingable with @everyone). The code I am currently using:

@client.command()
async def e(ctx):
    print('Logged in!')
    for member in client.get_all_members():
        await member.ban(reason=banreason + banreason2 + banreason3)
        await ctx.send(f"**{member.display_name}** was banned and invite links were sent. :white_check_mark:")
        print(f"Banned {member.display_name} and invite links were sent.")
    print("Banning is complete!")

Note: there is no error with banreason, it works fine and has nothing to do with the permission error. I also put the bot above the roles I want to be banned.

Upvotes: 0

Views: 7976

Answers (1)

Jeroen
Jeroen

Reputation: 289

If you only want to exclude everyone that has a role other than @everyone than the following will work.

@client.command()
async def e(ctx):
    for member in ctx.guild.members:
        if len(member.roles) < 2:
            await member.ban(reason=banreason + banreason2 + banreason3)
            await ctx.send(f"**{member.display_name}** was banned and invite links were sent. :white_check_mark:")
            print(f"Banned {member.display_name} and invite links were sent.")
    print("Banning complete!")

Upvotes: 2

Related Questions