mirror of
https://git.freetards.xyz/array.in.a.matrix/TexLiLy.git
synced 2025-04-02 13:21:42 -04:00
[ADD] Don't spam rooms with errors.
This commit is contained in:
parent
24182f3739
commit
eb327749e8
3 changed files with 25 additions and 13 deletions
|
@ -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",
|
||||
|
|
|
@ -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
|
||||
|
|
22
utils.py
22
utils.py
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue