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):
|
async def update_reaction(self, client: discord.Client, payload: RawReactionActionEvent):
|
||||||
payload.emoji: discord.PartialEmoji = convert_emoji(payload.emoji)
|
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)
|
chan: discord.TextChannel = await client.fetch_channel(channel_id=payload.channel_id)
|
||||||
msg: discord.Message = await chan.fetch_message(payload.message_id)
|
msg: discord.Message = await chan.fetch_message(payload.message_id)
|
||||||
idx = (msg.id, payload.emoji.name)
|
|
||||||
|
|
||||||
for reaction in msg.reactions:
|
with self.lock:
|
||||||
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:
|
try:
|
||||||
async with self.lock:
|
self.reactions.drop(msg.id, level=0, axis=0)
|
||||||
self.reactions = self.reactions.drop(idx, axis=0)
|
|
||||||
except KeyError as e:
|
except KeyError as e:
|
||||||
LOGGER.info(f'{idx} not in index')
|
LOGGER.warning(e)
|
||||||
else:
|
|
||||||
LOGGER.info(f'Dropped {idx}')
|
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):
|
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)
|
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: isinstance(c, discord.TextChannel), channels)
|
||||||
channels = filter(lambda c: c.category.name != 'Archive', channels)
|
channels = filter(lambda c: c.category.name != 'Archive', channels)
|
||||||
channels = sorted(channels, key=lambda c: (c.category.name, c.name))
|
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}')
|
LOGGER.info(f'{channel.category.name} #{channel.name}')
|
||||||
if 'after' not in kwargs:
|
if 'after' not in kwargs:
|
||||||
kwargs['after'] = (datetime.today() - timedelta(days=days))
|
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]):
|
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):
|
async def reaction_series(msg: discord.Message):
|
||||||
|
if len(msg.reactions) > 0:
|
||||||
return pd.DataFrame([
|
return pd.DataFrame([
|
||||||
await reaction_dict(r)
|
await reaction_dict(r)
|
||||||
for r in msg.reactions
|
for r in msg.reactions
|
||||||
@@ -204,7 +195,7 @@ async def reaction_series(msg: discord.Message):
|
|||||||
|
|
||||||
async def reaction_dict(r: discord.Reaction) -> Dict:
|
async def reaction_dict(r: discord.Reaction) -> Dict:
|
||||||
is_emoji = isinstance(r.emoji, (discord.Emoji, discord.PartialEmoji))
|
is_emoji = isinstance(r.emoji, (discord.Emoji, discord.PartialEmoji))
|
||||||
LOGGER.info(repr(r.emoji))
|
# LOGGER.info(repr(r.emoji))
|
||||||
return {
|
return {
|
||||||
'msg id': r.message.id,
|
'msg id': r.message.id,
|
||||||
'emoji': r.emoji.name if is_emoji else r.emoji.encode('unicode-escape').decode('ascii'),
|
'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():
|
async def on_ready():
|
||||||
# print(len(list(client.get_all_members())))
|
# print(len(list(client.get_all_members())))
|
||||||
await client.handle_ready()
|
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
|
@client.event
|
||||||
@@ -90,19 +90,13 @@ if __name__ == '__main__':
|
|||||||
@client.event
|
@client.event
|
||||||
async def on_raw_reaction_add(payload):
|
async def on_raw_reaction_add(payload):
|
||||||
LOGGER.info(payload)
|
LOGGER.info(payload)
|
||||||
try:
|
|
||||||
await client.data.update_reaction(payload=payload, client=client)
|
await client.data.update_reaction(payload=payload, client=client)
|
||||||
except AttributeError as e:
|
|
||||||
LOGGER.info(f'Robopage not initialized yet')
|
|
||||||
|
|
||||||
|
|
||||||
@client.event
|
@client.event
|
||||||
async def on_raw_reaction_remove(payload):
|
async def on_raw_reaction_remove(payload):
|
||||||
LOGGER.info(payload)
|
LOGGER.info(payload)
|
||||||
try:
|
|
||||||
await client.data.update_reaction(payload=payload, client=client)
|
await client.data.update_reaction(payload=payload, client=client)
|
||||||
except AttributeError as e:
|
|
||||||
LOGGER.info(f'Robopage not initialized yet')
|
|
||||||
|
|
||||||
|
|
||||||
client.run()
|
client.run()
|
||||||
|
|||||||
Reference in New Issue
Block a user