1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00

Use telegram_escape() and discord_escape() functions

This commit is contained in:
Steffo 2019-05-22 19:02:34 +02:00
parent 81163004bf
commit 0ad680a97e
4 changed files with 35 additions and 30 deletions

View file

@ -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:

View file

@ -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("<", "&lt;") \
.replace(">", "&gt;") \
.replace("[b]", "<b>") \
.replace("[/b]", "</b>") \
.replace("[i]", "<i>") \
.replace("[/i]", "</i>") \
.replace("[u]", "<b>") \
.replace("[/u]", "</b>") \
.replace("[c]", "<code>") \
.replace("[/c]", "</code>") \
.replace("[p]", "<pre>") \
.replace("[/p]", "</pre>")
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:

View file

@ -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

View file

@ -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("<", "&lt;") \
.replace(">", "&gt;") \
.replace("[b]", "<b>") \
.replace("[/b]", "</b>") \
.replace("[i]", "<i>") \
.replace("[/i]", "</i>") \
.replace("[u]", "<b>") \
.replace("[/u]", "</b>") \
.replace("[c]", "<code>") \
.replace("[/c]", "</code>") \
.replace("[p]", "<pre>") \
.replace("[/p]", "</pre>")