diff options
author | 2021-04-12 19:46:05 +0300 | |
---|---|---|
committer | 2021-04-12 19:46:05 +0300 | |
commit | 0c13ace89b071474416ffea5479a850791ad5f5f (patch) | |
tree | 4309df4ef4a8164c8cf4a5433e5c2dfdce639240 | |
parent | Merge remote-tracking branch 'origin/feature/latex' into feature/latex (diff) | |
parent | Merge pull request #672 from python-discord/feature/timed-execution (diff) |
Merge branch 'main' into feature/latex
-rw-r--r-- | bot/constants.py | 2 | ||||
-rw-r--r-- | bot/exts/evergreen/timed.py | 44 | ||||
-rw-r--r-- | bot/resources/evergreen/starter.yaml | 1 |
3 files changed, 45 insertions, 2 deletions
diff --git a/bot/constants.py b/bot/constants.py index 416dd0e7..1d35b3f1 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -153,7 +153,7 @@ class Emojis: christmas_tree = "\U0001F384" check = "\u2611" envelope = "\U0001F4E8" - trashcan = "<:trashcan:637136429717389331>" + trashcan = environ.get("TRASHCAN_EMOJI", "<:trashcan:637136429717389331>") ok_hand = ":ok_hand:" hand_raised = "\U0001f64b" diff --git a/bot/exts/evergreen/timed.py b/bot/exts/evergreen/timed.py new file mode 100644 index 00000000..635ccb32 --- /dev/null +++ b/bot/exts/evergreen/timed.py @@ -0,0 +1,44 @@ +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.""" + + @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.content = f"{ctx.prefix}{command}" + + return await ctx.bot.get_context(msg) + + @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) + + if not new_ctx.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." + + await ctx.send(error) + return + + if new_ctx.command.qualified_name == "timed": + 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) + 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)) diff --git a/bot/resources/evergreen/starter.yaml b/bot/resources/evergreen/starter.yaml index 4fec6a10..6b0de0ef 100644 --- a/bot/resources/evergreen/starter.yaml +++ b/bot/resources/evergreen/starter.yaml @@ -6,7 +6,6 @@ - "What is better: Milk, Dark or White chocolate?" - What is your favourite holiday? - If you could have any superpower, what would it be? -- Name one thing you like about a person to your right. - If you could be anyone else for one day, who would it be? - What Easter tradition do you enjoy most? - What is the best gift you've been given? |