discord.py missing permissions but the bot is an administrator

Question:

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!

Asked By: user12984498

||

Answers:

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)}')
Answered By: ssebastianoo

You may be missing intents. To enable intents go over here.

Bot entry in menu

  1. Go into your app portal, and click Bot

  2. Enable all intents:

Intents to enable

  1. And insert this code before client.run:
 intents = discord.Intents.default()
 intents.typing = True
 intents.presences = True
 intents.message_content = True

Intents documentation

Answered By: Gamercoder
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.