Skip to content

Commit c25e2a2

Browse files
authored
Merge pull request #398 from Apocryphon-X/master
Fix for discord.User parsing
2 parents 031b964 + 712584b commit c25e2a2

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

discord/commands/commands.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
)
6363

6464
if TYPE_CHECKING:
65-
pass
65+
from ..interactions import Interaction
6666

6767
def wrap_callback(coro):
6868
@functools.wraps(coro)
@@ -99,7 +99,7 @@ class _BaseCommand:
9999

100100
class ApplicationCommand(_BaseCommand):
101101
cog = None
102-
102+
103103
def __repr__(self):
104104
return f"<discord.commands.{self.__class__.__name__} name={self.name}>"
105105

@@ -143,8 +143,8 @@ async def can_run(self, ctx: ApplicationContext) -> bool:
143143
# since we have no checks, then we just return True.
144144
return True
145145

146-
return await async_all(predicate(ctx) for predicate in predicates) # type: ignore
147-
146+
return await async_all(predicate(ctx) for predicate in predicates) # type: ignore
147+
148148
async def dispatch_error(self, ctx: ApplicationContext, error: Exception) -> None:
149149
ctx.command_failed = True
150150
cog = self.cog
@@ -349,7 +349,7 @@ class SlashCommand(ApplicationCommand):
349349
350350
.. note::
351351
352-
If this is not empty then default_permissions will be set to False.
352+
If this is not empty then default_permissions will be set to False.
353353
354354
cog: Optional[:class:`Cog`]
355355
The cog that this command belongs to. ``None`` if there isn't one.
@@ -515,8 +515,13 @@ async def _invoke(self, ctx: ApplicationContext) -> None:
515515
<= op.input_type.value
516516
<= SlashCommandOptionType.role.value
517517
):
518-
name = "member" if op.input_type.name == "user" else op.input_type.name
519-
arg = await get_or_fetch(ctx.guild, name, int(arg), default=int(arg))
518+
if ctx.guild is None and op.input_type.name == "user":
519+
_data = ctx.interaction.data["resolved"]["users"][arg]
520+
_data["id"] = int(arg)
521+
arg = User(state=ctx.interaction._state, data=_data)
522+
else:
523+
name = "member" if op.input_type.name == "user" else op.input_type.name
524+
arg = await get_or_fetch(ctx.guild, name, int(arg), default=int(arg))
520525

521526
elif op.input_type == SlashCommandOptionType.mentionable:
522527
arg_id = int(arg)
@@ -532,7 +537,7 @@ async def _invoke(self, ctx: ApplicationContext) -> None:
532537
for o in self.options:
533538
if o._parameter_name not in kwargs:
534539
kwargs[o._parameter_name] = o.default
535-
540+
536541
if self.cog is not None:
537542
await self.callback(self.cog, ctx, **kwargs)
538543
elif self.parent is not None and self.attached_to_group is True:
@@ -542,12 +547,12 @@ async def _invoke(self, ctx: ApplicationContext) -> None:
542547

543548
async def invoke_autocomplete_callback(self, ctx: AutocompleteContext):
544549
values = { i.name: i.default for i in self.options }
545-
550+
546551
for op in ctx.interaction.data.get("options", []):
547552
if op.get("focused", False):
548553
option = find(lambda o: o.name == op["name"], self.options)
549554
values.update({
550-
i["name"]:i["value"]
555+
i["name"]:i["value"]
551556
for i in ctx.interaction.data["options"]
552557
})
553558
ctx.command = self
@@ -657,7 +662,7 @@ def __init__(
657662

658663
self.min_value: minmax_typehint = kwargs.pop("min_value", None)
659664
self.max_value: minmax_typehint = kwargs.pop("max_value", None)
660-
665+
661666
if not (isinstance(self.min_value, minmax_types) or self.min_value is None):
662667
raise TypeError(f"Expected {minmax_typehint} for min_value, got \"{type(self.min_value).__name__}\"")
663668
if not (isinstance(self.max_value, minmax_types) or self.min_value is None):
@@ -971,7 +976,7 @@ def __init__(self, func: Callable, *args, **kwargs) -> None:
971976
self.checks = checks
972977
self._before_invoke = None
973978
self._after_invoke = None
974-
979+
975980
self.validate_parameters()
976981

977982
# Context Menu commands don't have permissions
@@ -1013,7 +1018,7 @@ def validate_parameters(self):
10131018
)
10141019
except StopIteration:
10151020
pass
1016-
1021+
10171022
def qualified_name(self):
10181023
return self.name
10191024

@@ -1052,7 +1057,7 @@ async def _invoke(self, ctx: ApplicationContext) -> None:
10521057
guild=ctx.interaction._state._get_guild(ctx.interaction.guild_id),
10531058
state=ctx.interaction._state,
10541059
)
1055-
1060+
10561061
if self.cog is not None:
10571062
await self.callback(self.cog, ctx, target)
10581063
else:
@@ -1118,12 +1123,12 @@ async def _invoke(self, ctx: ApplicationContext):
11181123
channel = ctx.interaction._state.add_dm_channel(data)
11191124

11201125
target = Message(state=ctx.interaction._state, channel=channel, data=message)
1121-
1126+
11221127
if self.cog is not None:
11231128
await self.callback(self.cog, ctx, target)
11241129
else:
11251130
await self.callback(ctx, target)
1126-
1131+
11271132
def copy(self):
11281133
"""Creates a copy of this command.
11291134

0 commit comments

Comments
 (0)