-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorators.py
More file actions
52 lines (38 loc) · 1.46 KB
/
decorators.py
File metadata and controls
52 lines (38 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import logging
from functools import wraps
from inspect import signature
from typing import Callable
from discord import Interaction
from discord.app_commands import describe
from database import execute_get
from embeds import error_embed
from utilities import DESCRIBED_PARAMETERS
def log_command(command: Callable) -> Callable:
@wraps(command)
async def wrapper(*args, **kwargs) -> None:
try:
interaction: Interaction = args[1]
except IndexError:
interaction = args[0]
channel_name: str = interaction.channel.name if interaction.guild else f'Direct Messages'
logging.debug(f'@{interaction.user.name} used the \"/{interaction.command.qualified_name}\" command in {channel_name}.')
await command(*args, **kwargs)
return wrapper
def limit_command(command: Callable) -> Callable:
@wraps(command)
async def wrapper(*args, **kwargs) -> None:
try:
interaction: Interaction = args[1]
except IndexError:
interaction = args[0]
if interaction.channel.id in [channel[0] for channel in await execute_get('SELECT channel_id FROM settings')]:
await command(*args, **kwargs)
else:
await interaction.response.send_message(embed=error_embed('You cannot use my commands in this channel!'), ephemeral=True)
return wrapper
def smart_describe() -> Callable:
def decorator(command: Callable) -> Callable:
return describe(**{
key: value for key, value in DESCRIBED_PARAMETERS.items() if key in signature(command).parameters
})(command)
return decorator