from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper from maubot import Plugin, MessageEvent from maubot.handlers import command import aiohttp import asyncio from bs4 import BeautifulSoup import random class ChefMou(Plugin): async def start(self) -> None: self.on_external_config_update() async def get_site_content(self): async with aiohttp.ClientSession() as session: async with session.get("https://partoches.pustule.org") as resp: text = await resp.read() return BeautifulSoup(text.decode('iso-8859-1'), 'html.parser') @command.new("chefmou") @command.argument("pattern", pass_raw=True, required=False) async def morceau(self, evt: MessageEvent, pattern: str) -> None: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) soup = await self.get_site_content() s = soup.findAll('th', background='/fond3.gif') if not pattern: ps = random.choice(s) await evt.respond(ps.text) else: if pattern == "morceau": ps = random.choice(s) await evt.respond(ps.text) else: await evt.respond("Nope !") loop.close()