moved biggest_single and worst_offenses into RoboPage
This commit is contained in:
42
data.py
42
data.py
@@ -93,6 +93,7 @@ class MsgData:
|
||||
if len(msg.reactions) > 0:
|
||||
new = reaction_df(msg)
|
||||
async with self.lock:
|
||||
# self.reactions = self.reactions.join(new, how='outer')
|
||||
self.reactions = pd.concat([self.reactions, new])
|
||||
LOGGER.info(str(new.droplevel(level=0, axis=0).loc[:, 'count']))
|
||||
|
||||
@@ -114,13 +115,17 @@ class MsgData:
|
||||
|
||||
# If there were actually some message ids found
|
||||
if message_id_counts.shape[0] > 0:
|
||||
# Select the relevant messages from the self.msgs DataFrame using the filtered message ids
|
||||
res: pd.DataFrame = self.msgs.loc[message_id_counts]
|
||||
|
||||
# Add the 'count' column
|
||||
res['count'] = counts
|
||||
|
||||
# If necessary, filter by days into the past
|
||||
if days is not None and days > 0:
|
||||
res = res[res['created'] >= (datetime.today() - timedelta(days=days)).astimezone()]
|
||||
|
||||
# return the message DataFrame, sorted by created (sent) time
|
||||
return res.sort_values('created', ascending=False)
|
||||
|
||||
else:
|
||||
@@ -142,32 +147,19 @@ class MsgData:
|
||||
|
||||
def emoji_totals(self, emoji_name: str, days: int = None) -> pd.Series:
|
||||
"""Creates a Series indexed by user id and with the number of reactions with emoji_name as values"""
|
||||
return (self
|
||||
.emoji_messages(emoji_name, days)
|
||||
.groupby('user id')
|
||||
.apply(lambda gdf: gdf['count'].sum())
|
||||
.sort_values(ascending=False))
|
||||
messages = self.emoji_messages(emoji_name, days)
|
||||
if messages.shape[0] > 0:
|
||||
return (messages
|
||||
.groupby('user id')
|
||||
.apply(lambda gdf: gdf['count'].sum())
|
||||
.sort_values(ascending=False))
|
||||
else:
|
||||
raise ValueError(f'No messages found for' + \
|
||||
f' {type(emoji_name)}:{emoji_name}, {type(days)}:{days}')
|
||||
# return pd.DataFrame()
|
||||
|
||||
async def emoji_user_counts(self, client: discord.Client, emoji_name: str, days: int):
|
||||
async def emoji_user_counts(self, client: discord.Client, emoji_name: str, days: int = None):
|
||||
"""Creates a Series indexed by user display_name with the number of reactions with emoji_name as values"""
|
||||
counts: pd.Series = self.emoji_totals(emoji_name, days)
|
||||
counts.index = pd.Index([(await client.fetch_user(user_id=uid)).display_name for uid in counts.index])
|
||||
return counts
|
||||
|
||||
def worst_offsenses(self, user: str, days: int):
|
||||
cdf = self.emoji_messages('cancelled', days=days)
|
||||
cdf = cdf[cdf['display_name'].str.contains(user, case=False)]
|
||||
|
||||
if cdf.shape[0] > 0:
|
||||
res = f'{user}\'s top 5 cancellations in the last {days} days:\n'
|
||||
res += f'\n'.join(
|
||||
f'`{row["count"]:<2.0f}cancellations`\n{row["link"]}' for idx, row in cdf.iloc[:5].iterrows())
|
||||
else:
|
||||
res = f'No cancellations for {user} in the past {days} days'
|
||||
|
||||
return res
|
||||
|
||||
async def biggest_single(self, client: discord.Client, emoji: str, days: int) -> str:
|
||||
data: pd.Series = self.emoji_totals(emoji_name=emoji, days=days)
|
||||
user: discord.User = await client.fetch_user(user_id=data.index[0])
|
||||
LOGGER.info(f'User: {user.mention}')
|
||||
return f'{user.mention} with {data.iloc[0]:.0f} over the past {int(days)} days'
|
||||
|
||||
Reference in New Issue
Block a user