Dynamic poll creation
This commit is contained in:
parent
7b8122a69f
commit
6573710066
11
enums.py
11
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"
|
||||
|
225
main.py
225
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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user