Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create Context.has_permissions() & Member.has_permissions() #1056

Merged
merged 4 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions interactions/api/models/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1231,13 +1231,9 @@ async def get_permissions_for(self, member: "Member") -> Permissions:
if not self.guild_id:
return Permissions.DEFAULT

from .guild import Guild
permissions = await member.get_guild_permissions(self.guild_id)
EepyElvyra marked this conversation as resolved.
Show resolved Hide resolved

guild = Guild(**await self._client.get_guild(int(self.guild_id)), _client=self._client)

permissions = await member.get_guild_permissions(guild)

if permissions & Permissions.ADMINISTRATOR == Permissions.ADMINISTRATOR:
if Permissions.ADMINISTRATOR in permissions:
return Permissions.ALL

# @everyone role overwrites
Expand Down
60 changes: 58 additions & 2 deletions interactions/api/models/member.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,9 @@ def get_avatar_url(
url += ".gif" if self.avatar.startswith("a_") else ".png"
return url

async def get_guild_permissions(self, guild: "Guild") -> Permissions:
async def get_guild_permissions(
self, guild_id: Optional[Union[int, Snowflake, "Guild"]] = MISSING
) -> Permissions:
"""
Returns the permissions of the member for the specified guild.

Expand All @@ -521,6 +523,18 @@ async def get_guild_permissions(self, guild: "Guild") -> Permissions:
:return: Base permissions of the member in the specified guild
:rtype: Permissions
"""
from .guild import Guild

if guild_id is MISSING:
_guild_id = self.guild_id
if isinstance(_guild_id, LibraryException):
raise _guild_id

else:
_guild_id = int(guild_id) if not isinstance(guild_id, Guild) else int(guild_id.id)

guild = Guild(**await self._client.get_guild(int(_guild_id)), _client=self._client)

if int(guild.owner_id) == int(self.id):
return Permissions.ALL

Expand All @@ -531,7 +545,49 @@ async def get_guild_permissions(self, guild: "Guild") -> Permissions:
role = await guild.get_role(role_id)
permissions |= int(role.permissions)

if permissions & Permissions.ADMINISTRATOR == Permissions.ADMINISTRATOR:
if Permissions.ADMINISTRATOR in Permissions(permissions):
return Permissions.ALL

return Permissions(permissions)

async def has_permissions(
self,
*permissions: Union[int, Permissions],
channel: Optional[Channel] = MISSING,
guild_id: Optional[Union[int, Snowflake, "Guild"]] = MISSING,
operator: str = "and",
) -> bool:
"""
Returns whether the member has the permissions passed.

.. note::
If the channel argument is present, the function will look if the member has the permissions in the specified channel.
If the argument is missing, then it will only consider the member's guild permissions.

:param *permissions: The list of permissions
:type *permissions: Union[int, Permissions]
:param channel: The channel where to check for permissions
:type channel: Channel
:param guild_id: The id of the guild
:type guild_id: Optional[Union[int, Snowflake, Guild]]
:param operator: The operator to use to calculate permissions. Possible values: `and`, `or`. Defaults to `and`.
:type operator: str
:return: Whether the member has the permissions
:rtype: bool
"""
perms = (
await self.get_guild_permissions(guild_id)
if channel is MISSING
else await channel.get_permissions_for(self)
)

if operator == "and":
for perm in permissions:
if perm not in perms:
return False
return True
else:
for perm in permissions:
if perm in perms:
return True
return False
24 changes: 24 additions & 0 deletions interactions/client/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,30 @@ async def popup(self, modal: Modal) -> dict:

return payload

async def has_permissions(
self, *permissions: Union[int, Permissions], operator: str = "and"
) -> bool:
"""
Returns whether the author of the interaction has the permissions in the given context.

:param *permissions: The list of permissions
:type *permissions: Union[int, Permissions]
:param operator: The operator to use to calculate permissions. Possible values: `and`, `or`. Defaults to `and`.
:type operator: str
:return: Whether the author has the permissions
:rtype: bool
"""
if operator == "and":
for perm in permissions:
if perm not in self.author.permissions:
return False
return True
else:
for perm in permissions:
if perm in self.author.permissions:
return True
return False


@define()
class CommandContext(_Context):
Expand Down