diff options
| author | sudosnok <[email protected]> | 2021-05-07 12:37:42 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-05-07 07:37:42 -0400 |
| commit | 2a6d79078e791b0803b19d159672c501fd2b6d8a (patch) | |
| tree | 3d68715c93af6f4652b5cbb7c5b853f3a9433c46 /discord/ext/commands | |
| parent | Explicitly ignore legacy file reference errors in sphinx -n mode (diff) | |
| download | discord.py-2a6d79078e791b0803b19d159672c501fd2b6d8a.tar.xz discord.py-2a6d79078e791b0803b19d159672c501fd2b6d8a.zip | |
[commands] Add GuildChannelConverter
Diffstat (limited to 'discord/ext/commands')
| -rw-r--r-- | discord/ext/commands/converter.py | 43 |
1 files changed, 32 insertions, 11 deletions
diff --git a/discord/ext/commands/converter.py b/discord/ext/commands/converter.py index 1beba307..14dd5659 100644 --- a/discord/ext/commands/converter.py +++ b/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, } |