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)
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)}')
def emoji_messages(self, emoji_name: str, days: int):

8
msg.py
View File

@@ -84,18 +84,22 @@ def emoji_messages(msg_df, react_df, emoji_name: str, days: int = 10) -> pd.Data
if emoji_name in cached_emojis:
reactions = react_df.loc[pd.IndexSlice[:, emoji_name], :]
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:
LOGGER.error(f'No messages found with {emoji_name} reactions')
else:
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:
reacted_msgs['count'] = reacted_msgs.index.to_series().apply(
lambda idx: reactions.loc[pd.IndexSlice[idx, emoji_name], 'count'])
except pandas.errors.InvalidIndexError as e:
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()]

View File

@@ -39,7 +39,7 @@ class RoboPage(discord.Client):
self.data: data.MsgData = await data.MsgData.create(
client=self,
limit=3000,
limit=5000,
# limit=20,
days=30,
)
@@ -48,20 +48,22 @@ class RoboPage(discord.Client):
# await alive()
async def handle_message(self, message):
if hasattr(self, 'data'):
await self.data.add_msg(message)
if message.author != self.user:
if (m := self.leaderboard_regex.match(message.content)) is not None:
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:
LOGGER.exception(e)
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:
days = m.group('days') or 14
try:
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:
await message.reply('NObody')