1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Escape RoyalCode in Matrix messages

This commit is contained in:
Steffo 2019-12-27 01:34:26 +01:00
parent 2fa90fc4e0
commit 1633ff006c
4 changed files with 21 additions and 3 deletions

View file

@ -1,5 +1,7 @@
from .matrixserf import MatrixSerf from .matrixserf import MatrixSerf
from .escape import escape
__all__ = [ __all__ = [
"MatrixSerf", "MatrixSerf",
"escape",
] ]

View file

@ -0,0 +1,15 @@
def escape(string: str) -> str:
"""Escape a string to be sent through Matrix, and format it using RoyalCode.
Underlines are currently unsupported.
Warning:
Currently escapes everything, even items in code blocks."""
return string.replace("[b]", "**") \
.replace("[/b]", "**") \
.replace("[i]", "_") \
.replace("[/i]", "_") \
.replace("[c]", "`") \
.replace("[/c]", "`") \
.replace("[p]", "```") \
.replace("[/p]", "```")

View file

@ -6,6 +6,7 @@ import royalnet.backpack as rb
import royalnet.commands as rc import royalnet.commands as rc
import royalnet.utils as ru import royalnet.utils as ru
from ..serf import Serf from ..serf import Serf
from .escape import escape
try: try:
@ -77,7 +78,7 @@ class MatrixSerf(Serf):
async def reply(data, text: str): async def reply(data, text: str):
await self.client.room_send(room_id=data.room.room_id, message_type="m.room.message", content={ await self.client.room_send(room_id=data.room.room_id, message_type="m.room.message", content={
"msgtype": "m.text", "msgtype": "m.text",
"body": text "body": escape(text)
}) })
async def get_author(data, error_if_none=False): async def get_author(data, error_if_none=False):

View file

@ -1 +1 @@
semantic = "5.2.1" semantic = "5.2.2"