From: CMDR furrycat Date: Thu, 3 Nov 2016 12:36:35 +0000 (+0000) Subject: Added parse_channel(). X-Git-Url: http://git.furryclan.net/?a=commitdiff_plain;h=bc13267cec230c4b224f0f431b5885975ffc772e;p=furrycat%2Fcatbot.git Added parse_channel(). --- diff --git a/bot.py b/bot.py index 5c7cab3..3ac1952 100644 --- a/bot.py +++ b/bot.py @@ -102,6 +102,57 @@ def unparse_seconds(seconds): interval += '{}s'.format(seconds) return interval +def parse_channel(text, *, text_ok = True, voice_ok = True): + channel_type = '{}, {}'.format('text' if text_ok else 'non-text', 'voice' if voice_ok else 'non-voice') + log.debug("Parsing {} channel {}".format(channel_type, text)) + + # Private message to user. + if text == 'private': + log.warning('Parsed private channel. Caller should check for private!') + if not text_ok: + log.warning("Requested voice channel but private channels are text!") + return None + return text + + # <#mention>, #id or id. + m = re.match(r'<#(\d+)>', text) + if m is None: + m = re.match(r'#?(\d+)', text) + if m is not None: + channel_id = m.group(1) + log.debug('Channel ID {}.'.format(channel_id)) + channel = client.get_channel(channel_id) + if not channel: + log.warning("Can't find channel with ID {}!".format(channel_id)) + return None + if not text_ok and channel.type != discord.ChannelType.voice: + log.warning('Parsed channel {} of wrong type!'.format(channel_id)) + return None + if not voice_ok and channel.type == discord.ChannelType.voice: + log.warning('Parsed channel {} of wrong type!'.format(channel_id)) + return None + return channel + + # #name or name. + if text[0] == '#': + name = text[1:] + else: + name = text + for channel in client.get_all_channels(): + # Note that text channels are all lowercase; voice channels are title case. + if channel.name.lower() == name: + log.debug('Channel {} has ID {} on server {}.'.format(channel.name, channel.id, channel.server.id)) + if not text_ok and channel.type != discord.ChannelType.voice: + log.warning('Parsed channel {} of wrong type!'.format(channel_id)) + continue + if not voice_ok and channel.type == discord.ChannelType.voice: + log.warning('Parsed channel {} of wrong type!'.format(channel_id)) + continue + return channel + + log.warning("Can't find {} channel {}!".format(channel_type, text)) + return None + def open_url(url): if sys.version_info >= (3, 0): # Specify our own user agent as Cloudflare doesn't seem to like the urllib one