From 85116b28ceb3c29fcbda3000f3352f37f265d77b Mon Sep 17 00:00:00 2001 From: Isaac Beh Date: Thu, 7 Mar 2024 08:46:55 +1000 Subject: [PATCH] Added typing to command_utils.py --- pyproject.toml | 1 - uqcsbot/utils/command_utils.py | 12 +++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 2f225a04..b6c2175a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -52,7 +52,6 @@ exclude = [ "**/starboard.py", "**/uptime.py", "**/working_on.py", - "**/utils/command_utils.py", "**/utils/snailrace_utils.py", ] diff --git a/uqcsbot/utils/command_utils.py b/uqcsbot/utils/command_utils.py index b81bd74e..9be15258 100644 --- a/uqcsbot/utils/command_utils.py +++ b/uqcsbot/utils/command_utils.py @@ -1,7 +1,8 @@ from random import choice from functools import wraps -from typing import Callable +from typing import Callable, Any from discord.ext import commands +from uqcsbot.bot import UQCSBot LOADING_REACTS = ["⏰", "🕰️", "⏲️", "🕖", "🕔", "🕥"] HYPE_REACTS = [ @@ -16,12 +17,13 @@ ] -def loading_status(command_fn: Callable): +def loading_status(command_fn: Callable[..., Any]): @wraps(command_fn) # Important to preserve name because `command` uses it - async def wrapper(self, ctx: commands.Context, *args): - if ctx.message is None or ctx.bot is None: + # The use of Any in the following seems unavoidable. Using Any for *args is reasonable, as we want any type of arguments to be passed through. For self, it seems like the cleanest option at the moment: https://stackoverflow.com/a/69528375 + async def wrapper(self: Any, ctx: commands.Context[UQCSBot], *args: Any): + # Check for the bot user before reacting to ensure that a loading react is not left on a message + if ctx.bot.user is None: return - react = choice(LOADING_REACTS) await ctx.message.add_reaction(react) res = await command_fn(self, ctx, *args)