Files
kwaylon/robopage.py

114 lines
3.3 KiB
Python

import logging
import os
import re
from threading import Lock
import discord
from dotenv import load_dotenv
import msg
from jokes import CumJoke, BlackJoke, AssJoke, DominosJoke
logging.basicConfig(level=logging.INFO)
LIL_STINKY_ID = 704043422276780072
LOGGER = logging.getLogger(__name__)
class RoboPage(discord.Client):
db_path: str = 'messages.db'
def __init__(self, *args, **kwargs):
super(RoboPage, self).__init__(*args, **kwargs)
self.jokes = [
CumJoke(),
BlackJoke(),
AssJoke(),
DominosJoke()
]
self.lock = Lock()
def run(self):
return super().run(os.getenv('DISCORD_TOKEN'))
async def handle_ready(self):
async def alive():
channel: discord.TextChannel = discord.utils.get(self.get_all_channels(), name='robotics-facility')
await channel.send(f"I'm aliiiiiive {discord.utils.get(self.emojis, name='kaylon')}")
self.data: msg.MsgData = await msg.MsgData.create(
client=self,
limit=3000,
# limit=20,
days=14,
)
LOGGER.info(str(self.data.msgs.columns))
LOGGER.info(str(self.data.reactions.columns))
async def handle_message(self, message):
await self.data.add_msg(message)
if message.author != self.user:
if 'most cancelled' in message.content:
await message.reply(self.get_cancelled_totals(days=14))
elif (m := re.search('top cancelled (?P<name>\w+)', message.content)) is not None:
async with self.data.lock:
await message.reply(self.top_cancellations(user=m.group('name'), days=14))
for joke in self.jokes:
if (scan_res := joke.scan(message)):
print(f'{joke.__class__.__name__} detected:\n{message.content}\n{scan_res}')
await joke.respond(message, self, scan_res)
def get_cancelled_totals(self, days):
res = self.data.cancellation_totals(days)
res = f'Cancellation totals, past {days} days\n' + msg.report_string(res.iloc[:5])
return res
def top_cancellations(self, user: str, days: int):
cdf = self.data.cancellations(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"]} 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
if __name__ == '__main__':
load_dotenv()
client = RoboPage()
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
await client.handle_ready()
print(client.data.cancellation_totals(14))
@client.event
async def on_message(message: discord.Message):
await client.handle_message(message)
@client.event
async def on_raw_reaction_add(payload):
LOGGER.info(payload)
await client.data.update_reaction(payload=payload, client=client)
@client.event
async def on_raw_reaction_remove(payload):
LOGGER.info(payload)
await client.data.update_reaction(payload=payload, client=client)
client.run()