Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
krittick authored Apr 15, 2022
2 parents 605471a + 8f64303 commit 4e6a27f
Show file tree
Hide file tree
Showing 31 changed files with 231 additions and 206 deletions.
22 changes: 11 additions & 11 deletions discord/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
Any,
Callable,
Dict,
Iterable,
List,
Optional,
Protocol,
Expand All @@ -42,7 +43,6 @@
Union,
overload,
runtime_checkable,
Iterable,
)

from . import utils
Expand Down Expand Up @@ -111,16 +111,16 @@ async def _single_delete_strategy(messages: Iterable[Message], *, reason: Option


async def _purge_messages_helper(
channel: Union[TextChannel, Thread, VoiceChannel],
*,
limit: Optional[int] = 100,
check: Callable[[Message], bool] = MISSING,
before: Optional[SnowflakeTime] = None,
after: Optional[SnowflakeTime] = None,
around: Optional[SnowflakeTime] = None,
oldest_first: Optional[bool] = False,
bulk: bool = True,
reason: Optional[str] = None,
channel: Union[TextChannel, Thread, VoiceChannel],
*,
limit: Optional[int] = 100,
check: Callable[[Message], bool] = MISSING,
before: Optional[SnowflakeTime] = None,
after: Optional[SnowflakeTime] = None,
around: Optional[SnowflakeTime] = None,
oldest_first: Optional[bool] = False,
bulk: bool = True,
reason: Optional[str] = None,
) -> List[Message]:
if check is MISSING:
check = lambda m: True
Expand Down
72 changes: 33 additions & 39 deletions discord/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
Dict,
Generator,
List,
Literal,
Optional,
Type,
TypeVar,
Union,
Literal,
)

from .client import Client
Expand Down Expand Up @@ -248,11 +248,7 @@ def _check_command(cmd: ApplicationCommand, match: Dict) -> bool:
return True
for i, subcommand in enumerate(cmd.subcommands):
match_ = next(
(
data
for data in match["options"]
if data["name"] == subcommand.name
),
(data for data in match["options"] if data["name"] == subcommand.name),
MISSING,
)
if match_ is not MISSING and _check_command(subcommand, match_):
Expand All @@ -265,20 +261,23 @@ def _check_command(cmd: ApplicationCommand, match: Dict) -> bool:
"description": None,
"name_localizations": None,
"description_localizations": None,
"options": ["type", "name", "description", "autocomplete", "choices", "name_localizations",
"description_localizations"],
"options": [
"type",
"name",
"description",
"autocomplete",
"choices",
"name_localizations",
"description_localizations",
],
}
for check, value in to_check.items():
if type(to_check[check]) == list:
# We need to do some falsy conversion here
# The API considers False (autocomplete) and [] (choices) to be falsy values
falsy_vals = (False, [])
for opt in value:
cmd_vals = (
[val.get(opt, MISSING) for val in as_dict[check]]
if check in as_dict
else []
)
cmd_vals = [val.get(opt, MISSING) for val in as_dict[check]] if check in as_dict else []
for i, val in enumerate(cmd_vals):
if val in falsy_vals:
cmd_vals[i] = MISSING
Expand Down Expand Up @@ -379,12 +378,12 @@ async def register_command(
raise RuntimeError("This function has not been implemented yet")

async def register_commands(
self,
commands: Optional[List[ApplicationCommand]] = None,
guild_id: Optional[int] = None,
method: Literal["individual", "bulk", "auto"] = "bulk",
force: bool = False,
delete_existing: bool = True,
self,
commands: Optional[List[ApplicationCommand]] = None,
guild_id: Optional[int] = None,
method: Literal["individual", "bulk", "auto"] = "bulk",
force: bool = False,
delete_existing: bool = True,
) -> List[interactions.ApplicationCommand]:
"""|coro|
Expand Down Expand Up @@ -523,8 +522,8 @@ def register(method: Literal["bulk", "upsert", "delete", "edit"], *args, **kwarg
else:
_log.debug(
f"Bulk updating commands %s for guild %s",
{c['command'].name: c['action'] for c in pending_actions},
guild_id
{c["command"].name: c["action"] for c in pending_actions},
guild_id,
)
registered = await register("bulk", data, _log=False)
else:
Expand Down Expand Up @@ -565,14 +564,14 @@ def register(method: Literal["bulk", "upsert", "delete", "edit"], *args, **kwarg
return registered

async def sync_commands(
self,
commands: Optional[List[ApplicationCommand]] = None,
method: Literal["individual", "bulk", "auto"] = "bulk",
force: bool = False,
guild_ids: Optional[List[int]] = None,
register_guild_commands: bool = True,
check_guilds: Optional[List[int]] = [],
delete_exiting: bool = True,
self,
commands: Optional[List[ApplicationCommand]] = None,
method: Literal["individual", "bulk", "auto"] = "bulk",
force: bool = False,
guild_ids: Optional[List[int]] = None,
register_guild_commands: bool = True,
check_guilds: Optional[List[int]] = [],
delete_exiting: bool = True,
) -> None:
"""|coro|
Expand Down Expand Up @@ -626,8 +625,9 @@ async def sync_commands(
cmd.guild_ids = guild_ids

global_commands = [cmd for cmd in commands if cmd.guild_ids is None]
registered_commands = await self.register_commands(global_commands, method=method, force=force,
delete_existing=delete_exiting)
registered_commands = await self.register_commands(
global_commands, method=method, force=force, delete_existing=delete_exiting
)

registered_guild_commands = {}

Expand Down Expand Up @@ -830,16 +830,14 @@ async def process_application_commands(self, interaction: Interaction, auto_sync
await self.sync_commands(check_guilds=[guild_id])
return self._bot.dispatch("unknown_application_command", interaction)


if interaction.type is InteractionType.auto_complete:
return self.dispatch("application_command_auto_complete", interaction, command)

ctx = await self.get_application_context(interaction)
ctx.command = command
await self.invoke_application_command(ctx)

async def on_application_command_auto_complete(self, interaction: Interaction,
command: ApplicationCommand) -> None:
async def on_application_command_auto_complete(self, interaction: Interaction, command: ApplicationCommand) -> None:
async def callback() -> None:
ctx = await self.get_autocomplete_context(interaction)
ctx.command = command
Expand Down Expand Up @@ -937,11 +935,7 @@ def command(self, **kwargs):
return self.application_command(**kwargs)

def create_group(
self,
name: str,
description: Optional[str] = None,
guild_ids: Optional[List[int]] = None,
**kwargs
self, name: str, description: Optional[str] = None, guild_ids: Optional[List[int]] = None, **kwargs
) -> SlashCommandGroup:
"""A shortcut method that creates a slash command group with no subcommands and adds it to the internal
command list via :meth:`~.ApplicationCommandMixin.add_application_command`.
Expand Down
14 changes: 9 additions & 5 deletions discord/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def __new__(cls: Type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta:
if elem.startswith(("cog_", "bot_")):
raise TypeError(no_bot_cog.format(base, elem))
commands[elem] = value

try:
# a test to see if this value is a BridgeCommand
getattr(value, "add_to")
Expand All @@ -199,9 +199,9 @@ def __new__(cls: Type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta:
raise TypeError(f"Command in method {base}.{elem!r} must not be staticmethod.")
if elem.startswith(("cog_", "bot_")):
raise TypeError(no_bot_cog.format(base, elem))
commands["ext_" + elem] = value.get_ext_command()
commands["application_" + elem] = value.get_application_command()

commands[f"ext_{elem}"] = value.get_ext_command()
commands[f"application_{elem}"] = value.get_application_command()
except AttributeError:
# we are confident that the value is not a Bridge Command
pass
Expand Down Expand Up @@ -237,7 +237,11 @@ def __new__(cls: Type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta:

# Update the Command instances dynamically as well
for command in new_cls.__cog_commands__:
if isinstance(command, ApplicationCommand) and command.guild_ids is None and len(new_cls.__cog_guild_ids__) != 0:
if (
isinstance(command, ApplicationCommand)
and command.guild_ids is None
and len(new_cls.__cog_guild_ids__) != 0
):
command.guild_ids = new_cls.__cog_guild_ids__
if not isinstance(command, SlashCommandGroup):
setattr(new_cls, command.callback.__name__, command)
Expand Down
Loading

0 comments on commit 4e6a27f

Please sign in to comment.