From a814d9e9870373b71435e0ff98caf629dddf58c5 Mon Sep 17 00:00:00 2001 From: nexy7574 Date: Sun, 8 Sep 2024 01:24:03 +0100 Subject: [PATCH] info on error + thumbnailify improvement --- app/main.py | 23 +++++++++++++++-------- app/modules/evaluation.py | 18 +++++++++++++++++- app/modules/rust.py | 16 ---------------- 3 files changed, 32 insertions(+), 25 deletions(-) delete mode 100644 app/modules/rust.py diff --git a/app/main.py b/app/main.py index 3124545..058a684 100644 --- a/app/main.py +++ b/app/main.py @@ -1,10 +1,11 @@ -import sys +# import sys +import asyncio import niobot import tomllib import platform import logging -import tortoise +# import tortoise from pathlib import Path log = logging.getLogger(__name__) @@ -25,11 +26,11 @@ class TortoiseIntegratedBot(niobot.NioBot): cfg: dict async def start(self, **kwargs): - url = config["database"].get("uri") - if not url: - log.critical("No database URI specified in config.toml - using ephemeral sqlite.") - url = "sqlite://:memory:" - sys.path.extend(("..", ".")) + # url = config["database"].get("uri") + # if not url: + # log.critical("No database URI specified in config.toml - using ephemeral sqlite.") + # url = "sqlite://:memory:" + # sys.path.extend(("..", ".")) # await tortoise.Tortoise.init(db_url=url, modules={"models": ["app.models"]}) # await tortoise.Tortoise.generate_schemas() @@ -72,6 +73,11 @@ async def on_command(ctx: niobot.Context): @bot.on_event("command_error") async def on_command_error(ctx: niobot.Context, exc: Exception): log.error("Command %s failed.", ctx.command, exc_info=exc) + text = "\N{warning sign} Command failed: `%s`" % exc + text = text.replace(bot.access_token, "REDACTED") + await ctx.respond( + text + ) @bot.on_event("command_complete") @@ -80,4 +86,5 @@ async def on_command_complete(ctx: niobot.Context, _): if __name__ == "__main__": - tortoise.run_async(bot.start(access_token=bot.cfg["bot"]["access_token"])) + asyncio.run(bot.start(access_token=bot.cfg["bot"]["access_token"])) + # tortoise.run_async(bot.start(access_token=bot.cfg["bot"]["access_token"])) diff --git a/app/modules/evaluation.py b/app/modules/evaluation.py index 6340ef5..1336ac6 100644 --- a/app/modules/evaluation.py +++ b/app/modules/evaluation.py @@ -5,6 +5,7 @@ This entire module is locked to NioBot.owner. import shlex import tempfile import textwrap +from urllib.parse import urlparse import aiohttp import niobot @@ -157,8 +158,23 @@ class EvalModule(niobot.Module): @niobot.command("thumbnail", hidden=True) @niobot.is_owner() - async def thumbnail(self, ctx: niobot.Context, url: str): + async def thumbnail(self, ctx: niobot.Context, url: str, width: int = 320, height: int = 240): """Get the thumbnail for a URL""" + if url.startswith("mxc://"): + parsed = urlparse(url) + media_id = parsed.path.lstrip("/") + thumbnail = await ctx.bot.thumbnail( + parsed.netloc, + media_id, + width=width, + height=height + ) + if not isinstance(thumbnail, niobot.ThumbnailResponse): + return await ctx.respond("Failed to thumbnail: %r" % thumbnail) + fn = thumbnail.body.filename or "thumbnail.webp" + file = niobot.ImageAttachment.from_file(thumbnail.body, fn) + return await ctx.respond("Downloaded from %r" % url, file=file) + async with aiohttp.ClientSession(headers={"User-Agent": niobot.__user_agent__}) as client: async with client.get(url) as response: if response.status != 200: diff --git a/app/modules/rust.py b/app/modules/rust.py deleted file mode 100644 index f413571..0000000 --- a/app/modules/rust.py +++ /dev/null @@ -1,16 +0,0 @@ -import niobot -import textwrap - -with open("assets/rust.txt") as copypasta_fd: - COPYPASTA = textwrap.dedent(copypasta_fd.read()) - - -class RustModule(niobot.Module): - @niobot.event("message") - async def on_message(self, room: niobot.MatrixRoom, event: niobot.RoomMessage): - if not isinstance(event, niobot.RoomMessageText): - return - elif room.display_name != "rust": - return - if "rust" in event.body.lower(): - await self.bot.send_message(room, COPYPASTA, message_type="m.notice")