diff --git a/CHANGELOG.md b/CHANGELOG.md index 02a2e4ceb9..dd27c65485 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,8 @@ These changes are available on the `master` branch, but have not yet been releas `Permissions.use_external_sounds`, and `Permissions.view_creator_monetization_analytics`. ([#2620](https://github.com/Pycord-Development/pycord/pull/2620)) +- Added helper methods to determine the authorizing party of an `Interaction`. + ([#2659](https://github.com/Pycord-Development/pycord/pull/2659)) ### Fixed diff --git a/discord/commands/context.py b/discord/commands/context.py index 27c3b0acba..532d8abe2a 100644 --- a/discord/commands/context.py +++ b/discord/commands/context.py @@ -345,6 +345,40 @@ def cog(self) -> Cog | None: return self.command.cog + def is_guild_authorised(self) -> bool: + """:class:`bool`: Checks if the invoked command is guild-installed. + This is a shortcut for :meth:`Interaction.is_guild_authorised`. + + There is an alias for this called :meth:`.is_guild_authorized`. + + .. versionadded:: 2.7 + """ + return self.interaction.is_guild_authorised() + + def is_user_authorised(self) -> bool: + """:class:`bool`: Checks if the invoked command is user-installed. + This is a shortcut for :meth:`Interaction.is_user_authorised`. + + There is an alias for this called :meth:`.is_user_authorized`. + + .. versionadded:: 2.7 + """ + return self.interaction.is_user_authorised() + + def is_guild_authorized(self) -> bool: + """:class:`bool`: An alias for :meth:`.is_guild_authorised`. + + .. versionadded:: 2.7 + """ + return self.is_guild_authorised() + + def is_user_authorized(self) -> bool: + """:class:`bool`: An alias for :meth:`.is_user_authorised`. + + .. versionadded:: 2.7 + """ + return self.is_user_authorised() + class AutocompleteContext: """Represents context for a slash command's option autocomplete. diff --git a/discord/interactions.py b/discord/interactions.py index e7d7fade3e..e9b9c16eec 100644 --- a/discord/interactions.py +++ b/discord/interactions.py @@ -360,6 +360,48 @@ def followup(self) -> Webhook: } return Webhook.from_state(data=payload, state=self._state) + def is_guild_authorised(self) -> bool: + """:class:`bool`: Checks if the interaction is guild authorised. + + There is an alias for this called :meth:`.is_guild_authorized`. + + .. versionadded:: 2.7 + """ + if self.guild_id: + return self.authorizing_integration_owners.guild_id == self.guild_id + return False + + def is_user_authorised(self) -> bool: + """:class:`bool`: Checks if the interaction is user authorised. + + There is an alias for this called :meth:`.is_user_authorized`. + + .. versionadded:: 2.7 + """ + if self.user: + return self.authorizing_integration_owners.user_id == self.user.id + + # This return should not be called but to make sure it returns the expected value + return False + + def is_guild_authorized(self) -> bool: + """:class:`bool`: Checks if the interaction is guild authorized. + + There is an alias for this called :meth:`.is_guild_authorised`. + + .. versionadded:: 2.7 + """ + return self.is_guild_authorised() + + def is_user_authorized(self) -> bool: + """:class:`bool`: Checks if the interaction is user authorized. + + There is an alias for this called :meth:`.is_user_authorised`. + + .. versionadded:: 2.7 + """ + return self.is_user_authorised() + async def original_response(self) -> InteractionMessage: """|coro|