Skip to content

Commit

Permalink
Add fetch_invite with with_expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
NCPlayz committed May 1, 2021
1 parent 4fcbe75 commit e762f55
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
9 changes: 7 additions & 2 deletions discord/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ async def create_guild(self, name: str, region: Optional[VoiceRegion] = None, ic

# 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.
Expand All @@ -1151,6 +1151,11 @@ async def fetch_invite(self, url: Union[Invite, str], *, with_counts: bool = Tru
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
-------
Expand All @@ -1166,7 +1171,7 @@ async def fetch_invite(self, url: Union[Invite, str], *, with_counts: bool = Tru
"""

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:
Expand Down
3 changes: 2 additions & 1 deletion discord/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,9 +975,10 @@ def create_invite(self, channel_id, *, reason=None, **options):

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)

Expand Down
45 changes: 28 additions & 17 deletions discord/invite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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`]
Expand Down Expand Up @@ -295,6 +303,7 @@ class Invite(Hashable):
'_state',
'approximate_member_count',
'approximate_presence_count',
'expires_at',
)

BASE = 'https://discord.gg'
Expand All @@ -311,6 +320,8 @@ def __init__(self, *, state, data: InvitePayload):
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)
Expand Down

0 comments on commit e762f55

Please sign in to comment.