info on error + thumbnailify improvement

This commit is contained in:
Nexus 2024-09-08 01:24:03 +01:00
parent d58ce364f9
commit a814d9e987
3 changed files with 32 additions and 25 deletions

View file

@ -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"]))

View file

@ -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:

View file

@ -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")