diff --git a/data.py b/data.py index 43cd372..0aa98a4 100644 --- a/data.py +++ b/data.py @@ -82,27 +82,28 @@ class MsgData: LOGGER.info(f'Added message id {message.id} from {message.author}: {message.content}') async def update_reaction(self, msg: discord.Message): - async with self.lock: - # Drop all the reactions for this message id, if there are any - try: + # Drop all the reactions for this message id, if there are any + try: + async with self.lock: self.reactions.drop(msg.id, level=0, axis=0, inplace=True) - except KeyError as e: - pass + except KeyError as e: + pass - # If there are reactions on the message after the change - if len(msg.reactions) > 0: - new = reaction_df(msg) + # If there are reactions on the message after the change + if len(msg.reactions) > 0: + new = reaction_df(msg) + async with self.lock: self.reactions = pd.concat([self.reactions, new]) - LOGGER.info(str(new.droplevel(level=0, axis=0).loc[:, 'count'])) + LOGGER.info(str(new.droplevel(level=0, axis=0).loc[:, 'count'])) - if msg.id not in self.msgs.index: - await self.add_msg(msg) + if msg.id not in self.msgs.index: + await self.add_msg(msg) - return new + return new def emoji_messages(self, emoji_name: str, days: int = None) -> pd.DataFrame: """Creates a DataFrame of the messages that have reactions with a certain emoji. Includes a 'count' column""" - counts: pd.DataFrame = self.emoji_counts(emoji_name) + counts: pd.Series = self.emoji_counts(emoji_name) # Get the ids of messages that that have the targeted emoji message_id_counts: pd.Index = counts.index.drop_duplicates() @@ -117,10 +118,11 @@ class MsgData: res['count'] = counts - if days is not None: + if days is not None and days > 0: res = res[res['created'] >= (datetime.today() - timedelta(days=days)).astimezone()] return res.sort_values('created', ascending=False) + else: raise KeyError(f'No messages found with {emoji_name} reactions') diff --git a/robopage.py b/robopage.py index a4fe34d..ca3954b 100644 --- a/robopage.py +++ b/robopage.py @@ -1,9 +1,8 @@ -import json import logging import os import re -from pathlib import Path from typing import Union + import discord from discord import RawReactionActionEvent, RawReactionClearEmojiEvent from dotenv import load_dotenv @@ -28,16 +27,13 @@ class RoboPage(discord.Client): self.jokes = list(attrs) self.most_regex = re.compile( "^who is the most (?P\w+)(?: in the past (?P\d+) days)?\??$", - re.IGNORECASE + re.IGNORECASE & re.UNICODE, ) self.leaderboard_regex = re.compile( - '^most (?P\w+)(?=(?:.+)?leaderboard)(?:.+(?P\d+) days)?', - re.IGNORECASE + '^most (?P.+)(?=(?:.+)?leaderboard)(?:.+(?P\d+) days)?', + re.IGNORECASE & re.UNICODE ) - with Path('gifs.json').open('r') as file: - self.gifs = json.load(file) - def run(self): return super().run(os.getenv('DISCORD_TOKEN')) @@ -48,9 +44,10 @@ class RoboPage(discord.Client): self.data: data.MsgData = await data.MsgData.create( client=self, - # limit=5000, - limit=20, - # days=30, + limit=5000, + days=30, + # limit=500, + # days=3, ) self.data.to_sql('messages.db') LOGGER.info(f'{self.data.msgs.shape[0]} messages total') @@ -62,11 +59,11 @@ class RoboPage(discord.Client): await self.data.add_msg(message) if (m := self.leaderboard_regex.match(message.content)) is not None: - days = int(m.group('days')) or 14 - emoji = m.group('emoji').lower() + days = m.group('days') or 14 + emoji = m.group('emoji').lower().strip() try: await message.reply( - await self.data.emoji_leaderboard(client=self, emoji_name=emoji, days=days) + await self.data.emoji_leaderboard(client=self, emoji_name=emoji, days=int(days)) ) except KeyError as e: LOGGER.exception(e) @@ -74,14 +71,16 @@ class RoboPage(discord.Client): return elif (m := self.most_regex.match(message.content)) is not None: - days = int(m.group('days')) or 14 - emoji = m.group('emoji').lower() + days = m.group('days') or 14 + emoji = m.group('emoji').lower().strip() try: await message.reply( - await self.data.biggest_single(client=self, emoji=emoji, days=days)) + await self.data.biggest_single(client=self, emoji=emoji, days=int(days))) except IndexError as e: await message.reply('NObody') return + else: + LOGGER.warning(f'No self.data attribute') for joke in self.jokes: if (scan_res := joke.scan(message)):