Browse Source

[commands] Add GuildChannelConverter

pull/6881/head
sudosnok 4 years ago
committed by GitHub
parent
commit
2a6d79078e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 43
      discord/ext/commands/converter.py
  2. 3
      docs/ext/commands/api.rst
  3. 3
      docs/ext/commands/commands.rst

43
discord/ext/commands/converter.py

@ -71,6 +71,7 @@ __all__ = (
'CategoryChannelConverter',
'IDConverter',
'StoreChannelConverter',
'GuildChannelConverter',
'clean_content',
'Greedy',
'run_converters',
@ -376,8 +377,8 @@ class MessageConverter(IDConverter[discord.Message]):
raise ChannelNotReadable(channel)
class TextChannelConverter(IDConverter[discord.TextChannel]):
"""Converts to a :class:`~discord.TextChannel`.
class GuildChannelConverter(IDConverter[discord.abc.GuildChannel]):
"""Converts to a :class:`~discord.abc.GuildChannel`.
All lookups are via the local guild. If in a DM context, then the lookup
is done by the global cache.
@ -386,14 +387,13 @@ class TextChannelConverter(IDConverter[discord.TextChannel]):
1. Lookup by ID.
2. Lookup by mention.
3. Lookup by name
3. Lookup by name.
.. versionchanged:: 1.5
Raise :exc:`.ChannelNotFound` instead of generic :exc:`.BadArgument`
.. versionadded:: 2.0
"""
async def convert(self, ctx: Context, argument: str) -> discord.TextChannel:
return self._resolve_channel(ctx, argument, ctx.guild.text_channels, discord.TextChannel)
async def convert(self, ctx: Context, argument: str) -> discord.abc.GuildChannel:
return self._resolve_channel(ctx, argument, ctx.guild.channels, discord.abc.GuildChannel)
@staticmethod
def _resolve_channel(ctx: Context, argument: str, iterable: Iterable[CT], type: Type[CT]) -> CT:
@ -426,6 +426,26 @@ class TextChannelConverter(IDConverter[discord.TextChannel]):
return result
class TextChannelConverter(IDConverter[discord.TextChannel]):
"""Converts to a :class:`~discord.TextChannel`.
All lookups are via the local guild. If in a DM context, then the lookup
is done by the global cache.
The lookup strategy is as follows (in order):
1. Lookup by ID.
2. Lookup by mention.
3. Lookup by name
.. versionchanged:: 1.5
Raise :exc:`.ChannelNotFound` instead of generic :exc:`.BadArgument`
"""
async def convert(self, ctx: Context, argument: str) -> discord.TextChannel:
return GuildChannelConverter._resolve_channel(ctx, argument, ctx.guild.text_channels, discord.TextChannel)
class VoiceChannelConverter(IDConverter[discord.VoiceChannel]):
"""Converts to a :class:`~discord.VoiceChannel`.
@ -443,7 +463,7 @@ class VoiceChannelConverter(IDConverter[discord.VoiceChannel]):
"""
async def convert(self, ctx: Context, argument: str) -> discord.VoiceChannel:
return TextChannelConverter._resolve_channel(ctx, argument, ctx.guild.voice_channels, discord.VoiceChannel)
return GuildChannelConverter._resolve_channel(ctx, argument, ctx.guild.voice_channels, discord.VoiceChannel)
class StageChannelConverter(IDConverter[discord.StageChannel]):
@ -462,7 +482,7 @@ class StageChannelConverter(IDConverter[discord.StageChannel]):
"""
async def convert(self, ctx: Context, argument: str) -> discord.StageChannel:
return TextChannelConverter._resolve_channel(ctx, argument, ctx.guild.stage_channels, discord.StageChannel)
return GuildChannelConverter._resolve_channel(ctx, argument, ctx.guild.stage_channels, discord.StageChannel)
class CategoryChannelConverter(IDConverter[discord.CategoryChannel]):
@ -482,7 +502,7 @@ class CategoryChannelConverter(IDConverter[discord.CategoryChannel]):
"""
async def convert(self, ctx: Context, argument: str) -> discord.CategoryChannel:
return TextChannelConverter._resolve_channel(ctx, argument, ctx.guild.categories, discord.CategoryChannel)
return GuildChannelConverter._resolve_channel(ctx, argument, ctx.guild.categories, discord.CategoryChannel)
class StoreChannelConverter(IDConverter[discord.StoreChannel]):
@ -501,7 +521,7 @@ class StoreChannelConverter(IDConverter[discord.StoreChannel]):
"""
async def convert(self, ctx: Context, argument: str) -> discord.StoreChannel:
return TextChannelConverter._resolve_channel(ctx, argument, ctx.guild.channels, discord.StoreChannel)
return GuildChannelConverter._resolve_channel(ctx, argument, ctx.guild.channels, discord.StoreChannel)
class ColourConverter(Converter[discord.Colour]):
@ -930,6 +950,7 @@ CONVERTER_MAPPING: Dict[Type[Any], Any] = {
discord.PartialEmoji: PartialEmojiConverter,
discord.CategoryChannel: CategoryChannelConverter,
discord.StoreChannel: StoreChannelConverter,
discord.abc.GuildChannel: GuildChannelConverter,
}

3
docs/ext/commands/api.rst

@ -288,6 +288,9 @@ Converters
.. autoclass:: discord.ext.commands.PartialMessageConverter
:members:
.. autoclass:: discord.ext.commands.GuildChannelConverter
:members:
.. autoclass:: discord.ext.commands.TextChannelConverter
:members:

3
docs/ext/commands/commands.rst

@ -379,6 +379,7 @@ A lot of discord models work out of the gate as a parameter:
- :class:`User`
- :class:`Message` (since v1.1)
- :class:`PartialMessage` (since v1.7)
- :class:`abc.GuildChannel` (since 2.0)
- :class:`TextChannel`
- :class:`VoiceChannel`
- :class:`StageChannel` (since v1.7)
@ -411,6 +412,8 @@ converter is given below:
+--------------------------+-------------------------------------------------+
| :class:`PartialMessage` | :class:`~ext.commands.PartialMessageConverter` |
+--------------------------+-------------------------------------------------+
| :class:`.GuildChannel` | :class:`~ext.commands.GuildChannelConverter` |
+--------------------------+-------------------------------------------------+
| :class:`TextChannel` | :class:`~ext.commands.TextChannelConverter` |
+--------------------------+-------------------------------------------------+
| :class:`VoiceChannel` | :class:`~ext.commands.VoiceChannelConverter` |

Loading…
Cancel
Save