First pass at supporting user apps

Co-authored-by: red <red@kalab.sk>
Co-authored-by: Vioshim <63890837+Vioshim@users.noreply.github.com>
This commit is contained in:
Danny
2024-05-04 23:25:01 -04:00
committed by GitHub
parent 2892401992
commit 2e2f51fd5c
19 changed files with 920 additions and 52 deletions

View File

@ -166,6 +166,8 @@ class BotBase(GroupMixin[None]):
help_command: Optional[HelpCommand] = _default,
tree_cls: Type[app_commands.CommandTree[Any]] = app_commands.CommandTree,
description: Optional[str] = None,
allowed_contexts: app_commands.AppCommandContext = MISSING,
allowed_installs: app_commands.AppInstallationType = MISSING,
intents: discord.Intents,
**options: Any,
) -> None:
@ -174,6 +176,11 @@ class BotBase(GroupMixin[None]):
self.extra_events: Dict[str, List[CoroFunc]] = {}
# Self doesn't have the ClientT bound, but since this is a mixin it technically does
self.__tree: app_commands.CommandTree[Self] = tree_cls(self) # type: ignore
if allowed_contexts is not MISSING:
self.__tree.allowed_contexts = allowed_contexts
if allowed_installs is not MISSING:
self.__tree.allowed_installs = allowed_installs
self.__cogs: Dict[str, Cog] = {}
self.__extensions: Dict[str, types.ModuleType] = {}
self._checks: List[UserCheck] = []
@ -521,7 +528,6 @@ class BotBase(GroupMixin[None]):
elif self.owner_ids:
return user.id in self.owner_ids
else:
app: discord.AppInfo = await self.application_info() # type: ignore
if app.team:
self.owner_ids = ids = {
@ -1489,6 +1495,20 @@ class Bot(BotBase, discord.Client):
The type of application command tree to use. Defaults to :class:`~discord.app_commands.CommandTree`.
.. versionadded:: 2.0
allowed_contexts: :class:`~discord.app_commands.AppCommandContext`
The default allowed contexts that applies to all application commands
in the application command tree.
Note that you can override this on a per command basis.
.. versionadded:: 2.4
allowed_installs: :class:`~discord.app_commands.AppInstallationType`
The default allowed install locations that apply to all application commands
in the application command tree.
Note that you can override this on a per command basis.
.. versionadded:: 2.4
"""
pass

View File

@ -318,6 +318,8 @@ class Cog(metaclass=CogMeta):
parent=None,
guild_ids=getattr(cls, '__discord_app_commands_default_guilds__', None),
guild_only=getattr(cls, '__discord_app_commands_guild_only__', False),
allowed_contexts=getattr(cls, '__discord_app_commands_contexts__', None),
allowed_installs=getattr(cls, '__discord_app_commands_installation_types__', None),
default_permissions=getattr(cls, '__discord_app_commands_default_permissions__', None),
extras=cls.__cog_group_extras__,
)

View File

@ -472,7 +472,7 @@ class Context(discord.abc.Messageable, Generic[BotT]):
.. versionadded:: 2.0
"""
if self.channel.type is ChannelType.private:
if self.interaction is None and self.channel.type is ChannelType.private:
return Permissions._dm_permissions()
if not self.interaction:
# channel and author will always match relevant types here
@ -506,7 +506,7 @@ class Context(discord.abc.Messageable, Generic[BotT]):
.. versionadded:: 2.0
"""
channel = self.channel
if channel.type == ChannelType.private:
if self.interaction is None and channel.type == ChannelType.private:
return Permissions._dm_permissions()
if not self.interaction:
# channel and me will always match relevant types here

View File

@ -653,6 +653,8 @@ class HybridGroup(Group[CogT, P, T]):
guild_only = getattr(self.callback, '__discord_app_commands_guild_only__', False)
default_permissions = getattr(self.callback, '__discord_app_commands_default_permissions__', None)
nsfw = getattr(self.callback, '__discord_app_commands_is_nsfw__', False)
contexts = getattr(self.callback, '__discord_app_commands_contexts__', MISSING)
installs = getattr(self.callback, '__discord_app_commands_installation_types__', MISSING)
self.app_command = app_commands.Group(
name=self._locale_name or self.name,
description=self._locale_description or self.description or self.short_doc or '',
@ -660,6 +662,8 @@ class HybridGroup(Group[CogT, P, T]):
guild_only=guild_only,
default_permissions=default_permissions,
nsfw=nsfw,
allowed_installs=installs,
allowed_contexts=contexts,
)
# This prevents the group from re-adding the command at __init__