fix messages > 2000 chars, fix fetching channel names in messages, fix newlines

This commit is contained in:
git-bruh 2021-05-17 21:08:22 +05:30
parent 4713a00016
commit a3ee85d955
No known key found for this signature in database
GPG key ID: E1475C50075ADCE6
2 changed files with 14 additions and 12 deletions

View file

@ -3,6 +3,7 @@ from misc import dict_cls
CDN_URL = "https://cdn.discordapp.com" CDN_URL = "https://cdn.discordapp.com"
ID_LEN = 18 ID_LEN = 18
MESSAGE_LIMIT = 2000
def bitmask(bit: int) -> int: def bitmask(bit: int) -> int:

View file

@ -207,7 +207,9 @@ class MatrixClient(AppService):
reference: discord.MessageReference = None, reference: discord.MessageReference = None,
) -> dict: ) -> dict:
content = { content = {
"body": message, # Replace single newlines with double newlines so that clients
# render them properly.
"body": re.sub("\\b\n\\b", "\n\n", message),
"format": "org.matrix.custom.html", "format": "org.matrix.custom.html",
"msgtype": "m.text", "msgtype": "m.text",
"formatted_body": self.get_fmt(message, emotes), "formatted_body": self.get_fmt(message, emotes),
@ -237,12 +239,12 @@ class MatrixClient(AppService):
content = { content = {
**content, **content,
"body": ( "body": (
f"> <{event.sender}> {event.body}\n{content['body']}" f"> <{event.sender}> {event.body}\n\n{content['body']}"
), ),
"m.relates_to": {"m.in_reply_to": {"event_id": event.id}}, "m.relates_to": {"m.in_reply_to": {"event_id": event.id}},
"formatted_body": f"""<mx-reply><blockquote>\ "formatted_body": f"""<mx-reply><blockquote>\
<a href='https://matrix.to/#/{event.room_id}/{event.id}'>\ <a href="https://matrix.to/#/{event.room_id}/{event.id}">\
In reply to</a><a href='https://matrix.to/#/{event.sender}'>\ In reply to</a><a href="https://matrix.to/#/{event.sender}">\
{event.sender}</a><br>{event.formatted_body}</blockquote></mx-reply>\ {event.sender}</a><br>{event.formatted_body}</blockquote></mx-reply>\
{content["formatted_body"]}""", {content["formatted_body"]}""",
} }
@ -311,8 +313,6 @@ height=\"32\" src=\"{emote_}\" data-mx-emoticon />""",
def process_message(self, event: matrix.Event) -> str: def process_message(self, event: matrix.Event) -> str:
message = event.new_body if event.new_body else event.body message = event.new_body if event.new_body else event.body
message = message[:2000] # Discord limit.
id_regex = f"[0-9]{{{discord.ID_LEN}}}" id_regex = f"[0-9]{{{discord.ID_LEN}}}"
emotes = re.findall(r":(\w*):", message) emotes = re.findall(r":(\w*):", message)
@ -341,7 +341,8 @@ height=\"32\" src=\"{emote_}\" data-mx-emoticon />""",
replace, f"<@{match.group()}>" replace, f"<@{match.group()}>"
) )
return message # We trim the message later as emotes take up extra characters too.
return message[: discord.MESSAGE_LIMIT]
def upload_emote(self, emote_name: str, emote_id: str) -> None: def upload_emote(self, emote_name: str, emote_id: str) -> None:
# There won't be a race condition here, since only a unique # There won't be a race condition here, since only a unique
@ -623,12 +624,12 @@ class DiscordClient(Gateway):
# `except_deleted` for invalid channels. # `except_deleted` for invalid channels.
# TODO can this block for too long ? # TODO can this block for too long ?
for channel in re.findall(r"<#([0-9]{{{discord.ID_LEN}}})>", content): for channel in re.findall(f"<#([0-9]{{{discord.ID_LEN}}})>", content):
channel_ = except_deleted(self.get_channel)(channel) discord_channel = except_deleted(self.get_channel)(channel)
content = content.replace( name = (
f"<#{channel}>", discord_channel.name if discord_channel else "deleted-channel"
f"#{channel_.name}" if channel_ else "deleted-channel",
) )
content = content.replace(f"<#{channel}>", f"#{name}")
# { "emote_name": "emote_id" } # { "emote_name": "emote_id" }
for emote in re.findall(regex, content): for emote in re.findall(regex, content):