首页 > 解决方案 > 用于检查命令性能的命令

问题描述

我有一个命令来检查另一个命令的性能,该命令返回诸如命令运行多长时间以及其中是否发生错误之类的信息。但这仅适用于没有权限限制的命令,例如拥有管理员权限。

如何解决此问题,以便绕过将检查其性能的命令的权限限制?我目前拥有的代码是:

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

标签: discord.py

解决方案


我建议检查jishaku,它有一个内置的调试命令,可以输出任何错误和总时间。

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


推荐阅读