diff --git a/clients/discord.py b/clients/discord.py index e69228a..af1b9ac 100644 --- a/clients/discord.py +++ b/clients/discord.py @@ -1,4 +1,5 @@ import datetime +import io import re import time @@ -18,8 +19,6 @@ ANNOUNCEMENT_PATTERN = re.compile(r'''^ Come\s+and\s+say\s+hi\s+at\s+(?P.+)\s+! $''', re.VERBOSE) -EMOJI_PATTERN = re.compile(r'lilial1\w+') - def cooldown(retries, timeout, failure): def do_cooldown(function): @@ -70,6 +69,17 @@ class DiscordClient(discord.Client): ] super(DiscordClient, self).__init__() + def _replace_emojis(self, s, guild=None): + def repl(m): + name = m.group(0).strip(':') + emoji = None + if guild: + emoji = discord.utils.get(guild.emojis, name=name) + if not emoji: + emoji = discord.utils.get(self.emojis, name=name) + return str(emoji) if emoji else m.group(0) + return re.sub(r'(?P:)?\w+(?(c):)', repl, s) + async def start_(self): token = self.config['Discord'].get('token') await self.start(token) @@ -78,6 +88,9 @@ class DiscordClient(discord.Client): self.logger.info('Logged in as {0}'.format(self.user.name)) async def on_message(self, message): + if isinstance(message.channel, discord.DMChannel): + await self._process_dm(message) + return await self._process_announcement(message) server = message.guild.id if message.guild else None for pattern, action in self.supported_commands: @@ -86,12 +99,7 @@ class DiscordClient(discord.Client): await action(server, message.author.id, message, **m.groupdict()) for cmd, resp in self.extra_commands.items(): if cmd == message.content: - def repl(m): - emoji = discord.utils.get(message.guild.emojis, name=m.group(0)) - if not emoji: - emoji = discord.utils.get(self.get_all_emojis(), name=m.group(0)) - return str(emoji) if emoji else m.string - resp = EMOJI_PATTERN.sub(repl, resp) + resp = self._replace_emojis(resp, guild=message.guild) await message.channel.send(resp.format(user=message.author.mention)) async def on_member_join(self, member): @@ -140,6 +148,22 @@ class DiscordClient(discord.Client): except discord.errors.Forbidden: pass + async def _process_dm(self, message): + anon_channel = self.config['Discord'].getint('anon_channel') + anon_channel = self.get_channel(anon_channel) + member = anon_channel.guild.get_member(message.channel.recipient.id) + if not member: + return + if not member.permissions_in(anon_channel).send_messages: + return + files = [] + for attachment in message.attachments: + with io.BytesIO() as f: + await attachment.save(f) + files.append(discord.File(f, filename=attachment.filename, spoiler=attachment.is_spoiler())) + content = self._replace_emojis(message.content, guild=anon_channel.guild) + await anon_channel.send(content, files=files) + async def _process_announcement(self, message): announcer = self.config['Discord'].getint('announcer') channels = self.config['Discord'].get('announcement_channels').split(',') diff --git a/settings.cfg.example b/settings.cfg.example index 67443dc..576c474 100644 --- a/settings.cfg.example +++ b/settings.cfg.example @@ -20,6 +20,8 @@ announcement_channels = 383778221004554249,352145992256061440 # #gifted_alerts gifted_sub_notice_channel = 479792436080672789 gifted_sub_threshold = 60 +# #test +anon_channel = 448504662396502036 [Twitter] consumer_key = __TWITTER_CONSUMER_KEY__