|
|
|
@ -2,7 +2,9 @@ import datetime
|
|
|
|
|
import re
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
import dateutil.parser
|
|
|
|
|
import discord
|
|
|
|
|
import fuzzywuzzy.process
|
|
|
|
|
import twitter
|
|
|
|
|
|
|
|
|
|
from commands import CommandError
|
|
|
|
@ -80,6 +82,29 @@ class DiscordClient(discord.Client):
|
|
|
|
|
if cmd == message.content:
|
|
|
|
|
await self.send_message(message.channel, resp.format(user=message.author.mention))
|
|
|
|
|
|
|
|
|
|
def _guess_gifted_subs(self, nick, time):
|
|
|
|
|
subs = self.commands.plausible_gifted_subs(time)
|
|
|
|
|
choices = {i: s['receiver'] for i, s in enumerate(subs)}
|
|
|
|
|
guesses = fuzzywuzzy.process.extractWithoutOrder(nick, choices, score_cutoff=50)
|
|
|
|
|
result = []
|
|
|
|
|
for _, score, i in guesses:
|
|
|
|
|
days = (time - dateutil.parser.parse(subs[i]['time'])).days
|
|
|
|
|
result.append((subs[i]['receiver'], score, days))
|
|
|
|
|
return sorted(result, key=lambda x: (x[1], 32 - x[2], x[0]), reverse=True)
|
|
|
|
|
|
|
|
|
|
async def _process_gifted_subs(self, user, gifted_subs):
|
|
|
|
|
header = '{0: <20} | {1: <5} | {2: <11}'.format('Twitch username', 'Match', 'When')
|
|
|
|
|
table = [header, '-' * len(header)]
|
|
|
|
|
for nick, score, days in gifted_subs:
|
|
|
|
|
table.append('{0: <20} | {1: >4}% | {2: >2} days ago'.format(nick, score, days))
|
|
|
|
|
message = ('It seems that {0} could have been gifted a sub. '
|
|
|
|
|
'Here are the most probable candidates:\n```{1}```').format(user.mention, '\n'.join(table))
|
|
|
|
|
channel = self.config['Discord'].get('gifted_sub_notice_channel')
|
|
|
|
|
try:
|
|
|
|
|
await self.send_message(discord.Object(channel), message)
|
|
|
|
|
except discord.errors.Forbidden:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
async def _process_new_member(self, message):
|
|
|
|
|
new_members_channel = self.config['Discord'].get('new_members_channel')
|
|
|
|
|
info_channel = self.config['Discord'].get('info_channel')
|
|
|
|
@ -100,6 +125,10 @@ class DiscordClient(discord.Client):
|
|
|
|
|
user=message.author.mention, info_channel=info_channel.mention))
|
|
|
|
|
except discord.errors.Forbidden:
|
|
|
|
|
pass
|
|
|
|
|
self.logger.info('Looking for gifted subs matching {0}'.format(message.author.display_name))
|
|
|
|
|
gifted_subs = self._guess_gifted_subs(message.author.display_name, datetime.datetime.utcnow())
|
|
|
|
|
if gifted_subs:
|
|
|
|
|
await self._process_gifted_subs(message.author, gifted_subs)
|
|
|
|
|
|
|
|
|
|
async def _process_announcement(self, message):
|
|
|
|
|
announcer = self.config['Discord'].get('announcer')
|
|
|
|
|