diff options
| author | Rapptz <[email protected]> | 2021-06-08 10:54:22 -0400 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2021-06-08 10:56:26 -0400 |
| commit | 7dccbace7821cf96965000e593a8006e64a99709 (patch) | |
| tree | 322dc0ca2c02dc019c3cea26b15e8e4b5bba8e7f /discord/channel.py | |
| parent | [types] Use proper type for Guild.threads (diff) | |
| download | discord.py-7dccbace7821cf96965000e593a8006e64a99709.tar.xz discord.py-7dccbace7821cf96965000e593a8006e64a99709.zip | |
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
Diffstat (limited to 'discord/channel.py')
| -rw-r--r-- | discord/channel.py | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/discord/channel.py b/discord/channel.py index 9b1825f9..c1b95752 100644 --- a/discord/channel.py +++ b/discord/channel.py @@ -48,7 +48,6 @@ __all__ = ( 'CategoryChannel', 'StoreChannel', 'GroupChannel', - '_channel_factory', ) if TYPE_CHECKING: @@ -1834,18 +1833,19 @@ class GroupChannel(discord.abc.Messageable, Hashable): await self._state.http.leave_group(self.id) -def _channel_factory(channel_type): - value = try_enum(ChannelType, channel_type) +def _coerce_channel_type(value: Union[ChannelType, int]) -> ChannelType: + 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: return TextChannel, value elif value is ChannelType.voice: return VoiceChannel, value - elif value is ChannelType.private: - return DMChannel, value elif value is ChannelType.category: return CategoryChannel, value - elif value is ChannelType.group: - return GroupChannel, value elif value is ChannelType.news: return TextChannel, value elif value is ChannelType.store: @@ -1854,3 +1854,12 @@ def _channel_factory(channel_type): return StageChannel, value else: 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 |