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

Implement guild timeouts #8

Merged
merged 5 commits into from
Dec 20, 2021
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
50 changes: 36 additions & 14 deletions discord/member.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,16 +253,20 @@ class Member(discord.abc.Messageable, _UserTag):
premium_since: Optional[:class:`datetime.datetime`]
An aware datetime object that specifies the date and time in UTC when the member used their
"Nitro boost" on the guild, if available. This could be ``None``.
timeout_until: Optional[:class:`datetime.datetime`]
An aware datetime object that specifies the date and time in UTC when the member will have their
timeout removed. This could be ``None``.
"""

__slots__ = (
'_roles',
'joined_at',
'premium_since',
'timeout_until',
'activities',
'guild',
'pending',
'nick',
'_roles',
'_client_status',
'_user',
'_state',
Expand Down Expand Up @@ -293,6 +297,7 @@ def __init__(self, *, data: MemberWithUserPayload, guild: Guild, state: Connecti
self.guild: Guild = guild
self.joined_at: Optional[datetime.datetime] = utils.parse_time(data.get('joined_at'))
self.premium_since: Optional[datetime.datetime] = utils.parse_time(data.get('premium_since'))
self.timeout_until: Optional[datetime.datetime] = utils.parse_time(data.get('communication_disabled_until'))
self._roles: utils.SnowflakeList = utils.SnowflakeList(map(int, data['roles']))
self._client_status: Dict[Optional[str], str] = {None: 'offline'}
self.activities: Tuple[ActivityTypes, ...] = tuple()
Expand Down Expand Up @@ -662,6 +667,7 @@ async def edit(
suppress: bool = MISSING,
roles: List[discord.abc.Snowflake] = MISSING,
voice_channel: Optional[VocalGuildChannel] = MISSING,
timeout_until: Optional[datetime.datetime] = MISSING,
reason: Optional[str] = None,
) -> Optional[Member]:
"""|coro|
Expand All @@ -670,19 +676,21 @@ async def edit(

Depending on the parameter passed, this requires different permissions listed below:

+---------------+--------------------------------------+
| Parameter | Permission |
+---------------+--------------------------------------+
| nick | :attr:`Permissions.manage_nicknames` |
+---------------+--------------------------------------+
| mute | :attr:`Permissions.mute_members` |
+---------------+--------------------------------------+
| deafen | :attr:`Permissions.deafen_members` |
+---------------+--------------------------------------+
| roles | :attr:`Permissions.manage_roles` |
+---------------+--------------------------------------+
| voice_channel | :attr:`Permissions.move_members` |
+---------------+--------------------------------------+
+------------------------------+--------------------------------------+
| Parameter | Permission |
+------------------------------+--------------------------------------+
| nick | :attr:`Permissions.manage_nicknames` |
+------------------------------+--------------------------------------+
| mute | :attr:`Permissions.mute_members` |
+------------------------------+--------------------------------------+
| deafen | :attr:`Permissions.deafen_members` |
+------------------------------+--------------------------------------+
| roles | :attr:`Permissions.manage_roles` |
+------------------------------+--------------------------------------+
| voice_channel | :attr:`Permissions.move_members` |
+------------------------------+--------------------------------------+
| timeout_until | :attr:`Permissions.moderate_members` |
+------------------------------+--------------------------------------+

All parameters are optional.

Expand Down Expand Up @@ -710,6 +718,9 @@ async def edit(
voice_channel: Optional[:class:`VoiceChannel`]
The voice channel to move the member to.
Pass ``None`` to kick them from voice.
timeout_until: Optional[:class:`datetime.datetime`]
A datetime object when indicating when the user should be timed out until.
Pass ``None`` to remove their timeout.
reason: Optional[:class:`str`]
The reason for editing this member. Shows up on the audit log.

Expand Down Expand Up @@ -766,6 +777,17 @@ async def edit(
if roles is not MISSING:
payload['roles'] = tuple(r.id for r in roles)

if timeout_until is not MISSING:
if timeout_until is None:
payload['communication_disabled_until'] = None
else:
if timeout_until.tzinfo:
timestamp = timeout_until.astimezone(tz=datetime.timezone.utc).isoformat()
else:
timestamp = timeout_until.replace(tzinfo=datetime.timezone.utc).isoformat()

payload['communication_disabled_until'] = timestamp

if payload:
data = await http.edit_member(guild_id, self.id, reason=reason, **payload)
return Member(data=data, guild=self.guild, state=self._state)
Expand Down
10 changes: 9 additions & 1 deletion discord/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def all(cls: Type[P]) -> P:
"""A factory method that creates a :class:`Permissions` with all
permissions set to ``True``.
"""
return cls(0b1111111111111111111111111111111111111111)
return cls(0b11111111111111111111111111111111111111111)

@classmethod
def all_channel(cls: Type[P]) -> P:
Expand Down Expand Up @@ -565,6 +565,14 @@ def start_embedded_activities(self) -> int:
"""
return 1 << 39

@flag_value
def moderate_members(self) -> int:
""":class:`bool`: Returns ``True`` if a user can perform limited moderation actions (timeout).

.. versionadded:: 2.0
"""
return 1 << 40

PO = TypeVar('PO', bound='PermissionOverwrite')

def _augment_from_permissions(cls):
Expand Down
1 change: 1 addition & 0 deletions discord/types/member.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class PartialMember(TypedDict):
joined_at: str
deaf: str
mute: str
communication_disabled_until: str


class Member(PartialMember, total=False):
Expand Down