Search code examples
pythondiscorddiscord.py

discord.py missing permissions but the bot is an administrator


While coding a bot that is supposed to ban everyone in a discord guild (For educational purposes), I have encountered an error. The error says:

@bot.command(aliases=["ban"])
async def ban(ctx, reason="No reason ig"):
    for Member in list(ctx.guild.members):
           await Member.ban(reason=reason)
           await Member.send("You have been banned)
           print(f'[+] The User going under the name of {Member} has been banned')

Now; This is very weird, because the Bot has administrator permissions, it's role is higher than anyone else's. It also has the Priviliged Gateaway Intents enabled. One thing that should also be stated is that when I attempt to create roles or delete them, that works. Thanks in advance for your help!


Solution

  • even if the bot is admin, it can't ban people with higher roles, and if its role is the highest one, it still can't ban the owner, so:

    for Member in ctx.guild.members:
      try:
         try: # if a user has dms blocked this will stop the code with an error
            await Member.send("You have been banned")
         except:
            pass
         
         try:
           await Member.ban(reason=reason)
         except: 
             pass
         
         print(f'[+] The User going under the name of {Member} has been banned')
    
      except:
         print(f'[!] couldn\'t ban {str(Member)}')