From 0e9ea17010310c7d63d9b0fbba9e3a441cf2c1b5 Mon Sep 17 00:00:00 2001 From: vcokltfre Date: Sat, 10 Apr 2021 05:56:22 +0100 Subject: add timed command for timed execution of commands --- bot/exts/evergreen/timed.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 bot/exts/evergreen/timed.py (limited to 'bot/exts/evergreen/timed.py') 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)) -- cgit v1.2.3 From d7263238b68a3d55f47ec944cc9e85450e2800b6 Mon Sep 17 00:00:00 2001 From: vcokltfre Date: Sat, 10 Apr 2021 21:48:26 +0100 Subject: removed unused cog init and fixed timing timed command --- bot/exts/evergreen/timed.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'bot/exts/evergreen/timed.py') diff --git a/bot/exts/evergreen/timed.py b/bot/exts/evergreen/timed.py index 0537141b..58da3dc8 100644 --- a/bot/exts/evergreen/timed.py +++ b/bot/exts/evergreen/timed.py @@ -8,9 +8,6 @@ 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.""" @@ -24,6 +21,9 @@ class TimedCommands(commands.Cog): """Time the command execution of a command.""" new_ctx = await self.create_execution_context(ctx, command) + if new_ctx.command and new_ctx.command.qualified_name == "timed": + return await ctx.send("You are not allowed to time the execution of the `timed` command.") + t_start = perf_counter() await new_ctx.command.invoke(new_ctx) t_end = perf_counter() -- cgit v1.2.3 From bf1cd68efaa7f0fe5037c69fbdc7cfffcf284a2d Mon Sep 17 00:00:00 2001 From: vcokltfre Date: Sun, 11 Apr 2021 01:11:21 +0100 Subject: fix message updating and return types --- bot/exts/evergreen/timed.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'bot/exts/evergreen/timed.py') diff --git a/bot/exts/evergreen/timed.py b/bot/exts/evergreen/timed.py index 58da3dc8..57575993 100644 --- a/bot/exts/evergreen/timed.py +++ b/bot/exts/evergreen/timed.py @@ -12,7 +12,7 @@ class TimedCommands(commands.Cog): 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}"}) + msg.content = f"{ctx.prefix}{command}" return await ctx.bot.get_context(msg) @@ -22,7 +22,8 @@ class TimedCommands(commands.Cog): new_ctx = await self.create_execution_context(ctx, command) if new_ctx.command and new_ctx.command.qualified_name == "timed": - return await ctx.send("You are not allowed to time the execution of the `timed` command.") + await ctx.send("You are not allowed to time the execution of the `timed` command.") + return t_start = perf_counter() await new_ctx.command.invoke(new_ctx) -- cgit v1.2.3 From b0892e5afbbb13167c7c295c8f8241a2146eddf7 Mon Sep 17 00:00:00 2001 From: vcokltfre Date: Sun, 11 Apr 2021 04:03:59 +0100 Subject: return and send message if command isnt found while timing --- bot/exts/evergreen/timed.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'bot/exts/evergreen/timed.py') diff --git a/bot/exts/evergreen/timed.py b/bot/exts/evergreen/timed.py index 57575993..a0f29838 100644 --- a/bot/exts/evergreen/timed.py +++ b/bot/exts/evergreen/timed.py @@ -21,7 +21,11 @@ class TimedCommands(commands.Cog): """Time the command execution of a command.""" new_ctx = await self.create_execution_context(ctx, command) - if new_ctx.command and new_ctx.command.qualified_name == "timed": + if not new_ctx.command: + await ctx.send("The command you are trying to time doesn't exist. Use `.help` for a list of commands.") + return + + if new_ctx.command.qualified_name == "timed": await ctx.send("You are not allowed to time the execution of the `timed` command.") return -- cgit v1.2.3 From 7c7c6dd2318815bc14e1cb20d5dd5fbd416712a0 Mon Sep 17 00:00:00 2001 From: vcokltfre Date: Sun, 11 Apr 2021 04:19:58 +0100 Subject: fix: use ctx.prefix when suggesting the help command --- bot/exts/evergreen/timed.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'bot/exts/evergreen/timed.py') diff --git a/bot/exts/evergreen/timed.py b/bot/exts/evergreen/timed.py index a0f29838..604204b0 100644 --- a/bot/exts/evergreen/timed.py +++ b/bot/exts/evergreen/timed.py @@ -22,7 +22,10 @@ class TimedCommands(commands.Cog): new_ctx = await self.create_execution_context(ctx, command) if not new_ctx.command: - await ctx.send("The command you are trying to time doesn't exist. Use `.help` for a list of commands.") + help_command = f"{ctx.prefix}help" + error = f"The command you are trying to time doesn't exist. Use `{help_command}` for a list of commands." + + await ctx.send(error) return if new_ctx.command.qualified_name == "timed": -- cgit v1.2.3 From 652e306064efb451660e7c08dc88aeafdcf7360e Mon Sep 17 00:00:00 2001 From: vcokltfre Date: Sun, 11 Apr 2021 19:12:10 +0100 Subject: chore: add time as alias of timed Co-authored-by: Matteo Bertucci --- bot/exts/evergreen/timed.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/evergreen/timed.py') diff --git a/bot/exts/evergreen/timed.py b/bot/exts/evergreen/timed.py index 604204b0..635ccb32 100644 --- a/bot/exts/evergreen/timed.py +++ b/bot/exts/evergreen/timed.py @@ -16,7 +16,7 @@ class TimedCommands(commands.Cog): return await ctx.bot.get_context(msg) - @commands.command(name="timed", aliases=["t"]) + @commands.command(name="timed", aliases=["time", "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) -- cgit v1.2.3