diff --git a/data.py b/data.py index 0aa98a4..1787d02 100644 --- a/data.py +++ b/data.py @@ -148,14 +148,10 @@ class MsgData: .apply(lambda gdf: gdf['count'].sum()) .sort_values(ascending=False)) - async def emoji_leaderboard(self, client: discord.Client, emoji_name: str, days: int): + async def emoji_user_counts(self, client: discord.Client, emoji_name: str, days: int): counts: pd.Series = self.emoji_totals(emoji_name, days) counts.index = pd.Index([(await client.fetch_user(user_id=uid)).display_name for uid in counts.index]) - width = max([len(str(s)) for s in counts.index.values]) - res = f'{emoji_name} 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 + return counts def worst_offsenses(self, user: str, days: int): cdf = self.emoji_messages('cancelled', days=days) diff --git a/robopage.py b/robopage.py index a939354..a944cbe 100644 --- a/robopage.py +++ b/robopage.py @@ -59,13 +59,8 @@ class RoboPage(discord.Client): await self.data.add_msg(message) if (m := self.leaderboard_regex.match(message.content)) is not None: - days = m.group('days') or 14 try: - await message.reply( - await self.data.emoji_leaderboard(client=self, - emoji_name=get_emoji_name(m.group('emoji')), - days=int(days)) - ) + await message.reply(await self.leaderboard(match=m)) except KeyError as e: LOGGER.exception(e) await message.reply(f"I couldn't find any {m.group('emoji')} reactions. Leave me alone!") @@ -105,12 +100,25 @@ class RoboPage(discord.Client): if hasattr(self, 'data'): await self.data.update_reaction(msg=message) + async def leaderboard(self, match: re.Match) -> str: + emoji_name = get_emoji_name(match.group('emoji')) + days = match.group('days') or 14 + counts = await self.data.emoji_user_counts(client=self, + emoji_name=emoji_name, + days=int(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 + def get_emoji_name(string: str) -> str: if (m := re.search('<:(?P\w+):(?P\d+)>', string)): string = m.group('name') return string.lower().strip() + if __name__ == '__main__': load_dotenv()