From 757b9bab218f1fded6bf645d0f49eda1c2ec43c0 Mon Sep 17 00:00:00 2001 From: Cromatin Date: Fri, 18 Oct 2024 13:46:28 +0200 Subject: [PATCH] Added a way to log execution of app command --- src/utils/logger/decorator.py | 11 ++++++ src/utils/singleton.py | 74 +++++++++++++++++------------------ 2 files changed, 48 insertions(+), 37 deletions(-) create mode 100644 src/utils/logger/decorator.py diff --git a/src/utils/logger/decorator.py b/src/utils/logger/decorator.py new file mode 100644 index 0000000..c48aa77 --- /dev/null +++ b/src/utils/logger/decorator.py @@ -0,0 +1,11 @@ +from functools import wraps +import discord + +def log_command_execution(func): + """Wraps an command function to print execution logs to the console""" + @wraps(func) + async def wrapper(self, interaction:discord.Interaction, *args, **kwargs): + self._logger.debug(f"User {interaction.user} in channel {interaction.channel} on guild {interaction.guild} executed the {func.__name__} command") + + return await func(self, interaction, *args, **kwargs) + return wrapper diff --git a/src/utils/singleton.py b/src/utils/singleton.py index 5eff5d8..63f90df 100644 --- a/src/utils/singleton.py +++ b/src/utils/singleton.py @@ -1,38 +1,38 @@ -class Singleton: - """ - A non-thread-safe helper class to ease implementing singletons. - This should be used as a decorator -- not a metaclass -- to the - class that should be a singleton. - - The decorated class can define one `__init__` function that - takes only the `self` argument. Also, the decorated class cannot be - inherited from. Other than that, there are no restrictions that apply - to the decorated class. - - To get the singleton instance, use the `instance` method. Trying - to use `__call__` will result in a `TypeError` being raised. - - """ - # Source: https://stackoverflow.com/questions/31875/is-there-a-simple-elegant-way-to-define-singletons - - def __init__(self, decorated): - self._decorated = decorated - - def instance(self): - """ - Returns the singleton instance. Upon its first call, it creates a - new instance of the decorated class and calls its `__init__` method. - On all subsequent calls, the already created instance is returned. - - """ - try: - return self._instance - except AttributeError: - self._instance = self._decorated() - return self._instance - - def __call__(self): - raise TypeError('Singletons must be accessed through `instance()`.') - - def __instancecheck__(self, inst): +class Singleton: + """ + A non-thread-safe helper class to ease implementing singletons. + This should be used as a decorator -- not a metaclass -- to the + class that should be a singleton. + + The decorated class can define one `__init__` function that + takes only the `self` argument. Also, the decorated class cannot be + inherited from. Other than that, there are no restrictions that apply + to the decorated class. + + To get the singleton instance, use the `instance` method. Trying + to use `__call__` will result in a `TypeError` being raised. + + """ + # Source: https://stackoverflow.com/questions/31875/is-there-a-simple-elegant-way-to-define-singletons + + def __init__(self, decorated): + self._decorated = decorated + + def instance(self): + """ + Returns the singleton instance. Upon its first call, it creates a + new instance of the decorated class and calls its `__init__` method. + On all subsequent calls, the already created instance is returned. + + """ + try: + return self._instance + except AttributeError: + self._instance = self._decorated() + return self._instance + + def __call__(self): + raise TypeError('Singletons must be accessed through `instance()`.') + + def __instancecheck__(self, inst): return isinstance(inst, self._decorated) \ No newline at end of file