Skip to content

Commit

Permalink
feat: Add helper methods to determine interactions integrations (#2659)
Browse files Browse the repository at this point in the history
* Add is_..._integration helper methods

* chore: Update changelog

* chore: Update with suggestions

Co-authored-by: JustaSqu1d <[email protected]>
Signed-off-by: DA344 <[email protected]>

* chore: Update discord/commands/context.py

Co-authored-by: JustaSqu1d <[email protected]>
Signed-off-by: DA344 <[email protected]>

* chore: Update discord/interactions.py

Co-authored-by: JustaSqu1d <[email protected]>
Signed-off-by: DA344 <[email protected]>

* chore: Rename is_x_integration to is_x_authorised/authorized

* chore: 👽 Update base max filesize to `10` Mb (#2671)

* 👽 Update base max filesize to `10` Mb

* 📝 CHANGELOG.md

* chore: Update docstrings to mention aliases

* chore: Update CHANGELOG.md

Co-authored-by: JustaSqu1d <[email protected]>
Signed-off-by: DA344 <[email protected]>

* chore(deps-dev): update mypy requirement from ~=1.13.0 to ~=1.14.0 (#2676)

Updates the requirements on [mypy](https://github.com/python/mypy) to permit the latest version.
- [Changelog](https://github.com/python/mypy/blob/master/CHANGELOG.md)
- [Commits](python/mypy@v1.13.0...v1.14.0)

---
updated-dependencies:
- dependency-name: mypy
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Update CHANGELOG.md

Signed-off-by: plun1331 <[email protected]>

* chore: authorising -> authorizing (who commited this tf)

Co-authored-by: JustaSqu1d <[email protected]>
Signed-off-by: DA344 <[email protected]>

---------

Signed-off-by: DA344 <[email protected]>
Signed-off-by: dependabot[bot] <[email protected]>
Signed-off-by: plun1331 <[email protected]>
Co-authored-by: JustaSqu1d <[email protected]>
Co-authored-by: Paillat <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: plun1331 <[email protected]>
  • Loading branch information
5 people authored Dec 27, 2024
1 parent 7bd9235 commit 9fa5cbe
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 34 additions & 0 deletions discord/commands/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
42 changes: 42 additions & 0 deletions discord/interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down

0 comments on commit 9fa5cbe

Please sign in to comment.