[commands] Add CategoryChannelConverter

This commit is contained in:
Rapptz
2017-09-13 17:17:04 -04:00
parent 7df8897e3d
commit e614f6b4cd
3 changed files with 72 additions and 25 deletions

View File

@ -35,7 +35,8 @@ from .view import StringView
__all__ = [ 'Converter', 'MemberConverter', 'UserConverter',
'TextChannelConverter', 'InviteConverter', 'RoleConverter',
'GameConverter', 'ColourConverter', 'VoiceChannelConverter',
'EmojiConverter', 'IDConverter', 'clean_content' ]
'EmojiConverter','CategoryChannelConverter', 'IDConverter',
'clean_content' ]
def _get_from_guilds(bot, getter, argument):
result = None
@ -242,6 +243,46 @@ class VoiceChannelConverter(IDConverter):
return result
class CategoryChannelConverter(IDConverter):
"""Converts to a :class:`CategoryChannel`.
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
"""
@asyncio.coroutine
def convert(self, ctx, argument):
bot = ctx.bot
match = self._get_id_match(argument) or re.match(r'<#([0-9]+)>$', argument)
result = None
guild = ctx.guild
if match is None:
# not a mention
if guild:
result = discord.utils.get(guild.categories, name=argument)
else:
def check(c):
return isinstance(c, discord.CategoryChannel) and c.name == argument
result = discord.utils.find(check, bot.get_all_channels())
else:
channel_id = int(match.group(1))
if guild:
result = guild.get_channel(channel_id)
else:
result = _get_from_guilds(bot, 'get_channel', channel_id)
if not isinstance(result, discord.CategoryChannel):
raise BadArgument('Channel "{}" not found.'.format(argument))
return result
class ColourConverter(Converter):
"""Converts to a :class:`Colour`.