now with cancellation detection

This commit is contained in:
2021-07-15 22:11:38 -05:00
parent 4e62232248
commit 650bbfeb88

View File

@@ -4,6 +4,7 @@ import re
import discord import discord
import nltk import nltk
import pandas as pd
from dotenv import load_dotenv from dotenv import load_dotenv
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
@@ -17,6 +18,41 @@ class RoboPage(discord.Client):
BlackJoke(), BlackJoke(),
AssJoke() AssJoke()
] ]
print()
async def reaction_messages(self, target: str, **kwargs):
for c in self.get_all_channels():
if c.guild.name == 'Family Dinner' and \
isinstance(c, discord.TextChannel):
if c.id != 704043422276780072:
print(f'Scanning in {c.name} for cancellations')
for m in await c.history(**kwargs).flatten():
if len(m.reactions) > 0:
for r in m.reactions:
if isinstance(r.emoji, discord.Emoji) and r.emoji.name == target:
print(r.count, m.author.display_name)
yield m, r.count
async def get_cancelled_totals(self, limit=1000):
df = pd.DataFrame(
[{
'display_name': m.author.display_name,
'cancelled': count,
'message': m.content
}
async for m, count in self.reaction_messages('cancelled', limit=limit)]
)
df.to_csv('msgs.csv', index=False)
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'))
@@ -27,6 +63,10 @@ class RoboPage(discord.Client):
async def handle_message(self, message): async def handle_message(self, message):
if message.author != self.user: if message.author != self.user:
if 'most cancelled' in message.content:
msg: discord.Message = await message.reply('Hold please...')
await msg.reply(await self.get_cancelled_totals())
for joke in self.jokes: for joke in self.jokes:
if (scan_res := joke.scan(message)): if (scan_res := joke.scan(message)):
print(f'{joke.__class__.__name__} detected:\n{message.content}\n{scan_res}') print(f'{joke.__class__.__name__} detected:\n{message.content}\n{scan_res}')
@@ -57,7 +97,7 @@ class CumJoke(Joke):
'cumming', 'cumming',
'cummed' 'cummed'
] ]
return re.compile(f"(?<!\w)({'|'.join(words)})", re.IGNORECASE) return re.compile(f"(?<!\w)({'|'.join(words)})(?!\w)", re.IGNORECASE)
async def respond(self, message: discord.Message, client: discord.Client, match: re.Match): async def respond(self, message: discord.Message, client: discord.Client, match: re.Match):
if not match.group(0).startswith('be'): if not match.group(0).startswith('be'):
@@ -130,6 +170,11 @@ 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():
# 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):