mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-06-07 12:18:59 +00:00
Refactor Guild to support type hints
This patch also does the following: * Sets some parameters to be positional only * Changes Guild.edit to use the MISSING sentinel * Changes the various create_channel methods to be type safe * Changes many parameters from Optional[T] to use MISSING * Changes Guild.create_role to use MISSING sentinel This refactor is mostly partial but lays a decent foundation
This commit is contained in:
parent
2247fbb23a
commit
7dccbace78
@ -48,7 +48,6 @@ __all__ = (
|
|||||||
'CategoryChannel',
|
'CategoryChannel',
|
||||||
'StoreChannel',
|
'StoreChannel',
|
||||||
'GroupChannel',
|
'GroupChannel',
|
||||||
'_channel_factory',
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
@ -1834,18 +1833,19 @@ class GroupChannel(discord.abc.Messageable, Hashable):
|
|||||||
|
|
||||||
await self._state.http.leave_group(self.id)
|
await self._state.http.leave_group(self.id)
|
||||||
|
|
||||||
def _channel_factory(channel_type):
|
def _coerce_channel_type(value: Union[ChannelType, int]) -> ChannelType:
|
||||||
value = try_enum(ChannelType, channel_type)
|
if isinstance(value, ChannelType):
|
||||||
|
return value
|
||||||
|
return try_enum(ChannelType, value)
|
||||||
|
|
||||||
|
def _guild_channel_factory(channel_type: Union[ChannelType, int]):
|
||||||
|
value = _coerce_channel_type(channel_type)
|
||||||
if value is ChannelType.text:
|
if value is ChannelType.text:
|
||||||
return TextChannel, value
|
return TextChannel, value
|
||||||
elif value is ChannelType.voice:
|
elif value is ChannelType.voice:
|
||||||
return VoiceChannel, value
|
return VoiceChannel, value
|
||||||
elif value is ChannelType.private:
|
|
||||||
return DMChannel, value
|
|
||||||
elif value is ChannelType.category:
|
elif value is ChannelType.category:
|
||||||
return CategoryChannel, value
|
return CategoryChannel, value
|
||||||
elif value is ChannelType.group:
|
|
||||||
return GroupChannel, value
|
|
||||||
elif value is ChannelType.news:
|
elif value is ChannelType.news:
|
||||||
return TextChannel, value
|
return TextChannel, value
|
||||||
elif value is ChannelType.store:
|
elif value is ChannelType.store:
|
||||||
@ -1854,3 +1854,12 @@ def _channel_factory(channel_type):
|
|||||||
return StageChannel, value
|
return StageChannel, value
|
||||||
else:
|
else:
|
||||||
return None, value
|
return None, value
|
||||||
|
|
||||||
|
def _channel_factory(channel_type: Union[ChannelType, int]):
|
||||||
|
cls, value = _guild_channel_factory(channel_type)
|
||||||
|
if value is ChannelType.private:
|
||||||
|
return DMChannel, value
|
||||||
|
elif value is ChannelType.group:
|
||||||
|
return GroupChannel, value
|
||||||
|
else:
|
||||||
|
return cls, value
|
||||||
|
916
discord/guild.py
916
discord/guild.py
File diff suppressed because it is too large
Load Diff
@ -44,6 +44,7 @@ from .mentions import AllowedMentions
|
|||||||
from .partial_emoji import PartialEmoji
|
from .partial_emoji import PartialEmoji
|
||||||
from .message import Message
|
from .message import Message
|
||||||
from .channel import *
|
from .channel import *
|
||||||
|
from .channel import _channel_factory
|
||||||
from .raw_models import *
|
from .raw_models import *
|
||||||
from .member import Member
|
from .member import Member
|
||||||
from .role import Role
|
from .role import Role
|
||||||
|
Loading…
x
Reference in New Issue
Block a user