Skip to content

Commit 44a972d

Browse files
committed
Properly implemented kwargs option pass
1 parent b085121 commit 44a972d

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

discord_slash/client.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def __init__(self,
4949
self._discord.loop.create_task(self.register_all_commands())
5050
elif self.auto_delete:
5151
self._discord.loop.create_task(self.delete_unused_commands())
52-
52+
5353
if not isinstance(client, commands.Bot) and not isinstance(client,
5454
commands.AutoShardedBot) and not override_type:
5555
self.logger.info("Detected discord.Client! Overriding on_socket_response.")
@@ -60,21 +60,25 @@ def __init__(self,
6060
self._discord.slash = self
6161
else:
6262
raise error.DuplicateSlashClient("You can't have duplicate SlashCommand instances!")
63-
63+
6464
self._discord.add_listener(self.on_socket_response)
6565
self.has_listener = True
6666
default_add_function = self._discord.add_cog
67+
6768
def override_add_cog(cog: commands.Cog):
6869
default_add_function(cog)
6970
self.get_cog_commands(cog)
71+
7072
self._discord.add_cog = override_add_cog
7173
default_remove_function = self._discord.remove_cog
74+
7275
def override_remove_cog(name: str):
7376
cog = self._discord.get_cog(name)
7477
if cog is None:
7578
return
7679
self.remove_cog_commands(cog)
7780
default_remove_function(name)
81+
7882
self._discord.remove_cog = override_remove_cog
7983

8084
def get_cog_commands(self, cog: commands.Cog):
@@ -87,10 +91,10 @@ def get_cog_commands(self, cog: commands.Cog):
8791
:param cog: Cog that has slash commands.
8892
:type cog: discord.ext.commands.Cog
8993
"""
90-
if hasattr(cog, '_slash_registered'): # Temporary warning
94+
if hasattr(cog, '_slash_registered'): # Temporary warning
9195
return self.logger.warning("Calling get_cog_commands is no longer required "
9296
"to add cog slash commands. Make sure to remove all calls to this function.")
93-
cog._slash_registered = True # Assuming all went well
97+
cog._slash_registered = True # Assuming all went well
9498
func_list = [getattr(cog, x) for x in dir(cog)]
9599
res = [x for x in func_list if isinstance(x, (model.CogCommandObject, model.CogSubcommandObject))]
96100
for x in res:
@@ -197,7 +201,7 @@ async def to_dict(self):
197201
# so we will warn this at registering subcommands.
198202
self.logger.warning(f"Detected command name with same subcommand base name! "
199203
f"This command will only have subcommand: {x}")
200-
204+
201205
options = []
202206
if selected.has_subcommands:
203207
tgt = self.subcommands[x]
@@ -247,7 +251,7 @@ async def to_dict(self):
247251

248252
return commands
249253

250-
async def sync_all_commands(self, delete_from_unused_guilds = True):
254+
async def sync_all_commands(self, delete_from_unused_guilds=True):
251255
"""
252256
Matches commands registered on Discord to commands registered here.
253257
Deletes any commands on Discord but not here, and registers any not on Discord.
@@ -262,15 +266,15 @@ async def sync_all_commands(self, delete_from_unused_guilds = True):
262266
# This is an extremly bad way to do this, because slash cmds can be in guilds the bot isn't in
263267
# But it's the only way until discord makes an endpoint to request all the guild with cmds registered.
264268

265-
await self.req.put_slash_commands(slash_commands = commands["global"], guild_id = None)
266-
269+
await self.req.put_slash_commands(slash_commands=commands["global"], guild_id=None)
270+
267271
for guild in commands["guild"]:
268-
await self.req.put_slash_commands(slash_commands = commands["guild"][guild], guild_id = guild)
272+
await self.req.put_slash_commands(slash_commands=commands["guild"][guild], guild_id=guild)
269273
all_bot_guilds.remove(guild)
270274
if delete_from_unused_guilds:
271275
for guild in all_bot_guilds:
272-
await self.req.put_slash_commands(slash_commands=[], guild_id = guild)
273-
276+
await self.req.put_slash_commands(slash_commands=[], guild_id=guild)
277+
274278
self.logger.info("Completed syncing all commands!")
275279

276280
async def register_all_commands(self):
@@ -283,14 +287,14 @@ async def register_all_commands(self):
283287
for command in commands["global"]:
284288
name = command.pop('name')
285289
self.logger.debug(f"Registering global command {name}")
286-
await self.req.add_slash_command(guild_id = None, cmd_name = name, **command)
287-
290+
await self.req.add_slash_command(guild_id=None, cmd_name=name, **command)
291+
288292
for guild in commands["guild"]:
289293
guild_cmds = commands["guild"][guild]
290294
for command in guild_cmds:
291295
name = command.pop('name')
292296
self.logger.debug(f"Registering guild command {name} in guild: {guild}")
293-
await self.req.add_slash_command(guild_id = guild, cmd_name = name, **command)
297+
await self.req.add_slash_command(guild_id=guild, cmd_name=name, **command)
294298
self.logger.info("Completed registering all commands!")
295299

296300
async def delete_unused_commands(self):
@@ -660,12 +664,12 @@ async def process_options(self, guild: discord.Guild, options: list, auto_conver
660664
for x in options:
661665
selected = x
662666
if selected["name"] in auto_convert:
663-
processed = None # This isn't the best way, but we should to reduce duplicate lines.
667+
processed = None # This isn't the best way, but we should to reduce duplicate lines.
664668
if auto_convert[selected["name"]] not in types:
665669
processed = selected["value"]
666670
else:
667671
loaded_converter = converters[types[auto_convert[selected["name"]]]]
668-
if isinstance(loaded_converter, list): # For user type.
672+
if isinstance(loaded_converter, list): # For user type.
669673
cache_first = loaded_converter[0](int(selected["value"]))
670674
if cache_first:
671675
processed = cache_first
@@ -676,7 +680,7 @@ async def process_options(self, guild: discord.Guild, options: list, auto_conver
676680
processed = await loaded_converter(int(selected["value"])) \
677681
if iscoroutinefunction(loaded_converter) else \
678682
loaded_converter(int(selected["value"]))
679-
except (discord.Forbidden, discord.HTTPException, discord.NotFound): # Just in case.
683+
except (discord.Forbidden, discord.HTTPException, discord.NotFound): # Just in case.
680684
self.logger.warning("Failed fetching discord object! Passing ID instead.")
681685
processed = int(selected["value"])
682686
to_return[selected["name"]] = processed

discord_slash/model.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __init__(self, name, cmd): # Let's reuse old command formatting.
3535
# add checks at this var.
3636
self.__commands_checks__ = []
3737

38-
async def invoke(self, *args):
38+
async def invoke(self, *args, **kwargs):
3939
"""
4040
Invokes the command.
4141
@@ -46,7 +46,7 @@ async def invoke(self, *args):
4646
if not can_run:
4747
raise error.CheckFailure
4848

49-
return await self.func(*args)
49+
return await self.func(*args, **kwargs)
5050

5151
def add_check(self, func):
5252
"""
@@ -115,7 +115,7 @@ def __init__(self, *args):
115115
super().__init__(*args)
116116
self.cog = None # Manually set this later.
117117

118-
async def invoke(self, *args):
118+
async def invoke(self, *args, **kwargs):
119119
"""
120120
Invokes the command.
121121
@@ -126,7 +126,7 @@ async def invoke(self, *args):
126126
if not can_run:
127127
raise error.CheckFailure
128128

129-
return await self.func(self.cog, *args)
129+
return await self.func(self.cog, *args, **kwargs)
130130

131131

132132
class CogSubcommandObject(SubcommandObject):
@@ -140,7 +140,7 @@ def __init__(self, *args):
140140
super().__init__(*args)
141141
self.cog = None # Manually set this later.
142142

143-
async def invoke(self, *args):
143+
async def invoke(self, *args, **kwargs):
144144
"""
145145
Invokes the command.
146146
@@ -151,7 +151,7 @@ async def invoke(self, *args):
151151
if not can_run:
152152
raise error.CheckFailure
153153

154-
return await self.func(self.cog, *args)
154+
return await self.func(self.cog, *args, **kwargs)
155155

156156

157157
class SlashCommandOptionType(IntEnum):

0 commit comments

Comments
 (0)