aboutsummaryrefslogtreecommitdiffstats
path: root/bot/decorators.py
diff options
context:
space:
mode:
Diffstat (limited to 'bot/decorators.py')
-rw-r--r--bot/decorators.py22
1 files changed, 21 insertions, 1 deletions
diff --git a/bot/decorators.py b/bot/decorators.py
index 400f1bbb..efd43da0 100644
--- a/bot/decorators.py
+++ b/bot/decorators.py
@@ -12,7 +12,7 @@ from discord import Colour, Embed
from discord.ext import commands
from discord.ext.commands import CheckFailure, Context
-from bot.constants import ERROR_REPLIES, Month
+from bot.constants import Client, ERROR_REPLIES, Month
ONE_DAY = 24 * 60 * 60
@@ -261,3 +261,23 @@ def locked() -> typing.Union[typing.Callable, None]:
return await func(self, ctx, *args, **kwargs)
return inner
return wrap
+
+
+def mock_in_debug(return_value: typing.Any) -> typing.Callable:
+ """
+ Short-circuit function execution if in debug mode and return `return_value`.
+
+ The original function name, and the incoming args and kwargs are DEBUG level logged
+ upon each call. This is useful for expensive operations, i.e. media asset uploads
+ that are prone to rate-limits but need to be tested extensively.
+ """
+ def decorator(func: typing.Callable) -> typing.Callable:
+ @functools.wraps(func)
+ async def wrapped(*args, **kwargs) -> typing.Any:
+ """Short-circuit and log if in debug mode."""
+ if Client.debug:
+ log.debug(f"Function {func.__name__} called with args: {args}, kwargs: {kwargs}")
+ return return_value
+ return await func(*args, **kwargs)
+ return wrapped
+ return decorator