Bot get poll response from user
This commit is contained in:
parent
0900d8ed68
commit
50931d5215
37
database.sql
Normal file
37
database.sql
Normal file
@ -0,0 +1,37 @@
|
||||
DROP TABLE IF EXISTS vote_utilisateur;
|
||||
DROP TABLE IF EXISTS utilisateur;
|
||||
DROP TABLE IF EXISTS poll;
|
||||
DROP TABLE IF EXISTS vote;
|
||||
|
||||
|
||||
CREATE TABLE vote (
|
||||
vote_id UUID,
|
||||
vote_option VARCHAR(255),
|
||||
nbr_vote INT,
|
||||
PRIMARY KEY (vote_id)
|
||||
);
|
||||
|
||||
CREATE TABLE poll (
|
||||
poll_id UUID,
|
||||
vote_id UUID,
|
||||
question VARCHAR(255),
|
||||
opened BOOLEAN,
|
||||
PRIMARY KEY (poll_id),
|
||||
FOREIGN KEY (vote_id) REFERENCES vote(vote_id)
|
||||
);
|
||||
|
||||
CREATE TABLE utilisateur (
|
||||
user_id BIGINT,
|
||||
global_name VARCHAR(255),
|
||||
avatar TEXT,
|
||||
PRIMARY KEY (user_id)
|
||||
);
|
||||
|
||||
CREATE TABLE vote_utilisateur (
|
||||
user_id BIGINT,
|
||||
vote_id UUID,
|
||||
poll_id UUID,
|
||||
FOREIGN KEY (user_id) REFERENCES utilisateur(user_id),
|
||||
FOREIGN KEY (vote_id) REFERENCES vote(vote_id),
|
||||
FOREIGN KEY (poll_id) REFERENCES poll(poll_id)
|
||||
);
|
7
enums.py
7
enums.py
@ -32,3 +32,10 @@ class EventTitle(str, Enum):
|
||||
MESSAGE_CREATE = "MESSAGE_CREATE"
|
||||
GUILD_CREATE = "GUILD_CREATE"
|
||||
READY = "READY"
|
||||
|
||||
class InteractionType(int, Enum):
|
||||
PING = 1
|
||||
APPLICATION_COMMAND = 2
|
||||
MESSAGE_COMPONENT = 3
|
||||
APPLICATION_COMMAND_AUTOCOMPLETE = 4
|
||||
MODAL_SUBMIT = 5
|
||||
|
73
main.py
73
main.py
@ -7,7 +7,7 @@ import json
|
||||
|
||||
from websockets import Response
|
||||
|
||||
from enums import OpCode, EventTitle, MessageComponentType
|
||||
from enums import OpCode, EventTitle, InteractionType
|
||||
|
||||
with open('configuration.json', 'r') as file:
|
||||
CONFIG = json.load(file)
|
||||
@ -74,30 +74,40 @@ async def create_poll(interaction_id: int, interaction_token: str, components: l
|
||||
"Question du poll"
|
||||
)
|
||||
|
||||
vote_options = []
|
||||
print(components)
|
||||
|
||||
vote_options = ""
|
||||
buttons = []
|
||||
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}
|
||||
vote_options += f"Option {index}: {vote_text}\n"
|
||||
|
||||
buttons.append({
|
||||
"type": 2,
|
||||
"label": vote_text,
|
||||
"style": 1,
|
||||
"custom_id": f"vote_{index}"
|
||||
})
|
||||
|
||||
action_rows = [{"type": 1, "components": buttons[i:i + 5]} for i in range(0, len(buttons), 5)]
|
||||
|
||||
body = {
|
||||
"type": 4,
|
||||
"data": {
|
||||
"embeds": [
|
||||
{
|
||||
"title": "Les paris sont ouverts !",
|
||||
"description": "Vous pouvez parier vos points, ils ne passeront pas en dessous de 50."
|
||||
"description": "Vous pouvez parier vos points, ils ne passeront pas en dessous de 50.",
|
||||
"color": 5613215
|
||||
},
|
||||
{
|
||||
"title": f"Sondage: {question}",
|
||||
"description": vote_options,
|
||||
"color": 16775222
|
||||
}
|
||||
],
|
||||
"poll": {
|
||||
"question": {"text": question},
|
||||
"answers": vote_options,
|
||||
"allow_multiselect": False,
|
||||
"layout_type": 1
|
||||
}
|
||||
"components": action_rows
|
||||
}
|
||||
}
|
||||
|
||||
@ -232,21 +242,44 @@ async def heartbeat(websocket, interval):
|
||||
await asyncio.sleep(interval / 1000)
|
||||
await websocket.send(json.dumps({"op": OpCode.HEARTBEAT, "d": None}))
|
||||
|
||||
async def send_private_response(interaction_id: int, interaction_token: str, custom_id: str, user_id: str):
|
||||
|
||||
# Récupérer le nom de l'option corespondant à l'id en BDD
|
||||
body = {
|
||||
"type": 4,
|
||||
"data": {
|
||||
"content": f"<@{user_id}>, tu as misé 50 sur l'option **{custom_id.split('_')[1]}** ! 🎉",
|
||||
"flags": 64
|
||||
}
|
||||
}
|
||||
|
||||
res = requests.post(
|
||||
f"{API_URL}/interactions/{interaction_id}/{interaction_token}/callback",
|
||||
json=body,
|
||||
headers={"Authorization": f"Bot {TOKEN}", "Content-Type": "application/json"}
|
||||
)
|
||||
if res.status_code == 400:
|
||||
print(res.json())
|
||||
else:
|
||||
print(res)
|
||||
|
||||
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:
|
||||
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"]
|
||||
if response["d"]["type"] == InteractionType.MODAL_SUBMIT:
|
||||
await create_poll(
|
||||
int(response["d"]["id"]),
|
||||
response["d"]["token"],
|
||||
response["d"]["data"]["components"]
|
||||
)
|
||||
print(response)
|
||||
await create_poll_form(int(response["d"]["id"]), response["d"]["token"])
|
||||
elif response["d"]["type"] == InteractionType.MESSAGE_COMPONENT:
|
||||
custom_id = response["d"]["data"]["custom_id"]
|
||||
user_id = response["d"]["member"]["user"]["id"]
|
||||
await send_private_response(int(response["d"]["id"]), response["d"]["token"], custom_id, user_id)
|
||||
elif response["d"]["type"] == InteractionType.APPLICATION_COMMAND:
|
||||
await create_poll_form(int(response["d"]["id"]), response["d"]["token"])
|
||||
case _:
|
||||
print(response)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user