Add compatibility for the new bridge system

This commit is contained in:
Nexus 2023-11-06 22:40:35 +00:00
parent 0eaff595fe
commit 46ee34332d
Signed by: nex
GPG key ID: 0FA334385D0B689F
3 changed files with 50 additions and 23 deletions

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="dataSourceStorageLocal" created-in="PY-232.10072.31">
<component name="dataSourceStorageLocal" created-in="PY-232.10203.26">
<data-source name="main" uuid="28efee07-d306-4126-bf69-01008b4887e2">
<database-info product="SQLite" version="3.39.2" jdbc-version="2.1" driver-name="SQLite JDBC" driver-version="3.39.2.0" dbms="SQLITE" exact-version="3.39.2" exact-driver-version="3.39">
<identifier-quote-string>&quot;</identifier-quote-string>

View file

@ -10,6 +10,7 @@ import re
import subprocess
import textwrap
import traceback
import pydantic
import warnings
from datetime import datetime, timedelta, timezone
from pathlib import Path
@ -47,6 +48,26 @@ LTR = "\N{black rightwards arrow}\U0000fe0f"
RTL = "\N{leftwards black arrow}\U0000fe0f"
class MessagePayload(pydantic.BaseModel):
class MessageAttachmentPayload(pydantic.BaseModel):
url: str
proxy_url: str
filename: str
size: int
width: int
height: int
content_type: str
message_id: int
author: str
avatar: str
content: str
clean_content: str
at: float
attachments: list[MessageAttachmentPayload] = []
reply_to: Optional["MessagePayload"] = None
async def _dc(client: discord.VoiceClient | None):
if client is None:
return
@ -321,27 +342,33 @@ class Events(commands.Cog):
return
if message.channel.name == "femboy-hole":
payload = {
"author": message.author.name,
"avatar": message.author.display_avatar.with_format("png").with_size(512).url,
"content": message.content,
"at": message.created_at.timestamp(),
"attachments": [
{
"url": a.url,
"filename": a.filename,
"size": a.size,
"width": a.width,
"height": a.height,
"content_type": a.content_type,
}
for a in message.attachments
],
}
if message.author.discriminator != "0":
payload["author"] += "#%s" % message.author.discriminator
if message.author != self.bot.user and (payload["content"] or payload["attachments"]):
await self.bot.bridge_queue.put(payload)
def generate_payload(_message: discord.Message) -> MessagePayload:
_payload = MessagePayload(
message_id=_message.id,
author=_message.author.name,
avatar=_message.author.display_avatar.with_static_format("webp").with_size(512).url,
content=_message.content or '',
clean_content=str(_message.clean_content or ''),
at=_message.created_at.timestamp()
)
for attachment in _message.attachments:
_payload.attachments.append(
MessagePayload.MessageAttachmentPayload(
url=attachment.url,
proxy_url=attachment.proxy_url,
size=attachment.size,
width=attachment.width,
height=attachment.height,
content_type=attachment.content_type
)
)
if _message.reference is not None and _message.reference.cached_message:
_payload.reply_to = generate_payload(_message)
return _payload
payload = generate_payload(message)
if message.author != self.bot.user and (payload.content or payload.attachments):
await self.bot.bridge_queue.put(payload.model_dump())
if message.channel.name == "pinboard" and not message.content.startswith(("#", "//", ";", "h!")):
if message.type == discord.MessageType.pins_add:

View file

@ -254,7 +254,7 @@ async def bridge(req: Request):
if not channel:
raise HTTPException(status_code=404, detail="Channel does not exist.")
if len(body["message"]) > 6000:
if len(body["message"]) > 4000:
raise HTTPException(status_code=400, detail="Message too long.")
paginator = Paginator(prefix="", suffix="", max_size=1990)
for line in body["message"].splitlines():