generalized the cancellation totaling into emoji totaling functions

This commit is contained in:
2021-08-10 22:47:06 -05:00
parent 5745c55f9d
commit 2442d508cd
2 changed files with 18 additions and 20 deletions

36
msg.py
View File

@@ -96,14 +96,14 @@ class MsgData:
else:
LOGGER.info(f'Dropped {idx}')
def cancellations(self, days: int = 14):
return cancellations(msg_df=self.msgs, react_df=self.reactions, days=days)
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)
def cancellation_totals(self, days):
return cancelled_totals(cdf=self.cancellations(days=days))
def emoji_totals(self, emoji_name: str, days: int):
return emoji_totals(edf=self.emoji_messages(emoji_name, days))
def cancellation_leaderboard(self, days, top: int = None):
df = self.cancellation_totals(days)
df = self.emoji_totals('cancelled', days)
if top is not None:
df = df.iloc[:top]
width = max(list(map(lambda s: len(str(s)), df.index.values)))
@@ -115,7 +115,7 @@ class MsgData:
return res
def worst_offsenses(self, user: str, days: int):
cdf = self.cancellations(days=days)
cdf = self.emoji_messages('cancelled', days=days)
cdf = cdf[cdf['display_name'].str.contains(user, case=False)]
if cdf.shape[0] > 0:
@@ -185,28 +185,26 @@ async def reaction_dict(r: discord.Reaction) -> Dict:
}
def cancellations(msg_df, react_df, days: int = 10) -> pd.DataFrame:
def emoji_messages(msg_df, react_df, emoji_name: str, days: int = 10) -> pd.DataFrame:
# get reactions with a cancellation emoji
cancel_reactions = react_df.loc[pd.IndexSlice[:, 'cancelled'], :]
cancel_msgs = msg_df.loc[cancel_reactions.index.get_level_values(0).to_list()]
reactions = react_df.loc[pd.IndexSlice[:, emoji_name], :]
reacted_msgs = msg_df.loc[reactions.index.get_level_values(0).to_list()]
cancel_msgs['count'] = cancel_msgs.index.to_series().apply(
lambda idx: cancel_reactions.loc[pd.IndexSlice[idx, 'cancelled'], 'count'])
reacted_msgs['count'] = reacted_msgs.index.to_series().apply(
lambda idx: reactions.loc[pd.IndexSlice[idx, emoji_name], 'count'])
# filter outdated messages
cancel_msgs = cancel_msgs[cancel_msgs['created'] >= (datetime.today() - timedelta(days=days)).astimezone()]
reacted_msgs = reacted_msgs[reacted_msgs['created'] >= (datetime.today() - timedelta(days=days)).astimezone()]
cancel_msgs = cancel_msgs.sort_values('count', ascending=False)
reacted_msgs = reacted_msgs.sort_values('count', ascending=False)
return cancel_msgs
return cancelled_msgs
return reacted_msgs
def cancelled_totals(cdf: pd.DataFrame) -> pd.DataFrame:
totals = cdf.groupby('display_name').sum()['count'].sort_values(ascending=False)
def emoji_totals(edf: pd.DataFrame) -> pd.DataFrame:
totals = edf.groupby('display_name').sum()['count'].sort_values(ascending=False)
max_channels = (
cdf
edf
.groupby(['display_name', 'channel'])
.sum()['count']
.sort_values(ascending=False)

View File

@@ -46,7 +46,7 @@ class RoboPage(discord.Client):
if message.author != self.user:
if 'most cancelled' in message.content:
await message.reply(self.data.cancellation_leaderboard(14))
await message.reply(self.data.cancellation_leaderboard(days=14))
elif (m := re.search('top cancelled (?P<name>\w+)', message.content)) is not None:
async with self.data.lock: