moved biggest_single and worst_offenses into RoboPage
This commit is contained in:
46
robopage.py
46
robopage.py
@@ -4,6 +4,7 @@ import re
|
||||
from typing import Union
|
||||
|
||||
import discord
|
||||
import pandas as pd
|
||||
from discord import RawReactionActionEvent, RawReactionClearEmojiEvent
|
||||
from dotenv import load_dotenv
|
||||
|
||||
@@ -26,7 +27,7 @@ class RoboPage(discord.Client):
|
||||
attrs = map(lambda n: getattr(jokes, n)(), attrs)
|
||||
self.jokes = list(attrs)
|
||||
self.most_regex = re.compile(
|
||||
"^who is the most (?P<emoji>\w+)(?: in the past (?P<days>\d+) days)?\??$",
|
||||
"^who is the most\s+(?P<emoji>\S+)\s*?(?:in the past (?P<days>\d+) days)?\??$",
|
||||
re.IGNORECASE & re.UNICODE,
|
||||
)
|
||||
self.leaderboard_regex = re.compile(
|
||||
@@ -43,8 +44,8 @@ class RoboPage(discord.Client):
|
||||
client=self,
|
||||
limit=5000,
|
||||
days=30,
|
||||
# limit=500,
|
||||
# days=3,
|
||||
# limit=100,
|
||||
# days=14,
|
||||
)
|
||||
self.data.to_sql(self.db_path)
|
||||
LOGGER.info(f'{self.data.msgs.shape[0]} messages total')
|
||||
@@ -64,15 +65,11 @@ class RoboPage(discord.Client):
|
||||
return
|
||||
|
||||
elif (m := self.most_regex.match(message.content)) is not None:
|
||||
days = m.group('days') or 14
|
||||
try:
|
||||
await message.reply(
|
||||
await self.data.biggest_single(client=self,
|
||||
emoji=get_emoji_name(m.group('emoji')),
|
||||
days=int(days)))
|
||||
except IndexError as e:
|
||||
await message.reply(await self.biggest_single(match=m))
|
||||
except Exception as e:
|
||||
LOGGER.exception(e)
|
||||
await message.reply('NObody')
|
||||
return
|
||||
else:
|
||||
LOGGER.warning(f'No self.data attribute')
|
||||
|
||||
@@ -100,15 +97,42 @@ class RoboPage(discord.Client):
|
||||
async def leaderboard(self, match: re.Match) -> str:
|
||||
emoji_name = get_emoji_name(match.group('emoji'))
|
||||
days = match.group('days') or 14
|
||||
days = int(days)
|
||||
counts = await self.data.emoji_user_counts(client=self,
|
||||
emoji_name=emoji_name,
|
||||
days=int(days))
|
||||
days=days)
|
||||
width = max([len(str(s)) for s in counts.index.values])
|
||||
res = f'{match.group("emoji")} totals, past {days} days\n'
|
||||
res += '\n'.join(f"`{str(name).ljust(width + 1)}with {cnt:<2.0f} total`"
|
||||
for name, cnt in counts.iteritems())
|
||||
return res
|
||||
|
||||
async def biggest_single(self, match: re.Match) -> str:
|
||||
days = match.group('days') or 14
|
||||
days = int(days)
|
||||
data: pd.Series = self.data.emoji_totals(
|
||||
emoji_name=get_emoji_name(match.group('emoji')),
|
||||
days=days
|
||||
)
|
||||
user: discord.User = await client.fetch_user(user_id=data.index[0])
|
||||
LOGGER.info(f'User: {user.mention}')
|
||||
msg = f'{user.mention} with {data.iloc[0]:.0f}x {match.group("emoji")} over the past {days} days'
|
||||
msg += '\n' + await self.worst_offsenses(user=user, days=days, top=3, emoji_str=match.group('emoji'))
|
||||
return msg
|
||||
|
||||
async def worst_offsenses(self, user: discord.User, days: int, top: int, emoji_str: str) -> str:
|
||||
cdf = self.data.emoji_messages(get_emoji_name(emoji_str), days=days)
|
||||
cdf = cdf[cdf['user id'] == user.id].sort_values('count', ascending=False).iloc[:top]
|
||||
|
||||
if cdf.shape[0] > 0:
|
||||
res = f'Top {top} {emoji_str}\n'
|
||||
res += f'\n'.join(
|
||||
f'{emoji_str}x{row["count"]:.0f}\n{row["link"]}' for idx, row in cdf.iterrows())
|
||||
else:
|
||||
res = f'No {emoji_str} for {user} in the past {days} days'
|
||||
|
||||
return res
|
||||
|
||||
|
||||
def get_emoji_name(string: str) -> str:
|
||||
if (m := re.search('<:(?P<name>\w+):(?P<id>\d+)>', string)):
|
||||
|
||||
Reference in New Issue
Block a user