modernise code

This commit is contained in:
NCPlayz 2021-04-18 22:37:42 +01:00 committed by Arthur Jovart
parent c3fb10348a
commit b654530821
No known key found for this signature in database
GPG Key ID: DE4444AAAAAAAAAA
2 changed files with 60 additions and 24 deletions

View File

@ -40,6 +40,7 @@ from .colour import *
from .integrations import * from .integrations import *
from .invite import * from .invite import *
from .template import * from .template import *
from .welcome_screen import *
from .widget import * from .widget import *
from .object import * from .object import *
from .reaction import * from .reaction import *

View File

@ -24,15 +24,33 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. DEALINGS IN THE SOFTWARE.
""" """
from __future__ import annotations
from typing import Dict, List, Optional, TYPE_CHECKING, Union, overload
from .utils import _get_as_snowflake, get from .utils import _get_as_snowflake, get
from .errors import InvalidArgument from .errors import InvalidArgument
from .partial_emoji import _EmojiTag from .partial_emoji import _EmojiTag
__all__ = (
'WelcomeChannel',
'WelcomeScreen',
)
if TYPE_CHECKING:
from .types.welcome_screen import (
WelcomeScreen as WelcomeScreenPayload,
WelcomeScreenChannel as WelcomeScreenChannelPayload,
)
from .abc import Snowflake
from .guild import Guild
from .partial_emoji import PartialEmoji
from .emoji import Emoji
class WelcomeChannel: class WelcomeChannel:
"""Represents a :class:`WelcomeScreen` welcome channel. """Represents a :class:`WelcomeScreen` welcome channel.
.. versionadded:: 1.7 .. versionadded:: 2.0
Attributes Attributes
----------- -----------
@ -43,16 +61,17 @@ class WelcomeChannel:
emoji: Optional[:class:`PartialEmoji`, :class:`Emoji`, :class:`str`] emoji: Optional[:class:`PartialEmoji`, :class:`Emoji`, :class:`str`]
The emoji used beside the channel description. The emoji used beside the channel description.
""" """
def __init__(self, *, channel, description, emoji=None):
def __init__(self, *, channel: Snowflake, description: str, emoji: Union[PartialEmoji, Emoji, str] = None):
self.channel = channel self.channel = channel
self.description = description self.description = description
self.emoji = emoji self.emoji = emoji
def __repr__(self): def __repr__(self) -> str:
return '<WelcomeChannel channel={0.channel!r} description={0.description!r} emoji={0.emoji}>'.format(self) return f'<WelcomeChannel channel={self.channel!r} description={self.description!r} emoji={self.emoji!r}>'
@classmethod @classmethod
def _from_dict(cls, *, data, guild): def _from_dict(cls, *, data: WelcomeScreenChannelPayload, guild: Guild) -> WelcomeChannel:
channel_id = _get_as_snowflake(data, 'channel_id') channel_id = _get_as_snowflake(data, 'channel_id')
channel = guild.get_channel(channel_id) channel = guild.get_channel(channel_id)
description = data['description'] description = data['description']
@ -66,9 +85,9 @@ class WelcomeChannel:
# unicode or None # unicode or None
emoji = _emoji_name emoji = _emoji_name
return cls(channel=channel, description=description, emoji=emoji) return cls(channel=channel, description=description, emoji=emoji) # type: ignore
def to_dict(self): def to_dict(self) -> Dict[str, str]:
ret = { ret = {
'channel_id': self.channel.id, 'channel_id': self.channel.id,
'description': self.description, 'description': self.description,
@ -77,18 +96,19 @@ class WelcomeChannel:
} }
if isinstance(self.emoji, _EmojiTag): if isinstance(self.emoji, _EmojiTag):
ret['emoji_id'] = self.emoji.id ret['emoji_id'] = self.emoji.id # type: ignore
ret['emoji_name'] = self.emoji.name ret['emoji_name'] = self.emoji.name # type: ignore
else: else:
# unicode or None # unicode or None
ret['emoji_name'] = self.emoji ret['emoji_name'] = self.emoji
return ret return ret
class WelcomeScreen: class WelcomeScreen:
"""Represents a :class:`Guild` welcome screen. """Represents a :class:`Guild` welcome screen.
.. versionadded:: 1.7 .. versionadded:: 2.0
Attributes Attributes
----------- -----------
@ -97,21 +117,22 @@ class WelcomeScreen:
welcome_channels: List[:class:`WelcomeChannel`] welcome_channels: List[:class:`WelcomeChannel`]
The channels shown on the welcome screen. The channels shown on the welcome screen.
""" """
def __init__(self, *, data, guild):
def __init__(self, *, data: WelcomeScreenPayload, guild: Guild):
self._state = guild._state self._state = guild._state
self._guild = guild self._guild = guild
self._store(data) self._store(data)
def _store(self, data): def _store(self, data: WelcomeScreenPayload) -> None:
self.description = data['description'] self.description = data['description']
welcome_channels = data.get('welcome_channels', []) welcome_channels = data.get('welcome_channels', [])
self.welcome_channels = [WelcomeChannel._from_dict(data=wc, guild=self._guild) for wc in welcome_channels] self.welcome_channels = [WelcomeChannel._from_dict(data=wc, guild=self._guild) for wc in welcome_channels]
def __repr__(self): def __repr__(self) -> str:
return '<WelcomeScreen description={0.description!r} welcome_channels={0.welcome_channels!r} enabled={0.enabled}>'.format(self) return f'<WelcomeScreen description={self.description!r} welcome_channels={self.welcome_channels!r} enabled={self.enabled}>'
@property @property
def enabled(self): def enabled(self) -> bool:
""":class:`bool`: Whether the welcome screen is displayed. """:class:`bool`: Whether the welcome screen is displayed.
This is equivalent to checking if ``WELCOME_SCREEN_ENABLED`` This is equivalent to checking if ``WELCOME_SCREEN_ENABLED``
@ -119,6 +140,20 @@ class WelcomeScreen:
""" """
return 'WELCOME_SCREEN_ENABLED' in self._guild.features return 'WELCOME_SCREEN_ENABLED' in self._guild.features
@overload
async def edit(
self,
*,
description: Optional[str] = ...,
welcome_channels: Optional[List[WelcomeChannel]] = ...,
enabled: Optional[bool] = ...,
) -> None:
...
@overload
async def edit(self) -> None:
...
async def edit(self, **kwargs): async def edit(self, **kwargs):
"""|coro| """|coro|