[ADD] Don't spam rooms with errors.

This commit is contained in:
LoaD Accumulator 2023-06-15 17:11:10 +02:00
parent 24182f3739
commit eb327749e8
No known key found for this signature in database
GPG key ID: 6898757653ABE3E6
3 changed files with 25 additions and 13 deletions

View file

@ -59,17 +59,12 @@ async def msg_cb(room: MatrixRoom, event: RoomMessageText) -> None:
await utils.send_png(room.room_id, filename)
for tex in re.findall(latex_regex, event.body, re.M):
for tex in re.findall(latex_regex, event.formatted_body, re.M):
try:
filename = latex.render(event.sender, tex)
await utils.send_png(room.room_id, filename)
except FileNotFoundError as e:
content = {
"msgtype": "m.text",
"body": f"Couldn't parse LaTeX correctly.\n```{e.args[0]}\n```",
"formatted_body": f"Couldn't parse LaTeX correctly.<br><code><pre>{e.args[0]}</pre></code>",
}
await client.room_send(room.room_id, message_type="m.room.message", content=content)
await utils.send_plain(room.room_id, e.args[0])
except OSError:
content = {
"msgtype": "m.text",

View file

@ -43,12 +43,7 @@ async def route_lshow(router: Router, client: Client, room: str, event: RoomMess
file = lilypond.render("", args[0], version=version)
await utils.send_png(room, file)
except FileNotFoundError as e:
content = {
"msgtype": "m.text",
"body": f"Couldn't parse LilyPond correctly.\n```{e.args[0]}\n```",
"formatted_body": f"Couldn't parse LilyPond correctly.<br><code><pre>{e.args[0]}</pre></code>",
}
await client.room_send(room, message_type="m.room.message", content=content)
await utils.send_plain(room, e.args[0])
except Exception:
# ???
pass

View file

@ -64,5 +64,27 @@ async def send_png(room: str, filename: str) -> None:
"url": resp.content_uri,
}
file.close()
async def send_plain(room: str, cnt: str) -> None:
import imagesize
import tempfile
import io
b = bytes(cnt, 'utf8')
size = len(b)
resp, keys = await client.upload(io.BytesIO(b), content_type="text/plain")
if not isinstance(resp, UploadResponse):
raise OSError("Couldn't upload file.")
content = {
"body": "error.log",
"info": {
"size": size,
"mimetype": "text/plain"
},
"msgtype": "m.file",
"url": resp.content_uri,
}
await client.room_send(room, message_type="m.room.message", content=content)