Browse Source

Add fetch_invite with with_expiration

pull/6836/head
Nadir Chowdhury 4 years ago
committed by GitHub
parent
commit
e762f55847
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      discord/client.py
  2. 3
      discord/http.py
  3. 45
      discord/invite.py

9
discord/client.py

@ -1132,7 +1132,7 @@ class Client:
# Invite management # 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| """|coro|
Gets an :class:`.Invite` from a discord.gg URL or ID. 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 Whether to include count information in the invite. This fills the
:attr:`.Invite.approximate_member_count` and :attr:`.Invite.approximate_presence_count` :attr:`.Invite.approximate_member_count` and :attr:`.Invite.approximate_presence_count`
fields. 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 Raises
------- -------
@ -1166,7 +1171,7 @@ class Client:
""" """
invite_id = utils.resolve_invite(url) 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) return Invite.from_incomplete(state=self._connection, data=data)
async def delete_invite(self, invite: Union[Invite, str]) -> None: async def delete_invite(self, invite: Union[Invite, str]) -> None:

3
discord/http.py

@ -975,9 +975,10 @@ class HTTPClient:
return self.request(r, reason=reason, json=payload) 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 = { params = {
'with_counts': int(with_counts), 'with_counts': int(with_counts),
'with_expiration': int(with_expiration),
} }
return self.request(Route('GET', '/invites/{invite_id}', invite_id=invite_id), params=params) return self.request(Route('GET', '/invites/{invite_id}', invite_id=invite_id), params=params)

45
discord/invite.py

@ -219,23 +219,25 @@ class Invite(Hashable):
The following table illustrates what methods will obtain the attributes: The following table illustrates what methods will obtain the attributes:
+------------------------------------+----------------------------------------------------------+ +------------------------------------+------------------------------------------------------------+
| Attribute | Method | | Attribute | Method |
+====================================+==========================================================+ +====================================+============================================================+
| :attr:`max_age` | :meth:`abc.GuildChannel.invites`\, :meth:`Guild.invites` | | :attr:`max_age` | :meth:`abc.GuildChannel.invites`\, :meth:`Guild.invites` |
+------------------------------------+----------------------------------------------------------+ +------------------------------------+------------------------------------------------------------+
| :attr:`max_uses` | :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:`created_at` | :meth:`abc.GuildChannel.invites`\, :meth:`Guild.invites` |
+------------------------------------+----------------------------------------------------------+ +------------------------------------+------------------------------------------------------------+
| :attr:`temporary` | :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:`uses` | :meth:`abc.GuildChannel.invites`\, :meth:`Guild.invites` |
+------------------------------------+----------------------------------------------------------+ +------------------------------------+------------------------------------------------------------+
| :attr:`approximate_member_count` | :meth:`Client.fetch_invite` | | :attr:`approximate_member_count` | :meth:`Client.fetch_invite` with `with_counts` enabled |
+------------------------------------+----------------------------------------------------------+ +------------------------------------+------------------------------------------------------------+
| :attr:`approximate_presence_count` | :meth:`Client.fetch_invite` | | :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. 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`] approximate_presence_count: Optional[:class:`int`]
The approximate number of members currently active in the guild. The approximate number of members currently active in the guild.
This includes idle, dnd, online, and invisible members. Offline members are excluded. 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`] channel: Union[:class:`abc.GuildChannel`, :class:`Object`, :class:`PartialInviteChannel`]
The channel the invite is for. The channel the invite is for.
target_user: Optional[:class:`User`] target_user: Optional[:class:`User`]
@ -295,6 +303,7 @@ class Invite(Hashable):
'_state', '_state',
'approximate_member_count', 'approximate_member_count',
'approximate_presence_count', 'approximate_presence_count',
'expires_at',
) )
BASE = 'https://discord.gg' BASE = 'https://discord.gg'
@ -311,6 +320,8 @@ class Invite(Hashable):
self.max_uses = data.get('max_uses') self.max_uses = data.get('max_uses')
self.approximate_presence_count = data.get('approximate_presence_count') self.approximate_presence_count = data.get('approximate_presence_count')
self.approximate_member_count = data.get('approximate_member_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') inviter_data = data.get('inviter')
self.inviter = None if inviter_data is None else self._state.store_user(inviter_data) self.inviter = None if inviter_data is None else self._state.store_user(inviter_data)

Loading…
Cancel
Save