From 7b8122a69f89462c23e7fe7f33d54e8f6579edce Mon Sep 17 00:00:00 2001 From: Guams Date: Fri, 7 Mar 2025 19:10:15 +0100 Subject: [PATCH] Basic poll creation --- enums.py | 4 +- main.py | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 155 insertions(+), 16 deletions(-) diff --git a/enums.py b/enums.py index 3a5b32d..0e073a5 100644 --- a/enums.py +++ b/enums.py @@ -15,7 +15,9 @@ class OpCode(int, Enum): HEARTBEAT_ACK = 11 REQUEST_SOUNDBOARD_SOUNDS = 31 + class EventTitle(str, Enum): + INTERACTION_CREATE = "INTERACTION_CREATE" MESSAGE_CREATE = "MESSAGE_CREATE" GUILD_CREATE = "GUILD_CREATE" - READY = "READY" \ No newline at end of file + READY = "READY" diff --git a/main.py b/main.py index c0cf867..f270c40 100644 --- a/main.py +++ b/main.py @@ -12,23 +12,101 @@ from enums import OpCode, EventTitle with open('configuration.json', 'r') as file: CONFIG = json.load(file) +APPLICATION_ID: int = CONFIG["APPLICATION_ID"] GATEWAY_URL: str = "wss://gateway.discord.gg/?v=10&encoding=json" API_URL: str = "https://discord.com/api/v10" TOKEN: str = CONFIG["TOKEN"] -async def send_message(channel_id: int, message: str): - body: dict= { - "content": message, - "tts": False, - "embeds": [{ - "title": "Hello, Embed!", - "description": "This is an embedded message." - }] +async def create_command(): + body: dict = { + "name": "poll", + "type": 1, + "description": "Des votes sur lesquels gamble !", + "options": [ + { + "name": "intitulee", + "description": "Intitulé du vote", + "type": 3, + "required": True + }, + { + "name": "vote1", + "description": "Première option de vote", + "type": 3, + "required": True + }, + { + "name": "vote2", + "description": "Deuxième option de vote", + "type": 3, + "required": False + }, + { + "name": "vote3", + "description": "Troisième option de vote", + "type": 3, + "required": False + }, + { + "name": "vote4", + "description": "Quatrième option de vote", + "type": 3, + "required": False + }, + { + "name": "vote5", + "description": "Cinquième option de vote", + "type": 3, + "required": False + } + ] + } + res: Response = requests.post(f"{API_URL}/applications/{APPLICATION_ID}/commands", json=body, + headers={"Authorization": f"Bot {TOKEN}"}) + print(res) + + +async def init_commands(): + res = requests.get(f"{API_URL}/applications/{APPLICATION_ID}/commands", headers={"Authorization": f"Bot {TOKEN}"}) + commands = json.loads(res.content) + for command in commands: + response = requests.delete(f"{API_URL}/applications/{APPLICATION_ID}/commands/{command['id']}", + headers={"Authorization": f"Bot {TOKEN}"}) + if response.status_code == 204: + print("Suppression des commandes: Supprimé avec succès !") + else: + print("Suppression des commandes: Une erreur est survenue") + await create_command() + + +async def create_poll(channel_id: int, label: str, vote_options: list[dict]): + body: dict = { + "content": "salut feikrf", + "components": [ + { + "type": 1, + "components": [ + { + "type": 4, + "custom_id": "textinput", + "label": "Montant de la somme pariée", + "min_length": 1, + "required": True, + "value": "50", + "style": 1 + } + ] + } + ] } - res: Response = requests.post(url=f"{API_URL}/channels/{channel_id}/messages", json=body, headers={"Authorization": f"Bot {TOKEN}"}) - print(res) + res: Response = requests.post(url=f"{API_URL}/channels/{channel_id}/messages", json=body, + headers={"Authorization": f"Bot {TOKEN}"}) + if res.status_code == 400: + print(res.json()) + else: + print(res) async def identify(websocket): @@ -41,23 +119,80 @@ async def identify(websocket): "browser": "gambling", "device": "gambling" }, - "intents": 65024, + "intents": 50364416, } } await websocket.send(json.dumps(payload)) +async def create_interaction_response(interaction_id: int, interaction_token: str): + body = { + "type": 4, + "data": { + "embeds": [ + { + "title": "Les paris sont ouvert !", + "description": "Vous pouvez parier vos points, ils ne passeront pas en dessous de 50." + } + ], + "poll": { + "question": + { + "text": "Question du poll" + }, + "answers": + [ + { + "answer_id": 1, + "poll_media": + { + "text": "vote1" + } + }, + { + "answer_id": 2, + "poll_media": + { + "text": "vote2" + } + }, + { + "answer_id": 3, + "poll_media": + { + "text": "vote3" + } + } + ], + "allow_multiselect": False, + "layout_type": 1 + } + } + } + res = requests.post(f"{API_URL}/interactions/{interaction_id}/{interaction_token}/callback", json=body, + headers={"Authorization": f"Bot {TOKEN}"}) + print(res) + + async def heartbeat(websocket, interval): while True: await asyncio.sleep(interval / 1000) await websocket.send(json.dumps({"op": OpCode.HEARTBEAT, "d": None})) + async def get_event(response: Any): match response["t"]: case EventTitle.MESSAGE_CREATE: print(f'{response["d"]["author"]["username"]}: {response["d"]["content"]}') + case EventTitle.INTERACTION_CREATE: + print(response) + await create_interaction_response(int(response["d"]["id"]), response["d"]["token"]) + # poll_data: list = response["d"]["data"]["options"] + # channel_id = int(response["d"]["channel_id"]) + # label = poll_data.pop(0) + # await create_poll(channel_id=channel_id, label=label, vote_options=poll_data) case _: - print("Unknown event") + print(response) async def connect(): @@ -71,16 +206,18 @@ async def connect(): while True: response = json.loads(await websocket.recv()) - #print("Received: ", response) match response["op"]: case OpCode.DISPATCH: await get_event(response) case _: - print("osef") + print(response) async def main(): gateway_connect = asyncio.create_task(connect()) + # await init_commands() + # await create_poll(840107060901052426, "f", []) await gateway_connect -asyncio.run(main()) \ No newline at end of file + +asyncio.run(main())