Browse Source

Add Member.resolved_permissions attribute for interaction members

pull/7492/head
Rapptz 3 years ago
parent
commit
f80f81c395
  1. 2
      discord/interactions.py
  2. 24
      discord/member.py

2
discord/interactions.py

@ -163,7 +163,7 @@ class Interaction(Generic[ClientT]):
else: else:
# The fallback to Object for guild causes a type check error but is explicitly allowed here # The fallback to Object for guild causes a type check error but is explicitly allowed here
self.user = Member(state=self._state, guild=guild, data=member) # type: ignore self.user = Member(state=self._state, guild=guild, data=member) # type: ignore
self._permissions = int(member.get('permissions', 0)) self._permissions = self.user._permissions or 0
else: else:
try: try:
self.user = User(state=self._state, data=data['user']) # type: ignore - The key is optional and handled self.user = User(state=self._state, data=data['user']) # type: ignore - The key is optional and handled

24
discord/member.py

@ -272,6 +272,7 @@ class Member(discord.abc.Messageable, _UserTag):
'pending', 'pending',
'nick', 'nick',
'timed_out_until', 'timed_out_until',
'_permissions',
'_client_status', '_client_status',
'_user', '_user',
'_state', '_state',
@ -307,6 +308,12 @@ class Member(discord.abc.Messageable, _UserTag):
self.nick: Optional[str] = data.get('nick', None) self.nick: Optional[str] = data.get('nick', None)
self.pending: bool = data.get('pending', False) self.pending: bool = data.get('pending', False)
self._avatar: Optional[str] = data.get('avatar') self._avatar: Optional[str] = data.get('avatar')
self._permissions: Optional[int]
try:
self._permissions = int(data['permissions'])
except KeyError:
self._permissions = None
self.timed_out_until: Optional[datetime.datetime] = utils.parse_time(data.get('communication_disabled_until')) self.timed_out_until: Optional[datetime.datetime] = utils.parse_time(data.get('communication_disabled_until'))
def __str__(self) -> str: def __str__(self) -> str:
@ -365,6 +372,7 @@ class Member(discord.abc.Messageable, _UserTag):
self.pending = member.pending self.pending = member.pending
self.activities = member.activities self.activities = member.activities
self.timed_out_until = member.timed_out_until self.timed_out_until = member.timed_out_until
self._permissions = member._permissions
self._state = member._state self._state = member._state
self._avatar = member._avatar self._avatar = member._avatar
@ -618,6 +626,22 @@ class Member(discord.abc.Messageable, _UserTag):
return base return base
@property
def resolved_permissions(self) -> Optional[Permissions]:
"""Optional[:class:`Permissions`]: Returns the member's resolved permissions
from an interaction.
This is only available in interaction contexts and represents the resolved
permissions of the member in the channel the interaction was executed in.
This is more or less equivalent to calling :meth:`abc.GuildChannel.permissions_for`
but stored and returned as an attribute by the Discord API rather than computed.
.. versionadded:: 2.0
"""
if self._permissions is None:
return None
return Permissions(self._permissions)
@property @property
def voice(self) -> Optional[VoiceState]: def voice(self) -> Optional[VoiceState]:
"""Optional[:class:`VoiceState`]: Returns the member's current voice state.""" """Optional[:class:`VoiceState`]: Returns the member's current voice state."""

Loading…
Cancel
Save