Basic inserts

This commit is contained in:
Guams 2025-03-23 13:32:50 +01:00
parent 04b475b892
commit 2b3443e48a
4 changed files with 61 additions and 19 deletions

View File

@ -6,7 +6,35 @@ async def user_exists(conn, user_id, guild_id):
cur.close()
return result != None
return result is not None
async def insert_vote_options(conn, vote_id, vote_option):
cur = conn.cursor()
cur.execute(
"INSERT INTO "
"vote (vote_id, vote_option) "
"VALUES (%s, %s)",
(vote_id, vote_option)
)
conn.commit()
cur.close()
async def bet(conn, vote_id: str, poll_id: str, user_id: str, guild_id: str):
cur = conn.cursor()
cur.execute(
"INSERT INTO "
"vote_member (user_id, poll_id, guild_id, vote_id) "
"VALUES (%s, %s, %s, %s)",
(user_id, poll_id, guild_id, vote_id)
)
conn.commit()
cur.close()
async def guild_exists(conn, guild_id):
@ -17,21 +45,23 @@ async def guild_exists(conn, guild_id):
cur.close()
return result != None
return result is not None
async def insert_poll(conn, poll_id, guild_id, question):
async def insert_poll(conn, creator, poll_id, guild_id, question):
cur = conn.cursor()
cur.execute(
"INSERT INTO "
"poll (poll_id, guild_id, question, opened) "
"VALUES (%s, %s, %s, true)",
(poll_id, guild_id, question)
"poll (creator, poll_id, guild_id, question, opened) "
"VALUES (%s, %s, %s, %s, true)",
(creator, poll_id, guild_id, question)
)
conn.commit()
cur.close()
async def insert_guild(conn, guild_id):
cur = conn.cursor()
cur.execute(

View File

@ -12,7 +12,7 @@ CREATE TABLE guild
CREATE TABLE vote
(
vote_id UUID,
vote_id VARCHAR(255),
vote_option VARCHAR(255),
PRIMARY KEY (vote_id)
);
@ -20,6 +20,7 @@ CREATE TABLE vote
CREATE TABLE poll
(
poll_id UUID,
creator VARCHAR(255),
guild_id VARCHAR(255),
question VARCHAR(255),
opened BOOLEAN,
@ -32,6 +33,8 @@ CREATE TABLE member
user_id VARCHAR(255),
guild_id VARCHAR(255),
global_name VARCHAR(255),
points bigint DEFAULT 50,
bet_value bigint DEFAULT 50,
avatar TEXT,
PRIMARY KEY (user_id),
FOREIGN KEY (guild_id) REFERENCES guild (guild_id)
@ -41,7 +44,7 @@ CREATE TABLE vote_member
(
user_id VARCHAR(255),
guild_id VARCHAR(255),
vote_id UUID,
vote_id VARCHAR(255),
poll_id UUID,
FOREIGN KEY (guild_id) REFERENCES guild (guild_id),
FOREIGN KEY (user_id) REFERENCES member (user_id),

View File

@ -27,12 +27,14 @@ class MessageComponentType(str, Enum):
CHANNEL_SELECT = 8
MODAL = 9
class EventTitle(str, Enum):
INTERACTION_CREATE = "INTERACTION_CREATE"
MESSAGE_CREATE = "MESSAGE_CREATE"
GUILD_CREATE = "GUILD_CREATE"
READY = "READY"
class InteractionType(int, Enum):
PING = 1
APPLICATION_COMMAND = 2

27
main.py
View File

@ -6,9 +6,8 @@ from uuid import uuid4
import psycopg2
import requests
import websockets
from websockets import Response
from database import user_exists, insert_user, guild_exists, insert_guild
from database import user_exists, insert_user, guild_exists, insert_guild, insert_poll, insert_vote_options, bet
from enums import OpCode, EventTitle, InteractionType
with open('configuration.json', 'r') as file:
@ -37,7 +36,7 @@ async def create_command():
"type": 1,
"description": "Des votes sur lesquels gamble !",
}
res: Response = requests.post(f"{API_URL}/applications/{APPLICATION_ID}/commands", json=body,
requests.post(f"{API_URL}/applications/{APPLICATION_ID}/commands", json=body,
headers={"Authorization": f"Bot {TOKEN}"})
@ -54,7 +53,8 @@ async def init_commands():
await create_command()
async def create_poll(interaction_id: str, interaction_token: str, components: list[dict]):
async def create_poll(guild_id: str, creator_id: str, poll_created_id: str, interaction_id: str, 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"
@ -71,7 +71,7 @@ async def create_poll(interaction_id: str, interaction_token: str, components: l
"type": 2,
"label": vote_text,
"style": 1,
"custom_id": f"vote_{index}"
"custom_id": f"{poll_created_id}_{vote_text}_{index}"
})
action_rows = [{"type": 1, "components": buttons[i:i + 5]} for i in range(0, len(buttons), 5)]
@ -104,7 +104,9 @@ async def create_poll(interaction_id: str, interaction_token: str, components: l
if res.status_code == 400:
print(res.json())
else:
print(res)
await insert_poll(conn=conn, creator=creator_id, poll_id=poll_created_id, guild_id=guild_id, question=question)
for option in buttons:
await insert_vote_options(conn=conn, vote_id=option["custom_id"], vote_option=option["label"])
async def identify(websocket):
@ -140,7 +142,7 @@ async def create_poll_form(interaction_id: str, interaction_token: str, guild_id
"type": 9,
"data": {
"title": "Créer un vote",
"custom_id": poll_id,
"custom_id": str(poll_id),
"components": [
{
"type": 1,
@ -236,6 +238,7 @@ async def vote_confirmation(interaction_id: str, interaction_token: str, custom_
if not await guild_exists(conn=conn, guild_id=guild_id):
await insert_guild(conn=conn, guild_id=guild_id)
await insert_user(conn=conn, user_id=user_id, avatar=avatar, guild_id=guild_id, global_name=global_name)
await bet(conn=conn, guild_id=guild_id, user_id=user_id, vote_id=custom_id, poll_id=custom_id.split("_")[0])
body = {
"type": 4,
"data": {
@ -261,10 +264,14 @@ async def get_event(response: Any):
print(f'{response["d"]["author"]["username"]}: {response["d"]["content"]}')
case EventTitle.INTERACTION_CREATE:
if response["d"]["type"] == InteractionType.MODAL_SUBMIT:
print(response["d"])
await create_poll(
response["d"]["id"],
response["d"]["token"],
response["d"]["data"]["components"]
guild_id=response["d"]["guild"]["id"],
creator_id=response["d"]["member"]["id"],
poll_created_id=response["d"]["data"]["custom_id"],
interaction_id=response["d"]["id"],
interaction_token=response["d"]["token"],
components=response["d"]["data"]["components"]
)
elif response["d"]["type"] == InteractionType.MESSAGE_COMPONENT:
custom_id = response["d"]["data"]["custom_id"]