Add typings for invites, templates, and bans

This commit is contained in:
Nadir Chowdhury
2021-04-10 07:55:10 +01:00
committed by GitHub
parent 3e92196a2b
commit 1efdef3ac3
7 changed files with 185 additions and 38 deletions

View File

@ -22,6 +22,9 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from __future__ import annotations
from typing import Optional, TYPE_CHECKING
from .asset import Asset
from .utils import parse_time, snowflake_time, _get_as_snowflake
from .object import Object
@ -34,6 +37,16 @@ __all__ = (
'Invite',
)
if TYPE_CHECKING:
from .types.invite import (
Invite as InvitePayload,
InviteGuild as InviteGuildPayload,
)
from .types.channel import (
PartialChannel as PartialChannelPayload,
)
import datetime
class PartialInviteChannel:
"""Represents a "partial" invite channel.
@ -72,7 +85,7 @@ class PartialInviteChannel:
__slots__ = ('id', 'name', 'type')
def __init__(self, **kwargs):
self.id = kwargs.pop('id')
self.id = int(kwargs.pop('id'))
self.name = kwargs.pop('name')
self.type = kwargs.pop('type')
@ -139,7 +152,7 @@ class PartialInviteGuild:
__slots__ = ('_state', 'features', 'icon', 'banner', 'id', 'name', 'splash', 'verification_level', 'description')
def __init__(self, state, data, id):
def __init__(self, state, data: InviteGuildPayload, id: int):
self._state = state
self.id = id
self.name = data['name']
@ -150,33 +163,33 @@ class PartialInviteGuild:
self.verification_level = try_enum(VerificationLevel, data.get('verification_level'))
self.description = data.get('description')
def __str__(self):
def __str__(self) -> str:
return self.name
def __repr__(self):
def __repr__(self) -> str:
return (
f'<{self.__class__.__name__} id={self.id} name={self.name!r} features={self.features} '
f'description={self.description!r}>'
)
@property
def created_at(self):
def created_at(self) -> datetime.datetime:
""":class:`datetime.datetime`: Returns the guild's creation time in UTC."""
return snowflake_time(self.id)
@property
def icon_url(self):
def icon_url(self) -> Asset:
""":class:`Asset`: Returns the guild's icon asset."""
return self.icon_url_as()
def is_icon_animated(self):
def is_icon_animated(self) -> bool:
""":class:`bool`: Returns ``True`` if the guild has an animated icon.
.. versionadded:: 1.4
"""
return bool(self.icon and self.icon.startswith('a_'))
def icon_url_as(self, *, format=None, static_format='webp', size=1024):
def icon_url_as(self, *, format=None, static_format='webp', size=1024) -> Asset:
"""The same operation as :meth:`Guild.icon_url_as`.
Returns
@ -187,11 +200,11 @@ class PartialInviteGuild:
return Asset._from_guild_icon(self._state, self, format=format, static_format=static_format, size=size)
@property
def banner_url(self):
def banner_url(self) -> Asset:
""":class:`Asset`: Returns the guild's banner asset."""
return self.banner_url_as()
def banner_url_as(self, *, format='webp', size=2048):
def banner_url_as(self, *, format='webp', size=2048) -> Asset:
"""The same operation as :meth:`Guild.banner_url_as`.
Returns
@ -202,11 +215,11 @@ class PartialInviteGuild:
return Asset._from_guild_image(self._state, self.id, self.banner, 'banners', format=format, size=size)
@property
def splash_url(self):
def splash_url(self) -> Asset:
""":class:`Asset`: Returns the guild's invite splash asset."""
return self.splash_url_as()
def splash_url_as(self, *, format='webp', size=2048):
def splash_url_as(self, *, format='webp', size=2048) -> Asset:
"""The same operation as :meth:`Guild.splash_url_as`.
Returns
@ -313,13 +326,13 @@ class Invite(Hashable):
BASE = 'https://discord.gg'
def __init__(self, *, state, data):
def __init__(self, *, state, data: InvitePayload):
self._state = state
self.max_age = data.get('max_age')
self.code = data.get('code')
self.code = data['code']
self.guild = data.get('guild')
self.revoked = data.get('revoked')
self.created_at = parse_time(data.get('created_at'))
self.created_at: Optional[datetime.datetime] = parse_time(data.get('created_at')) # type: ignore
self.temporary = data.get('temporary')
self.uses = data.get('uses')
self.max_uses = data.get('max_uses')
@ -346,7 +359,7 @@ class Invite(Hashable):
# As far as I know, invites always need a channel
# So this should never raise.
channel_data = data['channel']
channel_data: PartialChannelPayload = data['channel']
channel_id = int(channel_data['id'])
channel_type = try_enum(ChannelType, channel_data['type'])
channel = PartialInviteChannel(id=channel_id, name=channel_data['name'], type=channel_type)
@ -373,30 +386,30 @@ class Invite(Hashable):
data['channel'] = channel
return cls(state=state, data=data)
def __str__(self):
def __str__(self) -> str:
return self.url
def __repr__(self):
def __repr__(self) -> str:
return (
f'<Invite code={self.code!r} guild={self.guild!r} '
f'online={self.approximate_presence_count} '
f'members={self.approximate_member_count}>'
)
def __hash__(self):
def __hash__(self) -> int:
return hash(self.code)
@property
def id(self):
def id(self) -> str:
""":class:`str`: Returns the proper code portion of the invite."""
return self.code
@property
def url(self):
def url(self) -> str:
""":class:`str`: A property that retrieves the invite URL."""
return self.BASE + '/' + self.code
async def delete(self, *, reason=None):
async def delete(self, *, reason: Optional[str] = None):
"""|coro|
Revokes the instant invite.