Implement anonymous posting through direct messages

master
Nikola Forró 5 years ago
parent 919b3f1bcb
commit ae2723aeb1

@ -1,4 +1,5 @@
import datetime import datetime
import io
import re import re
import time import time
@ -18,8 +19,6 @@ ANNOUNCEMENT_PATTERN = re.compile(r'''^
Come\s+and\s+say\s+hi\s+at\s+(?P<url>.+)\s+! Come\s+and\s+say\s+hi\s+at\s+(?P<url>.+)\s+!
$''', re.VERBOSE) $''', re.VERBOSE)
EMOJI_PATTERN = re.compile(r'lilial1\w+')
def cooldown(retries, timeout, failure): def cooldown(retries, timeout, failure):
def do_cooldown(function): def do_cooldown(function):
@ -70,6 +69,17 @@ class DiscordClient(discord.Client):
] ]
super(DiscordClient, self).__init__() 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<c>:)?\w+(?(c):)', repl, s)
async def start_(self): async def start_(self):
token = self.config['Discord'].get('token') token = self.config['Discord'].get('token')
await self.start(token) await self.start(token)
@ -78,6 +88,9 @@ class DiscordClient(discord.Client):
self.logger.info('Logged in as {0}'.format(self.user.name)) self.logger.info('Logged in as {0}'.format(self.user.name))
async def on_message(self, message): async def on_message(self, message):
if isinstance(message.channel, discord.DMChannel):
await self._process_dm(message)
return
await self._process_announcement(message) await self._process_announcement(message)
server = message.guild.id if message.guild else None server = message.guild.id if message.guild else None
for pattern, action in self.supported_commands: for pattern, action in self.supported_commands:
@ -86,12 +99,7 @@ class DiscordClient(discord.Client):
await action(server, message.author.id, message, **m.groupdict()) await action(server, message.author.id, message, **m.groupdict())
for cmd, resp in self.extra_commands.items(): for cmd, resp in self.extra_commands.items():
if cmd == message.content: if cmd == message.content:
def repl(m): resp = self._replace_emojis(resp, guild=message.guild)
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)
await message.channel.send(resp.format(user=message.author.mention)) await message.channel.send(resp.format(user=message.author.mention))
async def on_member_join(self, member): async def on_member_join(self, member):
@ -140,6 +148,22 @@ class DiscordClient(discord.Client):
except discord.errors.Forbidden: except discord.errors.Forbidden:
pass 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): async def _process_announcement(self, message):
announcer = self.config['Discord'].getint('announcer') announcer = self.config['Discord'].getint('announcer')
channels = self.config['Discord'].get('announcement_channels').split(',') channels = self.config['Discord'].get('announcement_channels').split(',')

@ -20,6 +20,8 @@ announcement_channels = 383778221004554249,352145992256061440
# #gifted_alerts # #gifted_alerts
gifted_sub_notice_channel = 479792436080672789 gifted_sub_notice_channel = 479792436080672789
gifted_sub_threshold = 60 gifted_sub_threshold = 60
# #test
anon_channel = 448504662396502036
[Twitter] [Twitter]
consumer_key = __TWITTER_CONSUMER_KEY__ consumer_key = __TWITTER_CONSUMER_KEY__

Loading…
Cancel
Save