@@ -12,6 +12,11 @@ class SlashCommand:
12
12
13
13
:param client: discord.py Bot class. Although it accepts :class:`discord.Client` at init, using it is not allowed since :class:`discord.Client` doesn't support :func:`add_listener`.
14
14
:type client: Union[discord.Client, discord.ext.commands.Bot]
15
+
16
+ :ivar _discord: Discord client of this client.
17
+ :ivar commands: Dictionary of the registered commands via :func:`.slash` decorator.
18
+ :ivar req: :class:`.http.SlashCommandRequest` of this client.
19
+ :ivar logger: Logger of this client.
15
20
"""
16
21
def __init__ (self ,
17
22
client : typing .Union [discord .Client ,
@@ -21,18 +26,38 @@ def __init__(self,
21
26
raise Exception ("Currently only commands.Bot is supported." )
22
27
self ._discord = client
23
28
self .commands = {}
24
- self .http = http .SlashCommandRequest ()
29
+ self .req = http .SlashCommandRequest ()
25
30
self .logger = logging .getLogger ("discord_slash" )
26
31
self ._discord .add_listener (self .on_socket_response )
27
32
28
- def slash (self , name = None ):
33
+ def slash (self , name = None , auto_convert : dict = None ):
29
34
"""
30
35
Decorator that registers coroutine as a slash command.\n
31
- 1 arg is required for ctx(:func:`discord_slash.model.SlashContext`), and if your slash command has some args, then those args are also required.\n
32
- All args are passed in order.\n
33
- Note that Role, User, and Channel types are passed as id, since API doesn't give type of the option for now.
36
+ 1 arg is required for ctx(:class:`.model.SlashContext`), and if your slash command has some args, then those args are also required.\n
37
+ All args are passed in order.
38
+
39
+ .. note::
40
+ Role, User, and Channel types are passed as id, since API doesn't give type of the option for now.
41
+
42
+ .. warning::
43
+ Unlike discord.py's command, ``*args``, keyword-only args, converters, etc. are NOT supported.
44
+
45
+ Example:
46
+
47
+ .. code-block:: python
48
+
49
+ @slash.slash(name="ping")
50
+ async def _slash(ctx): # Normal usage.
51
+ await ctx.send(text=f"Pong! (`{round(bot.latency*1000)}`ms)")
52
+
53
+
54
+ @slash.slash(name="pick")
55
+ async def _pick(ctx, choice1, choice2): # Command with 1 or more args.
56
+ await ctx.send(text=str(random.choice([choice1, choice2])))
34
57
35
58
:param name: Name of the slash command.
59
+ :param auto_convert: Not implemented.
60
+ :type auto_convert: dict
36
61
"""
37
62
def wrapper (cmd ):
38
63
self .commands [cmd .__name__ if not name else name ] = cmd
@@ -57,8 +82,10 @@ def process_options(self, options: dict) -> list:
57
82
58
83
async def on_socket_response (self , msg ):
59
84
"""
60
- This event listener is automatically registered at initialization of this class.\n
61
- DO NOT MANUALLY REGISTER, OVERRIDE, OR WHATEVER ACTION TO THIS COROUTINE UNLESS YOU KNOW WHAT YOU ARE DOING.
85
+ This event listener is automatically registered at initialization of this class.
86
+
87
+ .. warning::
88
+ DO NOT MANUALLY REGISTER, OVERRIDE, OR WHATEVER ACTION TO THIS COROUTINE UNLESS YOU KNOW WHAT YOU ARE DOING.
62
89
63
90
:param msg: Gateway message.
64
91
"""
@@ -67,5 +94,5 @@ async def on_socket_response(self, msg):
67
94
to_use = msg ["d" ]
68
95
if to_use ["data" ]["name" ] in self .commands .keys ():
69
96
args = [x ["value" ] for x in to_use ["data" ]["options" ]] if "options" in to_use ["data" ] else []
70
- ctx = model .SlashContext (self .http , to_use , self ._discord )
97
+ ctx = model .SlashContext (self .req , to_use , self ._discord )
71
98
await self .commands [to_use ["data" ]["name" ]](ctx , * args )
0 commit comments