From e762f5584765f2862b7e00fd3a4ad4c097da8e42 Mon Sep 17 00:00:00 2001 From: Nadir Chowdhury Date: Sat, 1 May 2021 12:46:16 +0100 Subject: [PATCH] Add fetch_invite with with_expiration --- discord/client.py | 9 +++++++-- discord/http.py | 3 ++- discord/invite.py | 45 ++++++++++++++++++++++++++++----------------- 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/discord/client.py b/discord/client.py index 8c6f4936d..71e4bf6b9 100644 --- a/discord/client.py +++ b/discord/client.py @@ -1132,7 +1132,7 @@ class Client: # Invite management - async def fetch_invite(self, url: Union[Invite, str], *, with_counts: bool = True) -> Invite: + async def fetch_invite(self, url: Union[Invite, str], *, with_counts: bool = True, with_expiration: bool = True) -> Invite: """|coro| Gets an :class:`.Invite` from a discord.gg URL or ID. @@ -1151,6 +1151,11 @@ class Client: Whether to include count information in the invite. This fills the :attr:`.Invite.approximate_member_count` and :attr:`.Invite.approximate_presence_count` fields. + with_expiration: :class:`bool` + Whether to include the expiration date of the invite. This fills the + :attr:`.Invite.expires_at` field. + + .. versionadded:: 2.0 Raises ------- @@ -1166,7 +1171,7 @@ class Client: """ invite_id = utils.resolve_invite(url) - data = await self.http.get_invite(invite_id, with_counts=with_counts) + data = await self.http.get_invite(invite_id, with_counts=with_counts, with_expiration=with_expiration) return Invite.from_incomplete(state=self._connection, data=data) async def delete_invite(self, invite: Union[Invite, str]) -> None: diff --git a/discord/http.py b/discord/http.py index 3bf134256..0eb481245 100644 --- a/discord/http.py +++ b/discord/http.py @@ -975,9 +975,10 @@ class HTTPClient: return self.request(r, reason=reason, json=payload) - def get_invite(self, invite_id, *, with_counts=True): + def get_invite(self, invite_id, *, with_counts=True, with_expiration=True): params = { 'with_counts': int(with_counts), + 'with_expiration': int(with_expiration), } return self.request(Route('GET', '/invites/{invite_id}', invite_id=invite_id), params=params) diff --git a/discord/invite.py b/discord/invite.py index 95fcb0b78..7a80d3012 100644 --- a/discord/invite.py +++ b/discord/invite.py @@ -219,23 +219,25 @@ class Invite(Hashable): The following table illustrates what methods will obtain the attributes: - +------------------------------------+----------------------------------------------------------+ - | Attribute | Method | - +====================================+==========================================================+ - | :attr:`max_age` | :meth:`abc.GuildChannel.invites`\, :meth:`Guild.invites` | - +------------------------------------+----------------------------------------------------------+ - | :attr:`max_uses` | :meth:`abc.GuildChannel.invites`\, :meth:`Guild.invites` | - +------------------------------------+----------------------------------------------------------+ - | :attr:`created_at` | :meth:`abc.GuildChannel.invites`\, :meth:`Guild.invites` | - +------------------------------------+----------------------------------------------------------+ - | :attr:`temporary` | :meth:`abc.GuildChannel.invites`\, :meth:`Guild.invites` | - +------------------------------------+----------------------------------------------------------+ - | :attr:`uses` | :meth:`abc.GuildChannel.invites`\, :meth:`Guild.invites` | - +------------------------------------+----------------------------------------------------------+ - | :attr:`approximate_member_count` | :meth:`Client.fetch_invite` | - +------------------------------------+----------------------------------------------------------+ - | :attr:`approximate_presence_count` | :meth:`Client.fetch_invite` | - +------------------------------------+----------------------------------------------------------+ + +------------------------------------+------------------------------------------------------------+ + | Attribute | Method | + +====================================+============================================================+ + | :attr:`max_age` | :meth:`abc.GuildChannel.invites`\, :meth:`Guild.invites` | + +------------------------------------+------------------------------------------------------------+ + | :attr:`max_uses` | :meth:`abc.GuildChannel.invites`\, :meth:`Guild.invites` | + +------------------------------------+------------------------------------------------------------+ + | :attr:`created_at` | :meth:`abc.GuildChannel.invites`\, :meth:`Guild.invites` | + +------------------------------------+------------------------------------------------------------+ + | :attr:`temporary` | :meth:`abc.GuildChannel.invites`\, :meth:`Guild.invites` | + +------------------------------------+------------------------------------------------------------+ + | :attr:`uses` | :meth:`abc.GuildChannel.invites`\, :meth:`Guild.invites` | + +------------------------------------+------------------------------------------------------------+ + | :attr:`approximate_member_count` | :meth:`Client.fetch_invite` with `with_counts` enabled | + +------------------------------------+------------------------------------------------------------+ + | :attr:`approximate_presence_count` | :meth:`Client.fetch_invite` with `with_counts` enabled | + +------------------------------------+------------------------------------------------------------+ + | :attr:`expires_at` | :meth:`Client.fetch_invite` with `with_expiration` enabled | + +------------------------------------+------------------------------------------------------------+ If it's not in the table above then it is available by all methods. @@ -267,6 +269,12 @@ class Invite(Hashable): approximate_presence_count: Optional[:class:`int`] The approximate number of members currently active in the guild. This includes idle, dnd, online, and invisible members. Offline members are excluded. + expires_at: Optional[:class:`datetime.datetime`] + The expiration date of the invite. If the value is ``None`` when received through + `Client.fetch_invite` with `with_expiration` enabled, the invite will never expire. + + .. versionadded:: 2.0 + channel: Union[:class:`abc.GuildChannel`, :class:`Object`, :class:`PartialInviteChannel`] The channel the invite is for. target_user: Optional[:class:`User`] @@ -295,6 +303,7 @@ class Invite(Hashable): '_state', 'approximate_member_count', 'approximate_presence_count', + 'expires_at', ) BASE = 'https://discord.gg' @@ -311,6 +320,8 @@ class Invite(Hashable): self.max_uses = data.get('max_uses') self.approximate_presence_count = data.get('approximate_presence_count') self.approximate_member_count = data.get('approximate_member_count') + expires_at = data.get('expires_at', None) + self.expires_at = parse_time(expires_at) if expires_at else None inviter_data = data.get('inviter') self.inviter = None if inviter_data is None else self._state.store_user(inviter_data)