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

Add support for guild incidents #9590

Merged
merged 15 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
82 changes: 82 additions & 0 deletions discord/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
__all__ = (
'Guild',
'BanEntry',
'IncidentData',
)

MISSING = utils.MISSING
Expand All @@ -109,6 +110,7 @@
Guild as GuildPayload,
RolePositionUpdate as RolePositionUpdatePayload,
GuildFeature,
IncidentData as IncidentDataPayload,
)
from .types.threads import (
Thread as ThreadPayload,
Expand Down Expand Up @@ -152,6 +154,84 @@
filesize: int


class IncidentData:
"""Represents the guild incident actions or "Security Actions" as its called in the UI.

.. versionadded:: 2.4

Attributes
------------
guild: :class:`Guild`
The guild this incident data belongs to.
invites_disabled_until: Optional[:class:`datetime.datetime`]
An aware datetime object that specifies the date and time in UTC that invites will be enabled again.
This will be set to ``None`` if invites are not disabled.
dms_disabled_until: Optional[:class:`datetime.datetime`]
An aware datetime object that specifies the date and time in UTC that direct messages will be allowed again.
This will be set to ``None`` if direct messages are not disabled.
"""

__slots__ = ('guild', 'invites_disabled_until', 'dms_disabled_until')

def __init__(self, *, guild: Guild, data: IncidentDataPayload) -> None:
self.guild: Guild = guild
self._from_data(data)

def _from_data(self, data: IncidentDataPayload) -> None:
self.invites_disabled_until: Optional[datetime.datetime] = utils.parse_time(data.get('invites_disabled_until'))
self.dms_disabled_until: Optional[datetime.datetime] = utils.parse_time(data.get('dms_disabled_until'))

def __repr__(self) -> str:
return f'<IncidentData invites_disabled_until={self.invites_disabled_until!r} dms_disabled_until={self.dms_disabled_until!r}>'

async def edit(
self,
*,
invites_disabled_until: Optional[datetime.datetime] = MISSING,
dms_disabled_until: Optional[datetime.datetime] = MISSING,
) -> IncidentData:
r"""|coro|

Edits the guild's security actions.

You must have :attr:`~Permissions.manage_guild` to edit the actions.

Passing ``None`` to any will revert all actions.

Parameters
------------
invites_disabled_until: Optional[:class:`datetime.datetime`]
The time when invites should be enabled again, or ``None`` to remove the timeout.
This must be a timezone-aware datetime object. Consider using :func:`utils.utcnow`.
dms_disabled_until: Optional[:class:`datetime.datetime`]
The time when direct messages should be allowed again, or ``None`` to remove the timeout.
This must be a timezone-aware datetime object. Consider using :func:`utils.utcnow`.
"""
payload: dict[str, Any] = {}
if invites_disabled_until is not MISSING:
if invites_disabled_until is None:
payload['invites_disabled_until'] = None
else:
if invites_disabled_until.tzinfo is None:
raise TypeError(
'invites_disabled_until must be an aware datetime. Consider using discord.utils.utcnow() or datetime.datetime.now().astimezone() for local time.'
)
payload['invites_disabled_until'] = invites_disabled_until.isoformat()

if dms_disabled_until is not MISSING:
if dms_disabled_until is None:
payload['dms_disabled_until'] = None
else:
if dms_disabled_until.tzinfo is None:
raise TypeError(
'dms_disabled_until must be an aware datetime. Consider using discord.utils.utcnow() or datetime.datetime.now().astimezone() for local time.'
)
payload['dms_disabled_until'] = dms_disabled_until.isoformat()

data = await self.guild._state.http.edit_incident_actions(self.guild.id, payload) # type: ignore # ???
return self.__class__(guild=self.guild, data=data)


class Guild(Hashable):
"""Represents a Discord guild.

Expand Down Expand Up @@ -320,6 +400,7 @@
'premium_progress_bar_enabled',
'_safety_alerts_channel_id',
'max_stage_video_users',
'incidents_data',
)

_PREMIUM_GUILD_LIMITS: ClassVar[Dict[Optional[int], _GuildLimit]] = {
Expand Down Expand Up @@ -509,6 +590,7 @@
self.owner_id: Optional[int] = utils._get_as_snowflake(guild, 'owner_id')
self._large: Optional[bool] = None if self._member_count is None else self._member_count >= 250
self._afk_channel_id: Optional[int] = utils._get_as_snowflake(guild, 'afk_channel_id')
self.incidents_data: Optional[IncidentData] = IncidentData(guild=self, data=guild.get('incidents_data', {}) or {}) # type: ignore # it's fine

Check warning on line 593 in discord/guild.py

View workflow job for this annotation

GitHub Actions / check 3.x

Unnecessary "# type: ignore" comment

if 'channels' in guild:
channels = guild['channels']
Expand Down
3 changes: 3 additions & 0 deletions discord/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,9 @@ def edit_widget(
) -> Response[widget.WidgetSettings]:
return self.request(Route('PATCH', '/guilds/{guild_id}/widget', guild_id=guild_id), json=payload, reason=reason)

def edit_incident_actions(self, guild_id: Snowflake, payload: guild.IncidentData) -> Response[guild.IncidentData]:
return self.request(Route('PUT', '/guilds/{guild_id}/incident-actions', guild_id=guild_id), json=payload)

# Invite management

def create_invite(
Expand Down
6 changes: 6 additions & 0 deletions discord/types/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class UnavailableGuild(TypedDict):
unavailable: NotRequired[bool]


class IncidentData(TypedDict):
invites_disabled_until: NotRequired[Optional[str]]
dms_disabled_until: NotRequired[Optional[str]]


DefaultMessageNotificationLevel = Literal[0, 1]
ExplicitContentFilterLevel = Literal[0, 1, 2]
MFALevel = Literal[0, 1]
Expand Down Expand Up @@ -97,6 +102,7 @@ class _BaseGuildPreview(UnavailableGuild):
stickers: List[GuildSticker]
features: List[GuildFeature]
description: Optional[str]
incidents_data: Optional[IncidentData]


class _GuildPreviewUnique(TypedDict):
Expand Down
Loading