added most cancelled channel

This commit is contained in:
2021-07-16 19:48:46 -05:00
parent 650bbfeb88
commit 7fae9cb02a

View File

@@ -9,6 +9,24 @@ from dotenv import load_dotenv
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
LIL_STINKY_ID = 704043422276780072
def cancelled_totals(df):
totals = df.groupby('display_name')['cancelled'].sum().sort_values(ascending=False)
name_width = df['display_name'].apply(str).apply(len).max()
df2 = df.groupby(['display_name', 'channel']).sum().reset_index().groupby('display_name').max()
df2.columns = ['channel most cancelled', 'channel cancel count']
df2['total cancelled'] = df2.index.to_series().apply(lambda n: df[df['display_name'] == n]['cancelled'].sum())
df2['link'] = df2.index.to_series().apply(lambda n: df[df['channel'] == df2.loc[n].iloc[0]]['channel link'].iloc[0])
res = 'Cancellation totals:\n'
res += '\n'.join(
f'`{total} {name.ljust(name_width)}` most in {df2.loc[name, "link"]}' for name, total in totals.iteritems()
)
return res
class RoboPage(discord.Client): class RoboPage(discord.Client):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@@ -24,7 +42,7 @@ class RoboPage(discord.Client):
for c in self.get_all_channels(): for c in self.get_all_channels():
if c.guild.name == 'Family Dinner' and \ if c.guild.name == 'Family Dinner' and \
isinstance(c, discord.TextChannel): isinstance(c, discord.TextChannel):
if c.id != 704043422276780072: if c.id != LIL_STINKY_ID:
print(f'Scanning in {c.name} for cancellations') print(f'Scanning in {c.name} for cancellations')
for m in await c.history(**kwargs).flatten(): for m in await c.history(**kwargs).flatten():
if len(m.reactions) > 0: if len(m.reactions) > 0:
@@ -39,20 +57,16 @@ class RoboPage(discord.Client):
[{ [{
'display_name': m.author.display_name, 'display_name': m.author.display_name,
'cancelled': count, 'cancelled': count,
'message': m.content 'message': m.content,
'channel': m.channel.name,
'channel link': f'<#{m.channel.id}>',
'link': m.jump_url
} }
async for m, count in self.reaction_messages('cancelled', limit=limit)] async for m, count in self.reaction_messages('cancelled', limit=limit)]
) )
df.to_csv('msgs.csv', index=False) df.to_csv('msgs.csv', index=False)
return cancelled_totals(df)
s = df.groupby('display_name')['cancelled'].sum().sort_values(ascending=False)
res = f'Cancellation totals, past {limit} messages in each channel:\n`'
for name, total in s.iteritems():
res += f'{total} {name}\n'
res = res[:-1]
res += '`'
return res
def run(self): def run(self):
return super().run(os.getenv('DISCORD_TOKEN')) return super().run(os.getenv('DISCORD_TOKEN'))
@@ -65,7 +79,7 @@ class RoboPage(discord.Client):
if message.author != self.user: if message.author != self.user:
if 'most cancelled' in message.content: if 'most cancelled' in message.content:
msg: discord.Message = await message.reply('Hold please...') msg: discord.Message = await message.reply('Hold please...')
await msg.reply(await self.get_cancelled_totals()) await msg.reply(await self.get_cancelled_totals(limit=1000))
for joke in self.jokes: for joke in self.jokes:
if (scan_res := joke.scan(message)): if (scan_res := joke.scan(message)):
@@ -170,11 +184,7 @@ if __name__ == '__main__':
async def on_ready(): async def on_ready():
print(f'{client.user} has connected to Discord!') print(f'{client.user} has connected to Discord!')
await client.handle_ready() await client.handle_ready()
# for c in client.get_all_channels(): # print(await client.get_cancelled_totals(limit=100))
# if isinstance(c, discord.TextChannel):
# c: discord.TextChannel = c
# print(c.name)
# print(await client.get_cancelled_totals())
@client.event @client.event
async def on_message(message: discord.Message): async def on_message(message: discord.Message):