import logging import os import re from threading import Lock import discord from dotenv import load_dotenv import jokes import msg 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) attrs = filter(lambda n: n.endswith('Joke') and not n.startswith('Joke'), dir(jokes)) attrs = map(lambda n: getattr(jokes, n)(), attrs) self.jokes = list(attrs) 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, days=14, ) self.data.to_sql('messages.db') 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.data.cancellation_leaderboard(days=14)) elif (m := re.search('top cancelled (?P\w+)', message.content)) is not None: async with self.data.lock: await message.reply(self.data.worst_offsenses(user=m.group('name'), days=14)) elif 'biggest daddy' in message.content: await message.reply(self.data.biggest_daddy(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) if __name__ == '__main__': load_dotenv() client = RoboPage() @client.event async def on_ready(): # print(len(list(client.get_all_members()))) await client.handle_ready() @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()