0
votes
@bot.command()
async def unban(ctx, id: int):
    user = await bot.fetch_user(id)
    async for entry in ctx.message.guild.audit_logs(limit=None, user=user, action=discord.AuditLogAction.ban):
        await ctx.guild.unban(entry.target)
        print("Unbanned ", entry.target, entry.target.id, "Banned by ", entry.user, "Entry ID: ", entry.id)

Above function was written to take in a user id, and to pass that into audit_logs() to get all audit log entries of that user's bans. Then I tried to issue the unban() using the entry target. This worked when I tested it on my private server. I invited a bunch of bots, banned some myself, and use one bot to ban others. I ran the command with my own user id, and BattleNubBot only unbanned the users I had banned.

I think had the admin of a server I mod for invite my bot with view audit log, and ban privileges, and then we tried the command there. We get:

Ignoring exception in command unban:

Traceback (most recent call last):
  File "C:\Users\Joey\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File ".\BattleNubBot.py", line 21, in unban
    await ctx.guild.unban(entry.target)
  File "C:\Users\Joey\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\guild.py", line 1892, in unban
    await self._state.http.unban(user.id, self.id, reason=reason)
  File "C:\Users\Joey\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\http.py", line 243, in request
    raise NotFound(r, data)
discord.errors.NotFound: 404 Not Found (error code: 10026): Unknown Ban
1

1 Answers

1
votes

I don't know why do you use audit_logs to unban a member, there is a better way to do this

@client.command()
async def unban(ctx, member):
    banned_users = await ctx.guild.bans()
    member_name, member_discriminator = member.split("#")
    for banned_member in banned_users:
        user = banned_member.user
        if (user.name, user.discriminator) == (member_name, member_discriminator):
            await ctx.guild.unban(user)

If you just want to do a unban command, this will work. You can use it like .unban someone#1234.