Skip to content

Commit

Permalink
Merge pull request #398 from Apocryphon-X/master
Browse files Browse the repository at this point in the history
Fix for discord.User parsing
  • Loading branch information
Lulalaby authored Dec 19, 2021
2 parents 031b964 + 712584b commit c25e2a2
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions discord/commands/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
)

if TYPE_CHECKING:
pass
from ..interactions import Interaction

def wrap_callback(coro):
@functools.wraps(coro)
Expand Down Expand Up @@ -99,7 +99,7 @@ class _BaseCommand:

class ApplicationCommand(_BaseCommand):
cog = None

def __repr__(self):
return f"<discord.commands.{self.__class__.__name__} name={self.name}>"

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

return await async_all(predicate(ctx) for predicate in predicates) # type: ignore
return await async_all(predicate(ctx) for predicate in predicates) # type: ignore

async def dispatch_error(self, ctx: ApplicationContext, error: Exception) -> None:
ctx.command_failed = True
cog = self.cog
Expand Down Expand Up @@ -349,7 +349,7 @@ class SlashCommand(ApplicationCommand):
.. note::
If this is not empty then default_permissions will be set to False.
If this is not empty then default_permissions will be set to False.
cog: Optional[:class:`Cog`]
The cog that this command belongs to. ``None`` if there isn't one.
Expand Down Expand Up @@ -515,8 +515,13 @@ async def _invoke(self, ctx: ApplicationContext) -> None:
<= op.input_type.value
<= SlashCommandOptionType.role.value
):
name = "member" if op.input_type.name == "user" else op.input_type.name
arg = await get_or_fetch(ctx.guild, name, int(arg), default=int(arg))
if ctx.guild is None and op.input_type.name == "user":
_data = ctx.interaction.data["resolved"]["users"][arg]
_data["id"] = int(arg)
arg = User(state=ctx.interaction._state, data=_data)
else:
name = "member" if op.input_type.name == "user" else op.input_type.name
arg = await get_or_fetch(ctx.guild, name, int(arg), default=int(arg))

elif op.input_type == SlashCommandOptionType.mentionable:
arg_id = int(arg)
Expand All @@ -532,7 +537,7 @@ async def _invoke(self, ctx: ApplicationContext) -> None:
for o in self.options:
if o._parameter_name not in kwargs:
kwargs[o._parameter_name] = o.default

if self.cog is not None:
await self.callback(self.cog, ctx, **kwargs)
elif self.parent is not None and self.attached_to_group is True:
Expand All @@ -542,12 +547,12 @@ async def _invoke(self, ctx: ApplicationContext) -> None:

async def invoke_autocomplete_callback(self, ctx: AutocompleteContext):
values = { i.name: i.default for i in self.options }

for op in ctx.interaction.data.get("options", []):
if op.get("focused", False):
option = find(lambda o: o.name == op["name"], self.options)
values.update({
i["name"]:i["value"]
i["name"]:i["value"]
for i in ctx.interaction.data["options"]
})
ctx.command = self
Expand Down Expand Up @@ -657,7 +662,7 @@ def __init__(

self.min_value: minmax_typehint = kwargs.pop("min_value", None)
self.max_value: minmax_typehint = kwargs.pop("max_value", None)

if not (isinstance(self.min_value, minmax_types) or self.min_value is None):
raise TypeError(f"Expected {minmax_typehint} for min_value, got \"{type(self.min_value).__name__}\"")
if not (isinstance(self.max_value, minmax_types) or self.min_value is None):
Expand Down Expand Up @@ -971,7 +976,7 @@ def __init__(self, func: Callable, *args, **kwargs) -> None:
self.checks = checks
self._before_invoke = None
self._after_invoke = None

self.validate_parameters()

# Context Menu commands don't have permissions
Expand Down Expand Up @@ -1013,7 +1018,7 @@ def validate_parameters(self):
)
except StopIteration:
pass

def qualified_name(self):
return self.name

Expand Down Expand Up @@ -1052,7 +1057,7 @@ async def _invoke(self, ctx: ApplicationContext) -> None:
guild=ctx.interaction._state._get_guild(ctx.interaction.guild_id),
state=ctx.interaction._state,
)

if self.cog is not None:
await self.callback(self.cog, ctx, target)
else:
Expand Down Expand Up @@ -1118,12 +1123,12 @@ async def _invoke(self, ctx: ApplicationContext):
channel = ctx.interaction._state.add_dm_channel(data)

target = Message(state=ctx.interaction._state, channel=channel, data=message)

if self.cog is not None:
await self.callback(self.cog, ctx, target)
else:
await self.callback(ctx, target)

def copy(self):
"""Creates a copy of this command.
Expand Down

0 comments on commit c25e2a2

Please sign in to comment.