From 89d9bdcf7bfe9f1978e4a1c6afac792c05651e19 Mon Sep 17 00:00:00 2001 From: jsl12 Date: Wed, 11 Aug 2021 16:35:38 -0500 Subject: [PATCH] greatly simplified emoji tracking --- msg.py | 43 +++++++++++++++++-------------------------- robopage.py | 12 +++--------- 2 files changed, 20 insertions(+), 35 deletions(-) diff --git a/msg.py b/msg.py index 4392869..775d174 100644 --- a/msg.py +++ b/msg.py @@ -74,28 +74,18 @@ class MsgData: async def update_reaction(self, client: discord.Client, payload: RawReactionActionEvent): payload.emoji: discord.PartialEmoji = convert_emoji(payload.emoji) - LOGGER.info(repr(payload.emoji)) chan: discord.TextChannel = await client.fetch_channel(channel_id=payload.channel_id) msg: discord.Message = await chan.fetch_message(payload.message_id) - idx = (msg.id, payload.emoji.name) - for reaction in msg.reactions: - if isinstance(reaction.emoji, discord.Emoji) and reaction.emoji.name == payload.emoji.name: - reactions = pd.Series(await reaction_dict(reaction)) - async with self.lock: - self.reactions.loc[pd.IndexSlice[idx], :] = reactions - LOGGER.info(f'Added {str(idx)}, {int(self.reactions.loc[pd.IndexSlice[idx], "count"])} total') - break - else: - # only reaches here if the remove action was to take off the last reaction of that type - if payload.event_type == 'REACTION_REMOVE': - try: - async with self.lock: - self.reactions = self.reactions.drop(idx, axis=0) - except KeyError as e: - LOGGER.info(f'{idx} not in index') - else: - LOGGER.info(f'Dropped {idx}') + with self.lock: + try: + self.reactions.drop(msg.id, level=0, axis=0) + except KeyError as e: + 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'])]) + LOGGER.info(f'\n{str(new)}') def emoji_messages(self, emoji_name: str, days: int): return emoji_messages(msg_df=self.msgs, react_df=self.reactions, emoji_name=emoji_name, days=days) @@ -167,7 +157,7 @@ async def message_gen(client: discord.Client, limit=20, days: int = 90, **kwargs channels = filter(lambda c: isinstance(c, discord.TextChannel), channels) channels = filter(lambda c: c.category.name != 'Archive', channels) channels = sorted(channels, key=lambda c: (c.category.name, c.name)) - for channel in channels: + for channel in channels[:5]: LOGGER.info(f'{channel.category.name} #{channel.name}') if 'after' not in kwargs: kwargs['after'] = (datetime.today() - timedelta(days=days)) @@ -192,19 +182,20 @@ def message_dict(m: discord.Message) -> Dict: async def reaction_df(msgs: Iterable[discord.Message]): - return pd.concat([await reaction_series(msg) for msg in msgs]).set_index(['msg id', 'emoji']) + return pd.concat([await reaction_series(msg) for msg in msgs if len(msg.reactions) > 0]).set_index(['msg id', 'emoji']) async def reaction_series(msg: discord.Message): - return pd.DataFrame([ - await reaction_dict(r) - for r in msg.reactions - ]) + if len(msg.reactions) > 0: + return pd.DataFrame([ + await reaction_dict(r) + for r in msg.reactions + ]) async def reaction_dict(r: discord.Reaction) -> Dict: is_emoji = isinstance(r.emoji, (discord.Emoji, discord.PartialEmoji)) - LOGGER.info(repr(r.emoji)) + # LOGGER.info(repr(r.emoji)) return { 'msg id': r.message.id, 'emoji': r.emoji.name if is_emoji else r.emoji.encode('unicode-escape').decode('ascii'), diff --git a/robopage.py b/robopage.py index 8ab7daa..b8d04c0 100644 --- a/robopage.py +++ b/robopage.py @@ -79,7 +79,7 @@ if __name__ == '__main__': async def on_ready(): # print(len(list(client.get_all_members()))) await client.handle_ready() - print('\n'.join(client.data.reactions.index.get_level_values(1).drop_duplicates().sort_values())) + # print('\n'.join(client.data.reactions.index.get_level_values(1).drop_duplicates().sort_values())) @client.event @@ -90,19 +90,13 @@ if __name__ == '__main__': @client.event async def on_raw_reaction_add(payload): LOGGER.info(payload) - try: - await client.data.update_reaction(payload=payload, client=client) - except AttributeError as e: - LOGGER.info(f'Robopage not initialized yet') + await client.data.update_reaction(payload=payload, client=client) @client.event async def on_raw_reaction_remove(payload): LOGGER.info(payload) - try: - await client.data.update_reaction(payload=payload, client=client) - except AttributeError as e: - LOGGER.info(f'Robopage not initialized yet') + await client.data.update_reaction(payload=payload, client=client) client.run()