diff options
author | 2021-04-10 05:56:22 +0100 | |
---|---|---|
committer | 2021-04-10 05:56:22 +0100 | |
commit | 0e9ea17010310c7d63d9b0fbba9e3a441cf2c1b5 (patch) | |
tree | dcd00c2a68889f42451b814487b75878cbe14fb3 /bot | |
parent | Merge pull request #666 from onerandomusername/remove-topic (diff) |
add timed command for timed execution of commands
Diffstat (limited to 'bot')
-rw-r--r-- | bot/exts/evergreen/timed.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/bot/exts/evergreen/timed.py b/bot/exts/evergreen/timed.py new file mode 100644 index 00000000..0537141b --- /dev/null +++ b/bot/exts/evergreen/timed.py @@ -0,0 +1,36 @@ +from copy import copy +from time import perf_counter + +from discord import Message +from discord.ext import commands + + +class TimedCommands(commands.Cog): + """Time the command execution of a command.""" + + def __init__(self, bot: commands.Bot) -> None: + self.bot = bot + + @staticmethod + async def create_execution_context(ctx: commands.Context, command: str) -> commands.Context: + """Get a new execution context for a command.""" + msg: Message = copy(ctx.message) + msg._update({"content": f"{ctx.prefix}{command}"}) + + return await ctx.bot.get_context(msg) + + @commands.command(name="timed", aliases=["t"]) + async def timed(self, ctx: commands.Context, *, command: str) -> None: + """Time the command execution of a command.""" + new_ctx = await self.create_execution_context(ctx, command) + + t_start = perf_counter() + await new_ctx.command.invoke(new_ctx) + t_end = perf_counter() + + await ctx.send(f"Command execution for `{new_ctx.command}` finished in {(t_end - t_start):.4f} seconds.") + + +def setup(bot: commands.Bot) -> None: + """Cog load.""" + bot.add_cog(TimedCommands(bot)) |