discord.py - 用于检查命令性能的命令
问题描述
我有一个命令来检查另一个命令的性能,该命令返回诸如命令运行多长时间以及其中是否发生错误之类的信息。但这仅适用于没有权限限制的命令,例如拥有管理员权限。
如何解决此问题,以便绕过将检查其性能的命令的权限限制?我目前拥有的代码是:
@commands.command(hidden=True)
@is_owner()
async def perf(self, ctx, *, command):
await asyncio.sleep(0.25)
await ctx.message.delete()
"""Checks the timing of a command, attempting to suppress HTTP and DB calls."""
msg = copy.copy(ctx.message)
msg.content = ctx.prefix + command
new_ctx = await self.bot.get_context(msg, cls=type(ctx))
new_ctx._db = PerformanceMocker()
# Intercepts the Messageable interface a bit
new_ctx._state = PerformanceMocker()
new_ctx.channel = PerformanceMocker()
new_ctx.author = ctx.author
if new_ctx.command is None:
return await ctx.send('No command found')
print(new_ctx.content)
print(new_ctx.author.permissions)
start = time.perf_counter()
try:
await new_ctx.command.invoke(new_ctx)
except commands.CommandError:
end = time.perf_counter()
success = False
try:
await ctx.send(f'```py\n{traceback.format_exc()}\n```')
except discord.HTTPException:
pass
else:
end = time.perf_counter()
success = True
await ctx.send(f'Status: {success} Time: {(end - start) * 1000:.2f}ms')
解决方案
我建议检查jishaku,它有一个内置的调试命令,可以输出任何错误和总时间。
要直接回答您的问题,您应该查看commands.Command.__call__它将绕过所有检查、转换器和冷却。
推荐阅读
- python - 如何在每次循环后更改其名称的变量?
- python - 如何拆分火花数据框列字符串?
- c++ - 为什么 C++ 范围“变换 - > 过滤器”为匹配过滤器谓词的值调用变换两次?
- git - 如何在 MacOS 上安装 Unity 2D Extras?
- reactjs - 既然 tsconfig.json 中的 compilerpaths.path 已被 create-react-app 删除,如何在 create-react-app 中使用 typeorm shim?
- c# - WPF:CroppedBitmap 的图像控件,因为 BitmapImage 仍然是未裁剪的图像(转换问题?)
- java - 在订阅方法之前结束方面调用
- r - 拆分R中不同列中的值
- python - Python Selenium - 在页面上找不到元素
- c++ - 如何跳过/忽略cin输入