Skip to content

Commit 5d6bf82

Browse files
committed
Fix for discord.User parsing
1 parent 85031d6 commit 5d6bf82

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

discord/commands/commands.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"MessageCommand",
6161
)
6262

63-
if TYPE_CHECKING:
63+
if TYPE_CHECKING:
6464
from ..interactions import Interaction
6565

6666
def wrap_callback(coro):
@@ -98,7 +98,7 @@ class _BaseCommand:
9898

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

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

145-
return await async_all(predicate(ctx) for predicate in predicates) # type: ignore
146-
145+
return await async_all(predicate(ctx) for predicate in predicates) # type: ignore
146+
147147
async def dispatch_error(self, ctx: ApplicationContext, error: Exception) -> None:
148148
ctx.command_failed = True
149149
cog = self.cog
@@ -345,7 +345,7 @@ class SlashCommand(ApplicationCommand):
345345
346346
.. note::
347347
348-
If this is not empty then default_permissions will be set to False.
348+
If this is not empty then default_permissions will be set to False.
349349
350350
cog: Optional[:class:`Cog`]
351351
The cog that this command belongs to. ``None`` if there isn't one.
@@ -504,8 +504,11 @@ async def _invoke(self, ctx: ApplicationContext) -> None:
504504
<= op.input_type.value
505505
<= SlashCommandOptionType.role.value
506506
):
507-
name = "member" if op.input_type.name == "user" else op.input_type.name
508-
arg = await get_or_fetch(ctx.guild, name, int(arg), default=int(arg))
507+
if ctx.guild is None and op.input_type.name == "user":
508+
arg = User(state=ctx.interaction._state, data=int(arg))
509+
else:
510+
name = "member" if op.input_type.name == "user" else op.input_type.name
511+
arg = await get_or_fetch(ctx.guild, name, int(arg), default=int(arg))
509512

510513
elif op.input_type == SlashCommandOptionType.mentionable:
511514
arg_id = int(arg)
@@ -521,20 +524,20 @@ async def _invoke(self, ctx: ApplicationContext) -> None:
521524
for o in self.options:
522525
if o._parameter_name not in kwargs:
523526
kwargs[o._parameter_name] = o.default
524-
527+
525528
if self.cog is not None:
526529
await self.callback(self.cog, ctx, **kwargs)
527530
else:
528531
await self.callback(ctx, **kwargs)
529532

530533
async def invoke_autocomplete_callback(self, interaction: Interaction):
531534
values = { i.name: i.default for i in self.options }
532-
535+
533536
for op in interaction.data.get("options", []):
534537
if op.get("focused", False):
535538
option = find(lambda o: o.name == op["name"], self.options)
536539
values.update({
537-
i["name"]:i["value"]
540+
i["name"]:i["value"]
538541
for i in interaction.data["options"]
539542
})
540543
ctx = AutocompleteContext(interaction, command=self, focused=option, value=op.get("value"), options=values)
@@ -631,15 +634,15 @@ def __init__(
631634

632635
self.min_value: minmax_typehint = kwargs.pop("min_value", None)
633636
self.max_value: minmax_typehint = kwargs.pop("max_value", None)
634-
637+
635638
if not (isinstance(self.min_value, minmax_types) or self.min_value is None):
636639
raise TypeError(f"Expected {minmax_typehint} for min_value, got \"{type(self.min_value).__name__}\"")
637640
if not (isinstance(self.max_value, minmax_types) or self.min_value is None):
638641
raise TypeError(f"Expected {minmax_typehint} for max_value, got \"{type(self.max_value).__name__}\"")
639642

640643
self.autocomplete = kwargs.pop("autocomplete", None)
641644
if (
642-
self.autocomplete and
645+
self.autocomplete and
643646
not asyncio.iscoroutinefunction(self.autocomplete)
644647
):
645648
raise TypeError("Autocomplete callback must be a coroutine.")
@@ -848,7 +851,7 @@ def __init__(self, func: Callable, *args, **kwargs) -> None:
848851
self.checks = checks
849852
self._before_invoke = None
850853
self._after_invoke = None
851-
854+
852855
self.validate_parameters()
853856

854857
# Context Menu commands don't have permissions
@@ -890,7 +893,7 @@ def validate_parameters(self):
890893
)
891894
except StopIteration:
892895
pass
893-
896+
894897
def qualified_name(self):
895898
return self.name
896899

@@ -929,12 +932,12 @@ async def _invoke(self, ctx: ApplicationContext) -> None:
929932
guild=ctx.interaction._state._get_guild(ctx.interaction.guild_id),
930933
state=ctx.interaction._state,
931934
)
932-
935+
933936
if self.cog is not None:
934937
await self.callback(self.cog, ctx, target)
935938
else:
936939
await self.callback(ctx, target)
937-
940+
938941
def copy(self):
939942
"""Creates a copy of this command.
940943
@@ -995,12 +998,12 @@ async def _invoke(self, ctx: ApplicationContext):
995998
channel = ctx.interaction._state.add_dm_channel(data)
996999

9971000
target = Message(state=ctx.interaction._state, channel=channel, data=message)
998-
1001+
9991002
if self.cog is not None:
10001003
await self.callback(self.cog, ctx, target)
10011004
else:
10021005
await self.callback(ctx, target)
1003-
1006+
10041007
def copy(self):
10051008
"""Creates a copy of this command.
10061009

0 commit comments

Comments
 (0)