@@ -27,9 +27,12 @@ def __init__(self,
27
27
auto_register : bool = False ):
28
28
self ._discord = client
29
29
self .commands = {}
30
+ self .subcommands = {}
30
31
self .req = http .SlashCommandRequest ()
31
32
self .logger = logging .getLogger ("discord_slash" )
32
33
self .auto_register = auto_register
34
+ if self .auto_register :
35
+ self .logger .warning ("auto_register is NOT implemented! Please manually add commands to Discord API." )
33
36
if not isinstance (client , commands .Bot ):
34
37
self .logger .info ("Detected discord.Client! Overriding on_socket_response." )
35
38
self ._discord .on_socket_response = self .on_socket_response
@@ -41,7 +44,7 @@ def add_slash_command(self,
41
44
name : str = None ,
42
45
description : str = None ,
43
46
auto_convert : dict = None ,
44
- guild_id : int = None ,
47
+ guild_ids : list = None ,
45
48
options : list = None ,
46
49
has_subcommands : bool = False ):
47
50
"""
@@ -51,7 +54,7 @@ def add_slash_command(self,
51
54
:param name: Name of the slash command. Default name of the coroutine.
52
55
:param description: Description of the slash command. Default ``None``.
53
56
:param auto_convert: Dictionary of how to convert option values. Default ``None``.
54
- :param guild_id: Guild ID of where the command will be used. Default ``None``, which will be global command.
57
+ :param guild_ids: List of Guild ID of where the command will be used. Default ``None``, which will be global command.
55
58
:param options: Options of the slash command. This will affect ``auto_convert`` and command data at Discord API. Default ``None``.
56
59
:param has_subcommands: Whether it has subcommand. Default ``False``.
57
60
:return: ``None``
@@ -60,22 +63,62 @@ def add_slash_command(self,
60
63
"func" : cmd ,
61
64
"description" : description ,
62
65
"auto_convert" : auto_convert ,
63
- "guild_id " : guild_id ,
66
+ "guild_ids " : guild_ids ,
64
67
"api_options" : options ,
65
68
"has_subcommands" : has_subcommands
66
69
}
67
70
self .commands [cmd .__name__ if not name else name ] = _cmd
68
71
self .logger .debug (f"Added command `{ cmd .__name__ if not name else name } `" )
69
72
70
- def add_subcommand (self ,):
71
- pass
73
+ def add_subcommand (self ,
74
+ cmd ,
75
+ base ,
76
+ subcommand_group = None ,
77
+ name = None ,
78
+ description : str = None ,
79
+ auto_convert : dict = None ,
80
+ guild_ids : list = None ):
81
+ """
82
+ Registers subcommand to SlashCommand.
83
+
84
+ :param cmd:
85
+ :param base:
86
+ :param subcommand_group:
87
+ :param name:
88
+ :param description:
89
+ :param auto_convert:
90
+ :param guild_ids:
91
+ :return:
92
+ """
93
+ name = cmd .__name__ if not name else name
94
+ _cmd = {
95
+ "guild_ids" : guild_ids ,
96
+ "has_subcommands" : True
97
+ }
98
+ _sub = {
99
+ "func" : cmd ,
100
+ "name" : name ,
101
+ "description" : description ,
102
+ "auto_convert" : auto_convert ,
103
+ "guild_ids" : guild_ids ,
104
+ }
105
+ self .commands [base ] = _cmd
106
+ if base not in self .subcommands .keys ():
107
+ self .subcommands [base ] = {}
108
+ if subcommand_group :
109
+ if subcommand_group not in self .subcommands [base ].keys ():
110
+ self .subcommands [base ][subcommand_group ] = {}
111
+ self .subcommands [base ][subcommand_group ][name ] = _sub
112
+ else :
113
+ self .subcommands [base ][name ] = _sub
72
114
73
115
def slash (self ,
74
116
* ,
75
117
name : str = None ,
76
118
description : str = None ,
77
119
auto_convert : dict = None ,
78
120
guild_id : int = None ,
121
+ guild_ids : list = None ,
79
122
options : list = None ):
80
123
"""
81
124
Decorator that registers coroutine as a slash command.\n
@@ -115,9 +158,15 @@ async def _pick(ctx, choice1, choice2): # Command with 1 or more args.
115
158
:param name: Name of the slash command. Default name of the coroutine.
116
159
:param description: Description of the slash command. Default ``None``.
117
160
:param auto_convert: Dictionary of how to convert option values. Default ``None``.
118
- :param guild_id: Guild ID of where the command will be used. Default ``None``, which will be global command.
161
+ :param guild_id: Deprecated. Use ``guild_ids`` instead.
162
+ :param guild_ids: Guild ID of where the command will be used. Default ``None``, which will be global command.
119
163
:param options: Options of the slash command. This will affect ``auto_convert`` and command data at Discord API. Default ``None``.
120
164
"""
165
+
166
+ if guild_id :
167
+ self .logger .warning ("`guild_id` is deprecated! `Use guild_ids` instead." )
168
+ guild_ids = [guild_id ]
169
+
121
170
if options :
122
171
# Overrides original auto_convert.
123
172
auto_convert = {}
@@ -127,7 +176,7 @@ async def _pick(ctx, choice1, choice2): # Command with 1 or more args.
127
176
auto_convert [x ["name" ]] = x ["type" ]
128
177
129
178
def wrapper (cmd ):
130
- self .add_slash_command (cmd , name , description , auto_convert , guild_id , options )
179
+ self .add_slash_command (cmd , name , description , auto_convert , guild_ids , options )
131
180
return cmd
132
181
return wrapper
133
182
@@ -138,7 +187,7 @@ def subcommand(self,
138
187
name = None ,
139
188
description : str = None ,
140
189
auto_convert : dict = None ,
141
- guild_id : int = None ):
190
+ guild_ids : int = None ):
142
191
"""
143
192
Decorator that registers subcommand.
144
193
Unlike discord.py, you don't need base command.
@@ -159,7 +208,7 @@ async def _group_say(ctx, _str):
159
208
:param name: Name of the subcommand. Default name of the coroutine.
160
209
:param description: Description of the subcommand. Default ``None``.
161
210
:param auto_convert: Dictionary of how to convert option values. Default ``None``.
162
- :param guild_id: Guild ID of where the command will be used. Default ``None``, which will be global command.
211
+ :param guild_ids: List of guild ID of where the command will be used. Default ``None``, which will be global command.
163
212
:return:
164
213
"""
165
214
def wrapper (cmd ):
@@ -220,25 +269,24 @@ async def on_socket_response(self, msg):
220
269
if msg ["t" ] != "INTERACTION_CREATE" :
221
270
return
222
271
to_use = msg ["d" ]
223
- print (to_use )
224
272
if to_use ["data" ]["name" ] in self .commands .keys ():
225
273
selected_cmd = self .commands [to_use ["data" ]["name" ]]
226
274
ctx = model .SlashContext (self .req , to_use , self ._discord )
227
- if selected_cmd ["guild_id " ]:
228
- if selected_cmd [ "guild_id" ] != ctx .guild .id :
275
+ if selected_cmd ["guild_ids " ]:
276
+ if ctx .guild .id not in selected_cmd [ "guild_ids" ] :
229
277
return
230
278
if selected_cmd ["has_subcommands" ]:
231
- return await self .handle_subcommand (to_use )
279
+ return await self .handle_subcommand (ctx , to_use )
232
280
args = self .process_options (ctx .guild , to_use ["data" ]["options" ], selected_cmd ["auto_convert" ]) \
233
281
if "options" in to_use ["data" ] else []
234
282
self .logger .debug (f"Command { to_use ['data' ]['name' ]} invoked." )
235
283
await selected_cmd ["func" ](ctx , * args )
236
284
237
- async def handle_subcommand (self , data : dict ):
285
+ async def handle_subcommand (self , ctx : model . SlashContext , data : dict ):
238
286
"""
239
287
Coroutine for handling subcommand.
240
288
289
+ :param ctx:
241
290
:param data:
242
- :return:
243
291
"""
244
292
pass
0 commit comments