首页 > 解决方案 > 使用 audit_log 条目解禁不在特定服务器上工作的目标用户 - discord.py

问题描述

@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)

上面的函数被编写为接收用户 ID,并将其传递给 audit_logs() 以获取该用户禁令的所有审计日志条目。然后我尝试使用入口目标发出 unban() 。当我在我的私人服务器上测试它时,这很有效。我邀请了一堆机器人,我自己禁止了一些机器人,并使用一个机器人来禁止其他机器人。我使用自己的用户 ID 运行命令,而 BattleNubBot 只取消了我禁止的用户。

我认为让我改装的服务器的管理员邀请我的机器人查看审核日志并禁止特权,然后我们在那里尝试了该命令。我们得到:

忽略命令 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

标签: pythondiscord.pydiscord.py-rewrite

解决方案


我不知道你为什么audit_logs要取消会员,有更好的方法可以做到这一点

@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)

如果您只想执行取消禁令命令,这将起作用。你可以像使用它一样.unban someone#1234


推荐阅读