From 14ceca64d9fa50dfcaa0dfe79bd3159a0850e6f3 Mon Sep 17 00:00:00 2001 From: Jason <81298350+Deutscher775@users.noreply.github.com> Date: Mon, 18 Nov 2024 19:29:16 +0100 Subject: [PATCH] fixed revolt sending mechanism, added revolt beta invite again --- src/api.py | 2 + src/astroidapi/sending_handler.py | 70 ++++++++++++++----------------- 2 files changed, 34 insertions(+), 38 deletions(-) diff --git a/src/api.py b/src/api.py index 9764a6d..455703f 100644 --- a/src/api.py +++ b/src/api.py @@ -147,6 +147,8 @@ def invite(platform: str, token: Annotated[str, fastapi.Query(max_length=85)] = return fastapi.responses.RedirectResponse(status_code=301, url="https://discord.com/oauth2/authorize?client_id=1230579871059804280&permissions=138046467089&scope=applications.commands+bot") elif platform == "guilded-beta" and token in beta_users.TOKENS: return fastapi.responses.RedirectResponse(status_code=301, url="https://www.guilded.gg/b/3f887186-82d5-4fe1-abbd-045748b125b3") + elif platform == "revolt-beta" and token in beta_users.TOKENS: + return fastapi.responses.RedirectResponse(status_code=301, url="https://app.revolt.chat/bot/01HZH72H3PZKTEG1ADQ5P2XBF5") else: return fastapi.responses.JSONResponse(status_code=400, content={"message": "Invalid platform."}) diff --git a/src/astroidapi/sending_handler.py b/src/astroidapi/sending_handler.py index 60972ca..962e275 100644 --- a/src/astroidapi/sending_handler.py +++ b/src/astroidapi/sending_handler.py @@ -51,9 +51,10 @@ async def distribute(cls, endpoint, updated_json): @classmethod async def send_from_discord(cls, updated_json, endpoint, attachments: list = None): try: - asyncio.create_task(cls.send_to_revolt(updated_json, endpoint, attachments)) asyncio.create_task(cls.send_to_guilded(updated_json, endpoint, attachments)) asyncio.create_task(cls.send_to_nerimity(updated_json, endpoint, attachments)) + if updated_json["config"]["isbeta"] is True: + asyncio.create_task(cls.send_to_revolt(updated_json, endpoint, attachments)) except Exception as e: raise errors.SendingError.SendFromDiscordError(e) @@ -257,41 +258,34 @@ async def send_to_nerimity(cls, updated_json, endpoint, attachments: list = None @classmethod async def send_to_revolt(cls, updated_json, endpoint, attachments: list = None): - return True - #if updated_json["meta"]["read"]["revolt"] is False: - # if updated_json["meta"]["sender-channel"] in updated_json["config"]["channels"]["discord"]: - # channel_id = updated_json["config"]["channels"]["revolt"][updated_json["config"]["channels"]["discord"].index(updated_json["meta"]["sender-channel"])] - # elif updated_json["meta"]["sender-channel"] in updated_json["config"]["channels"]["guilded"]: - # channel_id = updated_json["config"]["channels"]["revolt"][updated_json["config"]["channels"]["guilded"].index(updated_json["meta"]["sender-channel"])] - # elif updated_json["meta"]["sender-channel"] in updated_json["config"]["channels"]["nerimity"]: - # channel_id = updated_json["config"]["channels"]["revolt"][updated_json["config"]["channels"]["nerimity"].index(updated_json["meta"]["sender-channel"])] - # else: - # raise errors.SendingError.ChannelNotFound(f"The channel {updated_json["meta"]["sender-channel"]} ({updated_json["meta"]["sender"]}) does not seem to be a registered channel on other platforms.") - # - # headers = { - # "Authorization" : f"Bot {config.REVOLT_TOKEN}" - # } - # url = f"https://api.revolt.chat/channels/{channel_id}/messages" - # payload = { - # "attachments": [None], - # "masquerade": { - # "avatar": updated_json["meta"]["message"]["author"]["avatar"], - # "name": updated_json["meta"]["message"]["author"]["name"] - # - # }, - # "replies": [ - # { - # "id": "", - # "mention": True - # } - # ], - # "embeds": [{}] - # } - # async with aiohttp.ClientSession() as session: - # async with session.post(url, headers=headers, json=payload) as r: - # await session.post(f"https://astroid.deutscher775.de/read/{endpoint}?token={config.MASTER_TOKEN}&read_revolt=true") - # await session.close() - # return True - #else: - # return False + if updated_json["meta"]["read"]["revolt"] is False: + if updated_json["meta"]["sender-channel"] in updated_json["config"]["channels"]["discord"]: + channel_id = updated_json["config"]["channels"]["revolt"][updated_json["config"]["channels"]["discord"].index(updated_json["meta"]["sender-channel"])] + elif updated_json["meta"]["sender-channel"] in updated_json["config"]["channels"]["guilded"]: + channel_id = updated_json["config"]["channels"]["revolt"][updated_json["config"]["channels"]["guilded"].index(updated_json["meta"]["sender-channel"])] + elif updated_json["meta"]["sender-channel"] in updated_json["config"]["channels"]["nerimity"]: + channel_id = updated_json["config"]["channels"]["revolt"][updated_json["config"]["channels"]["nerimity"].index(updated_json["meta"]["sender-channel"])] + else: + raise errors.SendingError.ChannelNotFound(f"The channel {updated_json["meta"]["sender-channel"]} ({updated_json["meta"]["sender"]}) does not seem to be a registered channel on other platforms.") + + headers = { + "X-Bot-Token": f"{config.REVOLT_TOKEN}" + } + url = f"https://api.revolt.chat/channels/{channel_id}/messages" + payload = { + "masquerade": { + "avatar": updated_json["meta"]["message"]["author"]["avatar"], + "name": updated_json["meta"]["message"]["author"]["name"] + }, + "content": updated_json["meta"]["message"]["content"] + } + + async with aiohttp.ClientSession() as session: + async with session.post(url, headers=headers, json=payload) as r: + print(f"Sent to revolt. Response: {r.status}, {r.reason} {await r.text()}") + asyncio.create_task(read_handler.ReadHandler.mark_read(endpoint, "revolt")) + await session.close() + return True + else: + return False \ No newline at end of file