improved error handling

This commit is contained in:
2021-08-13 12:10:15 -05:00
parent 52dbb8d17b
commit de6e5414eb
3 changed files with 18 additions and 11 deletions

View File

@@ -83,7 +83,8 @@ class MsgData:
LOGGER.warning(e) LOGGER.warning(e)
if (new := await reaction_series(msg=msg)) is not None: if (new := await reaction_series(msg=msg)) is not None:
self.reactions = pd.concat([self.reactions, new.set_index(['msg id', 'emoji'])]) new = new.set_index(['msg id', 'emoji'])
self.reactions = pd.concat([self.reactions, new])
LOGGER.info(f'\n{str(new)}') LOGGER.info(f'\n{str(new)}')
def emoji_messages(self, emoji_name: str, days: int): def emoji_messages(self, emoji_name: str, days: int):

16
msg.py
View File

@@ -84,24 +84,28 @@ def emoji_messages(msg_df, react_df, emoji_name: str, days: int = 10) -> pd.Data
if emoji_name in cached_emojis: if emoji_name in cached_emojis:
reactions = react_df.loc[pd.IndexSlice[:, emoji_name], :] reactions = react_df.loc[pd.IndexSlice[:, emoji_name], :]
reacted_msgs = msg_df.loc[reactions.index.get_level_values(0).to_list()] reacted_msgs = msg_df.loc[reactions.index.get_level_values(0).to_list()]
reacted_msgs = reacted_msgs[~reacted_msgs.index.duplicated()].sort_index()
if reacted_msgs.shape[0] == 0: if reacted_msgs.shape[0] == 0:
LOGGER.error(f'No messages found with {emoji_name} reactions') LOGGER.error(f'No messages found with {emoji_name} reactions')
else: else:
LOGGER.info( LOGGER.info(
f'Found {reacted_msgs.shape[0]} messages for the leaderboard, {reactions["count"].sum():.0f} reactions total') f'Found {reacted_msgs.shape[0]} messages for the leaderboard, ' + \
f'{reactions["count"].sum():.0f} reactions total'
)
try: try:
reacted_msgs['count'] = reacted_msgs.index.to_series().apply( reacted_msgs['count'] = reacted_msgs.index.to_series().apply(
lambda idx: reactions.loc[pd.IndexSlice[idx, emoji_name], 'count']) lambda idx: reactions.loc[pd.IndexSlice[idx, emoji_name], 'count'])
except pandas.errors.InvalidIndexError as e: except pandas.errors.InvalidIndexError as e:
LOGGER.error(f'{e}\n{reacted_msgs[reacted_msgs.index.duplicated()]}') LOGGER.error(f'{e}\n{reacted_msgs[reacted_msgs.index.duplicated()]}')
raise
else:
reacted_msgs = reacted_msgs[
reacted_msgs['created'] >= (datetime.today() - timedelta(days=days)).astimezone()]
reacted_msgs = reacted_msgs[ reacted_msgs = reacted_msgs.sort_values('count', ascending=False)
reacted_msgs['created'] >= (datetime.today() - timedelta(days=days)).astimezone()]
reacted_msgs = reacted_msgs.sort_values('count', ascending=False) return reacted_msgs
return reacted_msgs
else: else:
LOGGER.error(f'Emoji not found in reactions DataFrame: {emoji_name}') LOGGER.error(f'Emoji not found in reactions DataFrame: {emoji_name}')

View File

@@ -39,7 +39,7 @@ class RoboPage(discord.Client):
self.data: data.MsgData = await data.MsgData.create( self.data: data.MsgData = await data.MsgData.create(
client=self, client=self,
limit=3000, limit=5000,
# limit=20, # limit=20,
days=30, days=30,
) )
@@ -48,20 +48,22 @@ class RoboPage(discord.Client):
# await alive() # await alive()
async def handle_message(self, message): async def handle_message(self, message):
await self.data.add_msg(message) if hasattr(self, 'data'):
await self.data.add_msg(message)
if message.author != self.user: if message.author != self.user:
if (m := self.leaderboard_regex.match(message.content)) is not None: if (m := self.leaderboard_regex.match(message.content)) is not None:
try: try:
await message.reply(self.data.emoji_leaderboard(emoji_name=m.group('emoji'), days=14)) await message.reply(self.data.emoji_leaderboard(emoji_name=m.group('emoji').lower(), days=14))
except KeyError as e: except KeyError as e:
LOGGER.exception(e)
await message.reply(f"I couldn't find any {m.group('emoji')} reactions. Leave me alone!") await message.reply(f"I couldn't find any {m.group('emoji')} reactions. Leave me alone!")
elif (m := self.most_regex.match(message.content)) is not None: elif (m := self.most_regex.match(message.content)) is not None:
days = m.group('days') or 14 days = m.group('days') or 14
try: try:
await message.reply( await message.reply(
await self.data.biggest_single(client=self, emoji=m.group('emoji'), days=int(days)) await self.data.biggest_single(client=self, emoji=m.group('emoji').lower(), days=int(days))
) )
except IndexError as e: except IndexError as e:
await message.reply('NObody') await message.reply('NObody')