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"
|
MESSAGE_CREATE = "MESSAGE_CREATE"
|
||||||
GUILD_CREATE = "GUILD_CREATE"
|
GUILD_CREATE = "GUILD_CREATE"
|
||||||
READY = "READY"
|
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 websockets import Response
|
||||||
|
|
||||||
from enums import OpCode, EventTitle, MessageComponentType
|
from enums import OpCode, EventTitle, InteractionType
|
||||||
|
|
||||||
with open('configuration.json', 'r') as file:
|
with open('configuration.json', 'r') as file:
|
||||||
CONFIG = json.load(file)
|
CONFIG = json.load(file)
|
||||||
@ -74,30 +74,40 @@ async def create_poll(interaction_id: int, interaction_token: str, components: l
|
|||||||
"Question du poll"
|
"Question du poll"
|
||||||
)
|
)
|
||||||
|
|
||||||
vote_options = []
|
print(components)
|
||||||
|
|
||||||
|
vote_options = ""
|
||||||
|
buttons = []
|
||||||
for index, comp in enumerate(components):
|
for index, comp in enumerate(components):
|
||||||
if "vote" in comp["components"][0]["custom_id"] and comp["components"][0]["value"] != "":
|
if "vote" in comp["components"][0]["custom_id"] and comp["components"][0]["value"] != "":
|
||||||
vote_text = comp["components"][0]["value"]
|
vote_text = comp["components"][0]["value"]
|
||||||
vote_options.append({
|
vote_options += f"Option {index}: {vote_text}\n"
|
||||||
"answer_id": len(vote_options) + 1,
|
|
||||||
"poll_media": {"text": vote_text}
|
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 = {
|
body = {
|
||||||
"type": 4,
|
"type": 4,
|
||||||
"data": {
|
"data": {
|
||||||
"embeds": [
|
"embeds": [
|
||||||
{
|
{
|
||||||
"title": "Les paris sont ouverts !",
|
"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": {
|
"components": action_rows
|
||||||
"question": {"text": question},
|
|
||||||
"answers": vote_options,
|
|
||||||
"allow_multiselect": False,
|
|
||||||
"layout_type": 1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -232,21 +242,44 @@ async def heartbeat(websocket, interval):
|
|||||||
await asyncio.sleep(interval / 1000)
|
await asyncio.sleep(interval / 1000)
|
||||||
await websocket.send(json.dumps({"op": OpCode.HEARTBEAT, "d": None}))
|
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):
|
async def get_event(response: Any):
|
||||||
match response["t"]:
|
match response["t"]:
|
||||||
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:
|
||||||
if "custom_id" in response["d"]["data"].keys():
|
if response["d"]["type"] == InteractionType.MODAL_SUBMIT:
|
||||||
if response["d"]["data"]["custom_id"] == "poll_id_create":
|
await create_poll(
|
||||||
await create_poll(
|
int(response["d"]["id"]),
|
||||||
int(response["d"]["id"]),
|
response["d"]["token"],
|
||||||
response["d"]["token"],
|
response["d"]["data"]["components"]
|
||||||
response["d"]["data"]["components"]
|
|
||||||
)
|
)
|
||||||
print(response)
|
elif response["d"]["type"] == InteractionType.MESSAGE_COMPONENT:
|
||||||
await create_poll_form(int(response["d"]["id"]), response["d"]["token"])
|
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 _:
|
case _:
|
||||||
print(response)
|
print(response)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user