Skip to content

Commit 1629ff9

Browse files
committed
Fixed args invoke attempt error
1 parent 1429bb2 commit 1629ff9

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

discord_slash/client.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -869,21 +869,7 @@ async def invoke_command(self, func, ctx, args):
869869
:param args: Args. Can be list or dict.
870870
"""
871871
try:
872-
not_kwargs = False
873-
if isinstance(args, dict):
874-
ctx.kwargs = args
875-
ctx.args = list(args.values())
876-
try:
877-
coro = func.invoke(ctx, **args)
878-
except TypeError:
879-
args = list(args.values())
880-
not_kwargs = True
881-
else:
882-
ctx.args = args
883-
not_kwargs = True
884-
if not_kwargs:
885-
coro = func.invoke(ctx, *args)
886-
await coro
872+
await func.invoke(ctx, args)
887873
except Exception as ex:
888874
await self.on_slash_command_error(ctx, ex)
889875

discord_slash/model.py

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,38 @@ def __init__(self, name, cmd): # Let's reuse old command formatting.
131131
if hasattr(self.func, '__commands_checks__'):
132132
self.__commands_checks__ = self.func.__commands_checks__
133133

134-
async def invoke(self, *args, **kwargs):
134+
async def invoke(self, *args):
135135
"""
136136
Invokes the command.
137137
138138
:param args: Args for the command.
139139
:raises: .error.CheckFailure
140140
"""
141-
can_run = await self.can_run(args[0])
141+
args = list(args)
142+
ctx = args.pop(0)
143+
can_run = await self.can_run(ctx)
142144
if not can_run:
143145
raise error.CheckFailure
144146

145-
return await self.func(*args, **kwargs)
147+
coro = None # Get rid of annoying IDE complainings.
148+
149+
not_kwargs = False
150+
if args and isinstance(args[0], dict):
151+
kwargs = args[0]
152+
ctx.kwargs = kwargs
153+
ctx.args = list(kwargs.values())
154+
try:
155+
coro = self.func(ctx, **kwargs)
156+
except TypeError:
157+
args = list(kwargs.values())
158+
not_kwargs = True
159+
else:
160+
ctx.args = args
161+
not_kwargs = True
162+
if not_kwargs:
163+
coro = self.func(ctx, *args)
164+
165+
return await coro
146166

147167
def add_check(self, func):
148168
"""
@@ -268,11 +288,31 @@ async def invoke(self, *args, **kwargs):
268288
:param args: Args for the command.
269289
:raises: .error.CheckFailure
270290
"""
271-
can_run = await self.can_run(args[0])
291+
args = list(args)
292+
ctx = args.pop(0)
293+
can_run = await self.can_run(ctx)
272294
if not can_run:
273295
raise error.CheckFailure
274296

275-
return await self.func(self.cog, *args, **kwargs)
297+
coro = None # Get rid of annoying IDE complainings.
298+
299+
not_kwargs = False
300+
if args and isinstance(args[0], dict):
301+
kwargs = args[0]
302+
ctx.kwargs = kwargs
303+
ctx.args = list(kwargs.values())
304+
try:
305+
coro = self.func(self.cog, ctx, **kwargs)
306+
except TypeError:
307+
args = list(kwargs.values())
308+
not_kwargs = True
309+
else:
310+
ctx.args = args
311+
not_kwargs = True
312+
if not_kwargs:
313+
coro = self.func(self.cog, ctx, *args)
314+
315+
return await coro
276316

277317

278318
class SlashCommandOptionType(IntEnum):

0 commit comments

Comments
 (0)