Search code examples
pythondiscorddiscord.py

'User' Object has no attribute 'Ban'


I'm having issues with my antinuke code on discord.py and I can't figure out what's wrong with my code, my IDE is repl.it and I don't know if it's replit's unstable coding or anything else but it just doesn't work.

The code i'm currently working with is this:

@bot.event
async def on_guild_role_create(role):
        async for i in role.guild.audit_logs(limit=1, after=datetime.datetime.now() - datetime.timedelta(minutes = 2), action=discord.AuditLogAction.role_create):

            await i.user.ban()
            return

Solution

  • According to the discord.py documentation on AuditLogEntry, the .user attribute usually returns a Member instance unless gone, then it’s a User.

    In your code, you could check whether it's a User instance or a Member instance before banning the user, in case it is not a member instance.

    If you believe the user is not gone, you may also try converting the user instance into a member instance with the following code:

    member = role.guild.get_member(i.user.id)