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 From fbe8e0a5bf4ffb4443a54588b7f55f25306eee6f Mon Sep 17 00:00:00 2001 From: vcokltfre Date: Mon, 12 Apr 2021 21:49:45 +0100 Subject: fix: display help for the correct command when an error occurs in timed --- bot/exts/evergreen/error_handler.py | 10 ++++++++-- bot/exts/evergreen/timed.py | 4 +++- 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'bot/exts/evergreen/timed.py') diff --git a/bot/exts/evergreen/error_handler.py b/bot/exts/evergreen/error_handler.py index 28902503..8db49748 100644 --- a/bot/exts/evergreen/error_handler.py +++ b/bot/exts/evergreen/error_handler.py @@ -46,6 +46,11 @@ class CommandErrorHandler(commands.Cog): logging.debug(f"Command {ctx.command} had its error already handled locally; ignoring.") return + parent_command = "" + if subctx := getattr(ctx, "subcontext", None): + parent_command = f"{ctx.command} " + ctx = subctx + error = getattr(error, 'original', error) logging.debug( f"Error Encountered: {type(error).__name__} - {str(error)}, " @@ -63,8 +68,9 @@ class CommandErrorHandler(commands.Cog): if isinstance(error, commands.UserInputError): self.revert_cooldown_counter(ctx.command, ctx.message) + usage = f"```{ctx.prefix}{parent_command}{ctx.command} {ctx.command.signature}```" embed = self.error_embed( - f"Your input was invalid: {error}\n\nUsage:\n```{ctx.prefix}{ctx.command} {ctx.command.signature}```" + f"Your input was invalid: {error}\n\nUsage:{usage}" ) await ctx.send(embed=embed) return @@ -95,7 +101,7 @@ class CommandErrorHandler(commands.Cog): self.revert_cooldown_counter(ctx.command, ctx.message) embed = self.error_embed( "The argument you provided was invalid: " - f"{error}\n\nUsage:\n```{ctx.prefix}{ctx.command} {ctx.command.signature}```" + f"{error}\n\nUsage:\n```{ctx.prefix}{parent_command}{ctx.command} {ctx.command.signature}```" ) await ctx.send(embed=embed) return diff --git a/bot/exts/evergreen/timed.py b/bot/exts/evergreen/timed.py index 635ccb32..5f177fd6 100644 --- a/bot/exts/evergreen/timed.py +++ b/bot/exts/evergreen/timed.py @@ -21,7 +21,9 @@ class TimedCommands(commands.Cog): """Time the command execution of a command.""" new_ctx = await self.create_execution_context(ctx, command) - if not new_ctx.command: + ctx.subcontext = new_ctx + + if not ctx.subcontext.command: 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." -- cgit v1.2.3