From e8133e5e1750ed3f5b8537e250ccf55ec0889084 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Thu, 30 Jun 2022 01:57:40 +0200 Subject: [PATCH 01/17] Implement app_permissions --- discord/commands/context.py | 5 +++++ discord/interactions.py | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/discord/commands/context.py b/discord/commands/context.py index fcdbb18a46..4dc2ea9d9b 100644 --- a/discord/commands/context.py +++ b/discord/commands/context.py @@ -43,6 +43,7 @@ from ..member import Member from ..message import Message from ..user import User + from ..permissions import Permissions from ..client import ClientUser from discord.webhook.async_ import Webhook @@ -158,6 +159,10 @@ def locale(self) -> Optional[str]: def guild_locale(self) -> Optional[str]: return self.interaction.guild_locale + @cached_property + def app_permissions(self) -> Permissions: + return self.interaction.app_permissions + @cached_property def me(self) -> Optional[Union[Member, ClientUser]]: return self.interaction.guild.me if self.interaction.guild is not None else self.bot.user diff --git a/discord/interactions.py b/discord/interactions.py index 157213ed01..80ab65243d 100644 --- a/discord/interactions.py +++ b/discord/interactions.py @@ -119,6 +119,8 @@ class Interaction: The guilds preferred locale, if invoked in a guild. custom_id: Optional[:class:`str`] The custom ID for the interaction. + app_permissions: :class:`Permissions` + The applications calculated permissions. """ __slots__: Tuple[str, ...] = ( @@ -135,8 +137,10 @@ class Interaction: "token", "version", "custom_id", + "app_permissions", "_message_data", "_permissions", + "_app_permissions", "_state", "_session", "_original_message", @@ -163,6 +167,7 @@ def _from_data(self, data: InteractionPayload): self.locale: Optional[str] = data.get("locale") self.guild_locale: Optional[str] = data.get("guild_locale") self.custom_id: Optional[str] = self.data.get("custom_id") if self.data is not None else None + self._app_permissions: int = int(data.get("app_permissions")) self.message: Optional[Message] = None @@ -233,6 +238,11 @@ def permissions(self) -> Permissions: """ return Permissions(self._permissions) + @property + def app_permissions(self) -> Permissions: + """:class:`Permissions`: The resolved permissions of the application in the channel, including overwrites.""" + return Permissions(self._app_permissions) + @utils.cached_slot_property("_cs_response") def response(self) -> InteractionResponse: """:class:`InteractionResponse`: Returns an object responsible for handling responding to the interaction. From 9fdd9f67cf6c7fa205e80fef0c22b79f493aaea1 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Thu, 30 Jun 2022 00:11:21 +0000 Subject: [PATCH 02/17] I am dumb --- discord/interactions.py | 8 +++----- discord/types/interactions.py | 2 ++ 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/discord/interactions.py b/discord/interactions.py index 80ab65243d..bf5ff51cc0 100644 --- a/discord/interactions.py +++ b/discord/interactions.py @@ -54,7 +54,6 @@ from .channel import ( CategoryChannel, - PartialMessageable, StageChannel, TextChannel, VoiceChannel, @@ -137,10 +136,9 @@ class Interaction: "token", "version", "custom_id", - "app_permissions", "_message_data", "_permissions", - "_app_permissions", + "_app_permissions_int", "_state", "_session", "_original_message", @@ -167,7 +165,7 @@ def _from_data(self, data: InteractionPayload): self.locale: Optional[str] = data.get("locale") self.guild_locale: Optional[str] = data.get("guild_locale") self.custom_id: Optional[str] = self.data.get("custom_id") if self.data is not None else None - self._app_permissions: int = int(data.get("app_permissions")) + self._app_permissions_int: int = int(data.get("app_permissions")) self.message: Optional[Message] = None @@ -241,7 +239,7 @@ def permissions(self) -> Permissions: @property def app_permissions(self) -> Permissions: """:class:`Permissions`: The resolved permissions of the application in the channel, including overwrites.""" - return Permissions(self._app_permissions) + return Permissions(self._app_permissions_int) @utils.cached_slot_property("_cs_response") def response(self) -> InteractionResponse: diff --git a/discord/types/interactions.py b/discord/types/interactions.py index 56cb9c1f5e..77096ebf47 100644 --- a/discord/types/interactions.py +++ b/discord/types/interactions.py @@ -35,6 +35,7 @@ from .role import Role from .snowflake import Snowflake from .user import User +from ..permissions import Permissions if TYPE_CHECKING: from .message import AllowedMentions, Message @@ -202,6 +203,7 @@ class _InteractionOptional(TypedDict, total=False): message: Message locale: str guild_locale: str + app_permissions: Permissions class Interaction(_InteractionOptional): From c242a7d0056c78414f81c65cc1a136f20ff4d729 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Thu, 30 Jun 2022 00:14:39 +0000 Subject: [PATCH 03/17] meow --- discord/interactions.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/discord/interactions.py b/discord/interactions.py index bf5ff51cc0..1253fd71bf 100644 --- a/discord/interactions.py +++ b/discord/interactions.py @@ -118,8 +118,6 @@ class Interaction: The guilds preferred locale, if invoked in a guild. custom_id: Optional[:class:`str`] The custom ID for the interaction. - app_permissions: :class:`Permissions` - The applications calculated permissions. """ __slots__: Tuple[str, ...] = ( @@ -138,7 +136,7 @@ class Interaction: "custom_id", "_message_data", "_permissions", - "_app_permissions_int", + "_app_permissions", "_state", "_session", "_original_message", @@ -165,7 +163,7 @@ def _from_data(self, data: InteractionPayload): self.locale: Optional[str] = data.get("locale") self.guild_locale: Optional[str] = data.get("guild_locale") self.custom_id: Optional[str] = self.data.get("custom_id") if self.data is not None else None - self._app_permissions_int: int = int(data.get("app_permissions")) + self._app_permissions: int = int(data.get("app_permissions")) self.message: Optional[Message] = None @@ -239,7 +237,7 @@ def permissions(self) -> Permissions: @property def app_permissions(self) -> Permissions: """:class:`Permissions`: The resolved permissions of the application in the channel, including overwrites.""" - return Permissions(self._app_permissions_int) + return Permissions(self._app_permissions) @utils.cached_slot_property("_cs_response") def response(self) -> InteractionResponse: From 8742a9db8c81b127a0f9af755dc837772b3179e8 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Thu, 30 Jun 2022 00:16:23 +0000 Subject: [PATCH 04/17] Fix doc warning --- discord/interactions.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/discord/interactions.py b/discord/interactions.py index 1253fd71bf..53c117c338 100644 --- a/discord/interactions.py +++ b/discord/interactions.py @@ -499,13 +499,17 @@ def is_done(self) -> bool: async def defer(self, *, ephemeral: bool = False, invisible: bool = True) -> None: """|coro| + Defers the interaction response. + This is typically used when the interaction is acknowledged and a secondary action will be done later. - This is can only be used with the following interaction types + + This is can only be used with the following interaction types: - :attr:`InteractionType.application_command` - :attr:`InteractionType.component` - :attr:`InteractionType.modal_submit` + Parameters ----------- ephemeral: :class:`bool` @@ -517,6 +521,7 @@ async def defer(self, *, ephemeral: bool = False, invisible: bool = True) -> Non In the Discord UI, this is represented as the bot thinking of a response. You must eventually send a followup message via :attr:`Interaction.followup` to make this thinking state go away. This parameter does not apply to interactions of type :attr:`InteractionType.application_command`. + Raises ------- HTTPException From 7dc34c646ff158855c797438e1421a990d38bf95 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Thu, 30 Jun 2022 00:19:03 +0000 Subject: [PATCH 05/17] Fix slots for converter --- discord/ext/commands/converter.py | 1 + 1 file changed, 1 insertion(+) diff --git a/discord/ext/commands/converter.py b/discord/ext/commands/converter.py index 1bc3481ba0..1de137b2ce 100644 --- a/discord/ext/commands/converter.py +++ b/discord/ext/commands/converter.py @@ -62,6 +62,7 @@ "MessageConverter", "PartialMessageConverter", "TextChannelConverter", + "ForumChannelConverter", "InviteConverter", "GuildConverter", "RoleConverter", From b3d7b7efd831db60607e1e6edb38b0e388d1ad86 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Thu, 30 Jun 2022 00:21:24 +0000 Subject: [PATCH 06/17] Fix faq docs link --- docs/faq.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/faq.rst b/docs/faq.rst index 5947c7f821..a43feaab64 100644 --- a/docs/faq.rst +++ b/docs/faq.rst @@ -53,8 +53,8 @@ instead. Similar to this example: :: # good await asyncio.sleep(10) -Another common source of blocking for too long is using HTTP requests with the famous module :doc:`req:index`. -While :doc:`req:index` is an amazing module for non-asynchronous programming, it is not a good choice for +Another common source of blocking for too long is using HTTP requests with the famous module :doc:`requests:index`. +While :doc:`requests:index` is an amazing module for non-asynchronous programming, it is not a good choice for :mod:`asyncio` because certain requests can block the event loop too long. Instead, use the :doc:`aiohttp ` library which is installed on the side with this library. From c59a1f15c5e3db49ffec3b32c1482c356e317ffb Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Thu, 30 Jun 2022 02:26:39 +0200 Subject: [PATCH 07/17] Update conf.py --- docs/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index f1bed25767..eb51c68507 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -57,7 +57,7 @@ intersphinx_mapping = { "py": ("https://docs.python.org/3", None), "aio": ("https://docs.aiohttp.org/en/stable/", None), - "req": ("https://docs.python-requests.org/en/latest/", None), + "req": ("https://requests.readthedocs.io/en/latest/", None), } rst_prolog = """ From c41d8fc153dae0b9553aed0c419da9b05023ca51 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Thu, 30 Jun 2022 02:26:59 +0200 Subject: [PATCH 08/17] Update faq.rst --- docs/faq.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/faq.rst b/docs/faq.rst index a43feaab64..72c43b7980 100644 --- a/docs/faq.rst +++ b/docs/faq.rst @@ -53,8 +53,8 @@ instead. Similar to this example: :: # good await asyncio.sleep(10) -Another common source of blocking for too long is using HTTP requests with the famous module :doc:`requests:index`. -While :doc:`requests:index` is an amazing module for non-asynchronous programming, it is not a good choice for +Another common source of blocking for too long is using HTTP requests with the famous module :doc:`requests `. +While :doc:`requests ` is an amazing module for non-asynchronous programming, it is not a good choice for :mod:`asyncio` because certain requests can block the event loop too long. Instead, use the :doc:`aiohttp ` library which is installed on the side with this library. From e89ad9e9bd5c19b4aaf2493a099311a3f10fac90 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Thu, 30 Jun 2022 02:33:26 +0200 Subject: [PATCH 09/17] Update discord/interactions.py Co-authored-by: baronkobama --- discord/interactions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/interactions.py b/discord/interactions.py index 53c117c338..e477777831 100644 --- a/discord/interactions.py +++ b/discord/interactions.py @@ -505,7 +505,7 @@ async def defer(self, *, ephemeral: bool = False, invisible: bool = True) -> Non This is typically used when the interaction is acknowledged and a secondary action will be done later. - This is can only be used with the following interaction types: + This can only be used with the following interaction types: - :attr:`InteractionType.application_command` - :attr:`InteractionType.component` - :attr:`InteractionType.modal_submit` From 0a48adaee9419e9386799753894f8b377474340d Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Thu, 30 Jun 2022 11:45:58 +0000 Subject: [PATCH 10/17] try fix of issue --- discord/ext/commands/core.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index 923835bde3..c75447c0e7 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -29,6 +29,7 @@ import functools import inspect import types + from typing import ( TYPE_CHECKING, Any, @@ -67,6 +68,8 @@ DynamicCooldownMapping, MaxConcurrency, ) +from ...channel import PartialMessageable + from .errors import * if TYPE_CHECKING: @@ -2121,6 +2124,9 @@ def bot_has_permissions(**perms: bool) -> Callable[[T], T]: def predicate(ctx: Context) -> bool: guild = ctx.guild me = guild.me if guild is not None else ctx.bot.user + if isinstance(ctx.channel, PartialMessageable): + return True + permissions = ctx.channel.permissions_for(me) # type: ignore missing = [perm for perm, value in perms.items() if getattr(permissions, perm) != value] From 500f63323565c162bacbd6c4992aa6ed3dde4d75 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Thu, 30 Jun 2022 12:03:57 +0000 Subject: [PATCH 11/17] better fix --- discord/ext/commands/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index c75447c0e7..73aa4d09aa 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -68,7 +68,7 @@ DynamicCooldownMapping, MaxConcurrency, ) -from ...channel import PartialMessageable +from ...enums import ChannelType from .errors import * @@ -2124,7 +2124,7 @@ def bot_has_permissions(**perms: bool) -> Callable[[T], T]: def predicate(ctx: Context) -> bool: guild = ctx.guild me = guild.me if guild is not None else ctx.bot.user - if isinstance(ctx.channel, PartialMessageable): + if ctx.channel.type == ChannelType.private: return True permissions = ctx.channel.permissions_for(me) # type: ignore From 1d1ed48a8e3bf9c4eec51bab27111ee76d676301 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Thu, 30 Jun 2022 12:05:45 +0000 Subject: [PATCH 12/17] eh --- discord/ext/commands/core.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index 73aa4d09aa..d9022093d7 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -2074,6 +2074,8 @@ def has_permissions(**perms: bool) -> Callable[[T], T]: This check raises a special exception, :exc:`.MissingPermissions` that is inherited from :exc:`.CheckFailure`. + If the command is executed withiun a dm, it returns True. + Parameters ------------ perms From 0e4ea70db8c8d6761f920a012018faeaf509efc5 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Thu, 30 Jun 2022 14:30:42 +0200 Subject: [PATCH 13/17] Update discord/ext/commands/core.py --- discord/ext/commands/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index d9022093d7..5636ab8958 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -2074,7 +2074,7 @@ def has_permissions(**perms: bool) -> Callable[[T], T]: This check raises a special exception, :exc:`.MissingPermissions` that is inherited from :exc:`.CheckFailure`. - If the command is executed withiun a dm, it returns True. + If the command is executed within a dm, it returns True. Parameters ------------ From 6ddd4538bc3dc829d4e1bc9ff5571c070f01555e Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Thu, 30 Jun 2022 17:24:01 +0200 Subject: [PATCH 14/17] Update discord/ext/commands/core.py Co-authored-by: plun1331 <49261529+plun1331@users.noreply.github.com> --- discord/ext/commands/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index 5636ab8958..0f15bfdcdd 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -2074,7 +2074,7 @@ def has_permissions(**perms: bool) -> Callable[[T], T]: This check raises a special exception, :exc:`.MissingPermissions` that is inherited from :exc:`.CheckFailure`. - If the command is executed within a dm, it returns True. + If the command is executed within a DM, it returns ``True``. Parameters ------------ From 2fa70ad10562e91b079e50bc8bb0776a36b99124 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Thu, 30 Jun 2022 17:24:30 +0200 Subject: [PATCH 15/17] Update discord/ext/commands/core.py Co-authored-by: plun1331 <49261529+plun1331@users.noreply.github.com> --- discord/ext/commands/core.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index 0f15bfdcdd..1ede1a4c41 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -2129,7 +2129,10 @@ def predicate(ctx: Context) -> bool: if ctx.channel.type == ChannelType.private: return True - permissions = ctx.channel.permissions_for(me) # type: ignore + if hasattr(ctx, 'app_permissions'): + permissions = ctx.app_permissions + else: + permissions = ctx.channel.permissions_for(me) # type: ignore missing = [perm for perm, value in perms.items() if getattr(permissions, perm) != value] From f0f9030e6c74d8b397080adaece69ab192275014 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Thu, 30 Jun 2022 17:25:06 +0200 Subject: [PATCH 16/17] Update discord/interactions.py Co-authored-by: plun1331 <49261529+plun1331@users.noreply.github.com> --- discord/interactions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/interactions.py b/discord/interactions.py index e477777831..2dc52cc579 100644 --- a/discord/interactions.py +++ b/discord/interactions.py @@ -234,7 +234,7 @@ def permissions(self) -> Permissions: """ return Permissions(self._permissions) - @property + @utils.cached_slot_property("_cs_app_permissions") def app_permissions(self) -> Permissions: """:class:`Permissions`: The resolved permissions of the application in the channel, including overwrites.""" return Permissions(self._app_permissions) From 7fb3e521507d98c72c54240c7f61ec4bc587f60b Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Thu, 30 Jun 2022 17:25:20 +0200 Subject: [PATCH 17/17] Update discord/interactions.py Co-authored-by: plun1331 <49261529+plun1331@users.noreply.github.com> --- discord/interactions.py | 1 + 1 file changed, 1 insertion(+) diff --git a/discord/interactions.py b/discord/interactions.py index 2dc52cc579..226ed5f309 100644 --- a/discord/interactions.py +++ b/discord/interactions.py @@ -140,6 +140,7 @@ class Interaction: "_state", "_session", "_original_message", + "_cs_app_permissions", "_cs_response", "_cs_followup", "_cs_channel",