From e459c84e1eb77318d4b52739d87ffd7cf3b41fc5 Mon Sep 17 00:00:00 2001 From: jsl12 Date: Wed, 11 Aug 2021 10:49:07 -0500 Subject: [PATCH] added a generic command for getting the single person with the most of a certain emoji --- msg.py | 11 ++++++++++- robopage.py | 11 +++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/msg.py b/msg.py index 561e006..f7649ca 100644 --- a/msg.py +++ b/msg.py @@ -139,6 +139,15 @@ class MsgData: ) return res + async def biggest_single(self, client: discord.Client, emoji: str, days: int) -> str: + data = self.emoji_totals(emoji_name=emoji, days=days) + username = data.index[0] + reacted_msgs = self.emoji_messages(emoji_name=emoji, days=days) + d = reacted_msgs.set_index('display_name')['user id'].drop_duplicates().to_dict() + user: discord.User = await client.fetch_user(user_id=d[username]) + LOGGER.info(f'User: {user.mention}') + return f'{user.mention} with {data.iloc[0]["total"]:.0f} over the past {int(days)} days' + async def message_df(client: discord.Client, **kwargs): return pd.DataFrame( [message_dict(m) async for m in message_gen(client, **kwargs)] @@ -213,7 +222,7 @@ def emoji_messages(msg_df, react_df, emoji_name: str, days: int = 10) -> pd.Data def emoji_totals(edf: pd.DataFrame) -> pd.DataFrame: - totals = edf.groupby('display_name').sum()['count'].sort_values(ascending=False) + totals = edf.groupby('display_name').sum()['count'].sort_values(ascending=False).apply(int) max_channels = ( edf .groupby(['display_name', 'channel']) diff --git a/robopage.py b/robopage.py index ef83d6d..6734bb3 100644 --- a/robopage.py +++ b/robopage.py @@ -25,6 +25,7 @@ class RoboPage(discord.Client): attrs = map(lambda n: getattr(jokes, n)(), attrs) self.jokes = list(attrs) self.lock = Lock() + self.emoji_regex = re.compile("^who is the most (?P\w+)(?: in the past (?P\d+) days)?\??$", re.IGNORECASE) def run(self): return super().run(os.getenv('DISCORD_TOKEN')) @@ -37,9 +38,12 @@ class RoboPage(discord.Client): self.data: msg.MsgData = await msg.MsgData.create( client=self, limit=3000, + # limit=20, days=14, ) self.data.to_sql('messages.db') + LOGGER.info(f'{self.data.msgs.shape[0]} messages total') + # await alive() async def handle_message(self, message): await self.data.add_msg(message) @@ -52,8 +56,11 @@ class RoboPage(discord.Client): async with self.data.lock: await message.reply(self.data.worst_offsenses(user=m.group('name'), days=14)) - elif 'biggest daddy' in message.content: - await message.reply(self.data.biggest_daddy(days=14)) + elif (m := self.emoji_regex.match(message.content)) is not None: + days = m.group('days') or 14 + await message.reply( + await self.data.biggest_single(client=self, emoji=m.group('emoji'), days=int(days)) + ) for joke in self.jokes: if (scan_res := joke.scan(message)):