info on error + thumbnailify improvement
This commit is contained in:
parent
d58ce364f9
commit
a814d9e987
3 changed files with 32 additions and 25 deletions
23
app/main.py
23
app/main.py
|
@ -1,10 +1,11 @@
|
||||||
import sys
|
# import sys
|
||||||
|
import asyncio
|
||||||
|
|
||||||
import niobot
|
import niobot
|
||||||
import tomllib
|
import tomllib
|
||||||
import platform
|
import platform
|
||||||
import logging
|
import logging
|
||||||
import tortoise
|
# import tortoise
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
@ -25,11 +26,11 @@ class TortoiseIntegratedBot(niobot.NioBot):
|
||||||
cfg: dict
|
cfg: dict
|
||||||
|
|
||||||
async def start(self, **kwargs):
|
async def start(self, **kwargs):
|
||||||
url = config["database"].get("uri")
|
# url = config["database"].get("uri")
|
||||||
if not url:
|
# if not url:
|
||||||
log.critical("No database URI specified in config.toml - using ephemeral sqlite.")
|
# log.critical("No database URI specified in config.toml - using ephemeral sqlite.")
|
||||||
url = "sqlite://:memory:"
|
# url = "sqlite://:memory:"
|
||||||
sys.path.extend(("..", "."))
|
# sys.path.extend(("..", "."))
|
||||||
# await tortoise.Tortoise.init(db_url=url, modules={"models": ["app.models"]})
|
# await tortoise.Tortoise.init(db_url=url, modules={"models": ["app.models"]})
|
||||||
# await tortoise.Tortoise.generate_schemas()
|
# await tortoise.Tortoise.generate_schemas()
|
||||||
|
|
||||||
|
@ -72,6 +73,11 @@ async def on_command(ctx: niobot.Context):
|
||||||
@bot.on_event("command_error")
|
@bot.on_event("command_error")
|
||||||
async def on_command_error(ctx: niobot.Context, exc: Exception):
|
async def on_command_error(ctx: niobot.Context, exc: Exception):
|
||||||
log.error("Command %s failed.", ctx.command, exc_info=exc)
|
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")
|
@bot.on_event("command_complete")
|
||||||
|
@ -80,4 +86,5 @@ async def on_command_complete(ctx: niobot.Context, _):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
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"]))
|
||||||
|
|
|
@ -5,6 +5,7 @@ This entire module is locked to NioBot.owner.
|
||||||
import shlex
|
import shlex
|
||||||
import tempfile
|
import tempfile
|
||||||
import textwrap
|
import textwrap
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import niobot
|
import niobot
|
||||||
|
@ -157,8 +158,23 @@ class EvalModule(niobot.Module):
|
||||||
|
|
||||||
@niobot.command("thumbnail", hidden=True)
|
@niobot.command("thumbnail", hidden=True)
|
||||||
@niobot.is_owner()
|
@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"""
|
"""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 aiohttp.ClientSession(headers={"User-Agent": niobot.__user_agent__}) as client:
|
||||||
async with client.get(url) as response:
|
async with client.get(url) as response:
|
||||||
if response.status != 200:
|
if response.status != 200:
|
||||||
|
|
|
@ -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")
|
|
Loading…
Reference in a new issue