命令以检查命令的性能

  • 本文关键字:命令 性能 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'```pyn{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,它有一个内置的调试命令,可以输出任何错误和所用的总时间。

要直接回答您的问题,您应该查看命令。命令__call__,它将绕过所有检查、转换器和冷却。

最新更新