diff --git a/royalnet/bots/discord.py b/royalnet/bots/discord.py
index 0bc2b2d2..62a7df92 100644
--- a/royalnet/bots/discord.py
+++ b/royalnet/bots/discord.py
@@ -4,7 +4,7 @@ import typing
import logging as _logging
from .generic import GenericBot
from ..commands import NullCommand
-from ..utils import asyncify, Call, Command
+from ..utils import asyncify, Call, Command, discord_escape
from ..error import UnregisteredError, NoneFoundError, TooManyFoundError, InvalidConfigError, RoyalnetResponseError
from ..network import RoyalnetConfig, Request, ResponseSuccess, ResponseError
from ..database import DatabaseConfig
@@ -47,20 +47,7 @@ class DiscordBot(GenericBot):
async def reply(call, text: str):
# TODO: don't escape characters inside [c][/c] blocks
- escaped_text = text.replace("*", "\\*") \
- .replace("_", "\\_") \
- .replace("`", "\\`") \
- .replace("[b]", "**") \
- .replace("[/b]", "**") \
- .replace("[i]", "_") \
- .replace("[/i]", "_") \
- .replace("[u]", "__") \
- .replace("[/u]", "__") \
- .replace("[c]", "`") \
- .replace("[/c]", "`") \
- .replace("[p]", "```") \
- .replace("[/p]", "```")
- await call.channel.send(escaped_text)
+ await call.channel.send(discord_escape(text))
async def net_request(call, request: Request, destination: str) -> dict:
if self.network is None:
diff --git a/royalnet/bots/telegram.py b/royalnet/bots/telegram.py
index 047f3055..874576c0 100644
--- a/royalnet/bots/telegram.py
+++ b/royalnet/bots/telegram.py
@@ -5,9 +5,9 @@ import typing
import logging as _logging
from .generic import GenericBot
from ..commands import NullCommand
-from ..utils import asyncify, Call, Command
+from ..utils import asyncify, Call, Command, telegram_escape
from ..error import UnregisteredError, InvalidConfigError, RoyalnetResponseError
-from ..network import RoyalnetConfig, Request, Response, ResponseSuccess, ResponseError
+from ..network import RoyalnetConfig, Request, ResponseSuccess, ResponseError
from ..database import DatabaseConfig
loop = asyncio.get_event_loop()
@@ -41,19 +41,7 @@ class TelegramBot(GenericBot):
alchemy = self.alchemy
async def reply(call, text: str):
- escaped_text = text.replace("<", "<") \
- .replace(">", ">") \
- .replace("[b]", "") \
- .replace("[/b]", "") \
- .replace("[i]", "") \
- .replace("[/i]", "") \
- .replace("[u]", "") \
- .replace("[/u]", "") \
- .replace("[c]", "") \
- .replace("[/c]", "
") \
- .replace("[p]", "
") \ - .replace("[/p]", "") - await asyncify(call.channel.send_message, escaped_text, parse_mode="HTML") + await asyncify(call.channel.send_message, telegram_escape(text), parse_mode="HTML") async def net_request(call, request: Request, destination: str) -> dict: if self.network is None: diff --git a/royalnet/utils/__init__.py b/royalnet/utils/__init__.py index 01bebb7d..2d42bbe3 100644 --- a/royalnet/utils/__init__.py +++ b/royalnet/utils/__init__.py @@ -1,6 +1,7 @@ """Miscellaneous useful functions and classes.""" from .asyncify import asyncify +from .escaping import telegram_escape, discord_escape from .call import Call from .command import Command from .commandargs import CommandArgs diff --git a/royalnet/utils/escaping.py b/royalnet/utils/escaping.py new file mode 100644 index 00000000..dc59bfde --- /dev/null +++ b/royalnet/utils/escaping.py @@ -0,0 +1,29 @@ +def discord_escape(string: str) -> str: + return string.replace("*", "\\*") \ + .replace("_", "\\_") \ + .replace("`", "\\`") \ + .replace("[b]", "**") \ + .replace("[/b]", "**") \ + .replace("[i]", "_") \ + .replace("[/i]", "_") \ + .replace("[u]", "__") \ + .replace("[/u]", "__") \ + .replace("[c]", "`") \ + .replace("[/c]", "`") \ + .replace("[p]", "```") \ + .replace("[/p]", "```") + + +def telegram_escape(string: str) -> str: + return string.replace("<", "<") \ + .replace(">", ">") \ + .replace("[b]", "") \ + .replace("[/b]", "") \ + .replace("[i]", "") \ + .replace("[/i]", "") \ + .replace("[u]", "") \ + .replace("[/u]", "") \ + .replace("[c]", "
") \
+ .replace("[/c]", "
") \
+ .replace("[p]", "") \ + .replace("[/p]", "")