使用audit_log条目禁止目标用户在特定服务器上不工作-discord.py

时间:2020-09-01 15:56:32

标签: python discord.py discord.py-rewrite

@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仅取消了我已禁止的用户。

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

忽略命令取消禁止中的异常:

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 个答案:

答案 0 :(得分:1)

我不知道您为什么使用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一样使用它。