Dynamic poll creation

This commit is contained in:
Guams 2025-03-07 23:11:16 +01:00
parent 7b8122a69f
commit 6573710066
3 changed files with 133 additions and 111 deletions

View File

@ -16,6 +16,17 @@ class OpCode(int, Enum):
REQUEST_SOUNDBOARD_SOUNDS = 31 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): class EventTitle(str, Enum):
INTERACTION_CREATE = "INTERACTION_CREATE" INTERACTION_CREATE = "INTERACTION_CREATE"
MESSAGE_CREATE = "MESSAGE_CREATE" MESSAGE_CREATE = "MESSAGE_CREATE"

225
main.py
View File

@ -7,7 +7,7 @@ import json
from websockets import Response from websockets import Response
from enums import OpCode, EventTitle from enums import OpCode, EventTitle, MessageComponentType
with open('configuration.json', 'r') as file: with open('configuration.json', 'r') as file:
CONFIG = json.load(file) CONFIG = json.load(file)
@ -23,44 +23,6 @@ async def create_command():
"name": "poll", "name": "poll",
"type": 1, "type": 1,
"description": "Des votes sur lesquels gamble !", "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, res: Response = requests.post(f"{API_URL}/applications/{APPLICATION_ID}/commands", json=body,
headers={"Authorization": f"Bot {TOKEN}"}) headers={"Authorization": f"Bot {TOKEN}"})
@ -80,35 +42,52 @@ async def init_commands():
await create_command() await create_command()
async def create_poll(channel_id: int, label: str, vote_options: list[dict]): async def create_poll(interaction_id: int, interaction_token: str, components: list[dict]):
body: dict = { question = next(
"content": "salut feikrf", (comp["components"][0]["value"] for comp in components if comp["components"][0]["custom_id"] == "label"),
"components": [ "Question du poll"
{ )
"type": 1,
"components": [ vote_options = []
{ for index, comp in enumerate(components):
"type": 4, if "vote" in comp["components"][0]["custom_id"] and comp["components"][0]["value"] != "":
"custom_id": "textinput", vote_text = comp["components"][0]["value"]
"label": "Montant de la somme pariée", vote_options.append({
"min_length": 1, "answer_id": len(vote_options) + 1,
"required": True, "poll_media": {"text": vote_text}
"value": "50", })
"style": 1
} 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, res = requests.post(
headers={"Authorization": f"Bot {TOKEN}"}) f"{API_URL}/interactions/{interaction_id}/{interaction_token}/callback",
json=body,
headers={"Authorization": f"Bot {TOKEN}"}
)
if res.status_code == 400: if res.status_code == 400:
print(res.json()) print(res.json())
else: else:
print(res) print(res)
async def identify(websocket): async def identify(websocket):
payload: dict = { payload: dict = {
"op": OpCode.IDENTIFY, "op": OpCode.IDENTIFY,
@ -125,53 +104,93 @@ async def identify(websocket):
await websocket.send(json.dumps(payload)) 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 = { body = {
"type": 4, "type": 9,
"data": { "data": {
"embeds": [ "title": "Créer un vote",
"custom_id": "poll_id_create",
"components": [
{ {
"title": "Les paris sont ouvert !", "type": 1,
"description": "Vous pouvez parier vos points, ils ne passeront pas en dessous de 50." "components": [
}
],
"poll": {
"question":
{
"text": "Question du poll"
},
"answers":
[
{ {
"answer_id": 1, "type": 4,
"poll_media": "custom_id": "label",
{ "label": "Question du vote",
"text": "vote1" "placeholder": "Shy aime-t-il les robots?",
} "style": 1,
}, "min_length": 1,
{ "required": True
"answer_id": 2,
"poll_media":
{
"text": "vote2"
}
},
{
"answer_id": 3,
"poll_media":
{
"text": "vote3"
}
} }
], ]
"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, res = requests.post(f"{API_URL}/interactions/{interaction_id}/{interaction_token}/callback", json=body,
headers={"Authorization": f"Bot {TOKEN}"}) headers={"Authorization": f"Bot {TOKEN}"})
print(res) if res.status_code == 400:
print(res.json())
else:
print(res)
async def heartbeat(websocket, interval): async def heartbeat(websocket, interval):
@ -185,12 +204,12 @@ async def get_event(response: Any):
case EventTitle.MESSAGE_CREATE: case EventTitle.MESSAGE_CREATE:
print(f'{response["d"]["author"]["username"]}: {response["d"]["content"]}') print(f'{response["d"]["author"]["username"]}: {response["d"]["content"]}')
case EventTitle.INTERACTION_CREATE: case EventTitle.INTERACTION_CREATE:
print(response) print(response["d"]["data"].keys())
await create_interaction_response(int(response["d"]["id"]), response["d"]["token"]) if "custom_id" in response["d"]["data"].keys():
# poll_data: list = response["d"]["data"]["options"] if response["d"]["data"]["custom_id"] == "poll_id_create":
# channel_id = int(response["d"]["channel_id"]) await create_poll(int(response["d"]["id"]), response["d"]["token"], response["d"]["data"]["components"])
# label = poll_data.pop(0)
# await create_poll(channel_id=channel_id, label=label, vote_options=poll_data) await create_poll_form(int(response["d"]["id"]), response["d"]["token"])
case _: case _:
print(response) print(response)

View File

@ -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