From 6166cbc2e76f52d7127074e330ed65aef54fa84f Mon Sep 17 00:00:00 2001 From: Rapptz Date: Mon, 13 Feb 2017 22:15:39 -0500 Subject: [PATCH] [commands] Add commands.clean_content converter. --- discord/ext/commands/converter.py | 62 ++++++++++++++++++++++++++++++- discord/ext/commands/core.py | 2 +- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/discord/ext/commands/converter.py b/discord/ext/commands/converter.py index d89b163e3..ce52caab6 100644 --- a/discord/ext/commands/converter.py +++ b/discord/ext/commands/converter.py @@ -30,10 +30,12 @@ import re import inspect from .errors import BadArgument, NoPrivateMessage +from .view import StringView __all__ = [ 'Converter', 'MemberConverter', 'UserConverter', 'TextChannelConverter', 'InviteConverter', 'RoleConverter', - 'GameConverter', 'ColourConverter', 'VoiceChannelConverter' ] + 'GameConverter', 'ColourConverter', 'VoiceChannelConverter', + 'clean_content' ] def _get_from_guilds(bot, getter, argument): result = None @@ -258,3 +260,61 @@ class EmojiConverter(IDConverter): raise BadArgument('Emoji "{}" not found.'.format(self.argument)) return result + +class clean_content(Converter): + def __init__(self, *, fix_channel_mentions=False, use_nicknames=True): + self.fix_channel_mentions = fix_channel_mentions + self.use_nicknames = use_nicknames + + def convert(self): + message = self.ctx.message + transformations = {} + + if self.fix_channel_mentions: + transformations.update( + ('<#%s>' % channel.id, '#' + channel.name) + for channel in message.channel_mentions + ) + + if self.use_nicknames: + transformations.update( + ('<@%s>' % member.id, '@' + member.display_name) + for member in message.mentions + ) + + transformations.update( + ('<@!%s>' % member.id, '@' + member.display_name) + for member in message.mentions + ) + else: + transformations.update( + ('<@%s>' % member.id, '@' + member.name) + for member in message.mentions + ) + + transformations.update( + ('<@!%s>' % member.id, '@' + member.name) + for member in message.mentions + ) + + transformations.update( + ('<@&%s>' % role.id, '@' + role.name) + for role in message.role_mentions + ) + + def repl(obj): + return transformations.get(obj.group(0), '') + + pattern = re.compile('|'.join(transformations.keys())) + result = pattern.sub(repl, self.argument) + + transformations = { + '@everyone': '@\u200beveryone', + '@here': '@\u200bhere' + } + + def repl2(obj): + return transformations.get(obj.group(0), '') + + pattern = re.compile('|'.join(transformations.keys())) + return pattern.sub(repl2, result) diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index 2c683b241..b335e87ba 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -199,7 +199,7 @@ class Command: if converter is bool: return _convert_to_bool(argument) - if converter.__module__.startswith('discord.'): + if converter.__module__.startswith('discord.') and not converter.__module__.endswith('converter'): converter = getattr(converters, converter.__name__ + 'Converter') if inspect.isclass(converter) and issubclass(converter, converters.Converter):