From 657371006657e59e3d3fe9b555907b45120cb3a5 Mon Sep 17 00:00:00 2001 From: Guams Date: Fri, 7 Mar 2025 23:11:16 +0100 Subject: [PATCH] Dynamic poll creation --- enums.py | 11 +++ main.py | 225 +++++++++++++++++++++++++++++------------------------- models.py | 8 -- 3 files changed, 133 insertions(+), 111 deletions(-) delete mode 100644 models.py diff --git a/enums.py b/enums.py index 0e073a5..ffe5edb 100644 --- a/enums.py +++ b/enums.py @@ -16,6 +16,17 @@ class OpCode(int, Enum): REQUEST_SOUNDBOARD_SOUNDS = 31 +class MessageComponentType(str, Enum): + ACTION_ROW = 1 + BUTTON = 2 + STRING_SELECT = 3 + TEXT_INPUT = 4 + USER_SELECT = 5 + ROLE_SELECT = 6 + MENTIONNABLE_SELECT = 7 + CHANNEL_SELECT = 8 + MODAL = 9 + class EventTitle(str, Enum): INTERACTION_CREATE = "INTERACTION_CREATE" MESSAGE_CREATE = "MESSAGE_CREATE" diff --git a/main.py b/main.py index f270c40..90428a6 100644 --- a/main.py +++ b/main.py @@ -7,7 +7,7 @@ import json from websockets import Response -from enums import OpCode, EventTitle +from enums import OpCode, EventTitle, MessageComponentType with open('configuration.json', 'r') as file: CONFIG = json.load(file) @@ -23,44 +23,6 @@ async def create_command(): "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}"}) @@ -80,35 +42,52 @@ async def init_commands(): 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 - } - ] +async def create_poll(interaction_id: int, interaction_token: str, components: list[dict]): + question = next( + (comp["components"][0]["value"] for comp in components if comp["components"][0]["custom_id"] == "label"), + "Question du poll" + ) + + vote_options = [] + for index, comp in enumerate(components): + if "vote" in comp["components"][0]["custom_id"] and comp["components"][0]["value"] != "": + vote_text = comp["components"][0]["value"] + vote_options.append({ + "answer_id": len(vote_options) + 1, + "poll_media": {"text": vote_text} + }) + + body = { + "type": 4, + "data": { + "embeds": [ + { + "title": "Les paris sont ouverts !", + "description": "Vous pouvez parier vos points, ils ne passeront pas en dessous de 50." + } + ], + "poll": { + "question": {"text": question}, + "answers": vote_options, + "allow_multiselect": False, + "layout_type": 1 } - ] + } } - res: Response = requests.post(url=f"{API_URL}/channels/{channel_id}/messages", json=body, - headers={"Authorization": f"Bot {TOKEN}"}) + res = requests.post( + f"{API_URL}/interactions/{interaction_id}/{interaction_token}/callback", + json=body, + headers={"Authorization": f"Bot {TOKEN}"} + ) + if res.status_code == 400: print(res.json()) else: print(res) + async def identify(websocket): payload: dict = { "op": OpCode.IDENTIFY, @@ -125,53 +104,93 @@ async def identify(websocket): await websocket.send(json.dumps(payload)) -async def create_interaction_response(interaction_id: int, interaction_token: str): +async def create_poll_form(interaction_id: int, interaction_token: str): body = { - "type": 4, + "type": 9, "data": { - "embeds": [ + "title": "Créer un vote", + "custom_id": "poll_id_create", + "components": [ { - "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": - [ + "type": 1, + "components": [ { - "answer_id": 1, - "poll_media": - { - "text": "vote1" - } - }, - { - "answer_id": 2, - "poll_media": - { - "text": "vote2" - } - }, - { - "answer_id": 3, - "poll_media": - { - "text": "vote3" - } + "type": 4, + "custom_id": "label", + "label": "Question du vote", + "placeholder": "Shy aime-t-il les robots?", + "style": 1, + "min_length": 1, + "required": True } - ], - "allow_multiselect": False, - "layout_type": 1 - } + ] + }, + { + "type": 1, + "components": [ + { + "type": 4, + "custom_id": "vote1", + "label": "Première option", + "placeholder": "Oui", + "style": 1, + "min_length": 1, + "required": True + } + ] + }, + { + "type": 1, + "components": [ + { + "type": 4, + "custom_id": "vote2", + "label": "Deuxième option", + "placeholder": "Non", + "style": 1, + "min_length": 1, + "required": False + } + ] + }, + { + "type": 1, + "components": [ + { + "type": 4, + "custom_id": "vote3", + "label": "Troisième option", + "placeholder": "Peut-être", + "style": 1, + "min_length": 1, + "required": False + } + ] + }, + { + "type": 1, + "components": [ + { + "type": 4, + "custom_id": "vote4", + "label": "Quatrième option", + "placeholder": "Pas du tout", + "style": 1, + "min_length": 1, + "required": False + } + ] + } + ] } } + res = requests.post(f"{API_URL}/interactions/{interaction_id}/{interaction_token}/callback", json=body, headers={"Authorization": f"Bot {TOKEN}"}) - print(res) + if res.status_code == 400: + print(res.json()) + else: + print(res) async def heartbeat(websocket, interval): @@ -185,12 +204,12 @@ async def get_event(response: Any): 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) + print(response["d"]["data"].keys()) + if "custom_id" in response["d"]["data"].keys(): + if response["d"]["data"]["custom_id"] == "poll_id_create": + await create_poll(int(response["d"]["id"]), response["d"]["token"], response["d"]["data"]["components"]) + + await create_poll_form(int(response["d"]["id"]), response["d"]["token"]) case _: print(response) diff --git a/models.py b/models.py deleted file mode 100644 index fd20423..0000000 --- a/models.py +++ /dev/null @@ -1,8 +0,0 @@ - -class User(): - def __init__(self, id: str, username: str, discriminator: str, global_name: str, avatar= None): - self.id = id - self.username = username - self.discriminator = discriminator - self.global_name = global_name - self.avatar = avatar \ No newline at end of file