Browse Source

[commands] Add commands.clean_content converter.

pull/1278/head
Rapptz 8 years ago
parent
commit
6166cbc2e7
  1. 62
      discord/ext/commands/converter.py
  2. 2
      discord/ext/commands/core.py

62
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)

2
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):

Loading…
Cancel
Save