From 117e9930e4b78003de36e66e76f52bcaec5544e8 Mon Sep 17 00:00:00 2001 From: jsl12 Date: Mon, 2 Aug 2021 15:03:23 -0500 Subject: [PATCH] added top cancellation function --- robopage.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/robopage.py b/robopage.py index 6d669d8..c713423 100644 --- a/robopage.py +++ b/robopage.py @@ -49,7 +49,11 @@ class RoboPage(discord.Client): if message.author != self.user: if 'most cancelled' in message.content: msg: discord.Message = await message.reply('Hold please...') - await msg.reply(await self.get_cancelled_totals(limit=1000)) + await message.reply(await self.get_cancelled_totals(limit=1000, days=14)) + + elif (m := re.search('top cancelled (?P\w+)', message.content)) is not None: + msg: discord.Message = await message.reply('Hold please...') + await message.reply(await self.top_cancellations(user=m.group('name'), limit=1000, days=14)) for joke in self.jokes: if (scan_res := joke.scan(message)): @@ -57,9 +61,24 @@ class RoboPage(discord.Client): await joke.respond(message, self, scan_res) async def get_cancelled_totals(self, limit=1000, days: int = 90): - msg_df, react_df = await get_and_save('messages.db', client=self, limit=limit, days=days) + msg_df, react_df = await get_and_save(self.db_path, client=self, limit=limit, days=days) res = cancelled_totals(cancellations(msg_df, react_df, days=days)) - return report_string(res.iloc[:5]) + res = f'Cancellation totals, past {days} days\n' + report_string(res.iloc[:5]) + return res + + async def top_cancellations(self, user: str, limit: int, days: int): + msg_df, react_df = await get_and_save(self.db_path, client=self, limit=limit, days=days) + + cdf = cancellations(msg_df, react_df, days=days) + cdf = cdf[cdf['display_name'].str.contains(user, case=False)] + + if cdf.shape[0] > 0: + res = f'{user}\'s top 5 cancellations in the last {days} days:\n' + res += f'\n'.join(f'`{row["count"]} cancellations`\n{row["link"]}' for idx, row in cdf.iloc[:5].iterrows()) + else: + res = f'No cancellations in the past {days} days' + + return res class Joke: @@ -173,6 +192,7 @@ if __name__ == '__main__': async def on_ready(): print(f'{client.user} has connected to Discord!') # await client.handle_ready() + # msg_df, react_df = await get_and_save('messages.db', client=client, limit=5000, days=90) @client.event