greatly simplified emoji tracking
This commit is contained in:
31
msg.py
31
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':
|
||||
with self.lock:
|
||||
try:
|
||||
async with self.lock:
|
||||
self.reactions = self.reactions.drop(idx, axis=0)
|
||||
self.reactions.drop(msg.id, level=0, axis=0)
|
||||
except KeyError as e:
|
||||
LOGGER.info(f'{idx} not in index')
|
||||
else:
|
||||
LOGGER.info(f'Dropped {idx}')
|
||||
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,10 +182,11 @@ 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):
|
||||
if len(msg.reactions) > 0:
|
||||
return pd.DataFrame([
|
||||
await reaction_dict(r)
|
||||
for r in msg.reactions
|
||||
@@ -204,7 +195,7 @@ async def reaction_series(msg: discord.Message):
|
||||
|
||||
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'),
|
||||
|
||||
@@ -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')
|
||||
|
||||
|
||||
@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')
|
||||
|
||||
|
||||
client.run()
|
||||
|
||||
Reference in New Issue
Block a user