python - 使用 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
解决方案
我不知道你为什么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
。
推荐阅读
- azure - Azure 应用程序无法启动并收到 HTTP 500 错误
- swift - 数组没有被洗牌并且屏幕被重绘
- python - 从 API 的多个页面检索的安全数据
- reactjs - 升级到 Gatsby v.2 后组件没有在 React 中呈现,即使我在 Gatsby CLI 中没有错误
- html - 谁能解释一下“在 HTML 中,计算机可以解释包含在 HTML 元素中的原始文本”
- powershell - PowerShell 功能未按计划运行
- c# - 类库中的 C# 重构
- c# - Webservice 更多负载请求有待优化
- java - 类似于 JPanel 的 JFrame.pack() 的东西
- ssh - Pycharm 无法检测图形环境