Basic inserts
This commit is contained in:
parent
04b475b892
commit
2b3443e48a
42
database.py
42
database.py
@ -6,7 +6,35 @@ async def user_exists(conn, user_id, guild_id):
|
|||||||
|
|
||||||
cur.close()
|
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):
|
async def guild_exists(conn, guild_id):
|
||||||
@ -17,21 +45,23 @@ async def guild_exists(conn, guild_id):
|
|||||||
|
|
||||||
cur.close()
|
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 = conn.cursor()
|
||||||
|
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"INSERT INTO "
|
"INSERT INTO "
|
||||||
"poll (poll_id, guild_id, question, opened) "
|
"poll (creator, poll_id, guild_id, question, opened) "
|
||||||
"VALUES (%s, %s, %s, true)",
|
"VALUES (%s, %s, %s, %s, true)",
|
||||||
(poll_id, guild_id, question)
|
(creator, poll_id, guild_id, question)
|
||||||
)
|
)
|
||||||
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
cur.close()
|
cur.close()
|
||||||
|
|
||||||
|
|
||||||
async def insert_guild(conn, guild_id):
|
async def insert_guild(conn, guild_id):
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
cur.execute(
|
cur.execute(
|
||||||
|
@ -12,7 +12,7 @@ CREATE TABLE guild
|
|||||||
|
|
||||||
CREATE TABLE vote
|
CREATE TABLE vote
|
||||||
(
|
(
|
||||||
vote_id UUID,
|
vote_id VARCHAR(255),
|
||||||
vote_option VARCHAR(255),
|
vote_option VARCHAR(255),
|
||||||
PRIMARY KEY (vote_id)
|
PRIMARY KEY (vote_id)
|
||||||
);
|
);
|
||||||
@ -20,6 +20,7 @@ CREATE TABLE vote
|
|||||||
CREATE TABLE poll
|
CREATE TABLE poll
|
||||||
(
|
(
|
||||||
poll_id UUID,
|
poll_id UUID,
|
||||||
|
creator VARCHAR(255),
|
||||||
guild_id VARCHAR(255),
|
guild_id VARCHAR(255),
|
||||||
question VARCHAR(255),
|
question VARCHAR(255),
|
||||||
opened BOOLEAN,
|
opened BOOLEAN,
|
||||||
@ -32,6 +33,8 @@ CREATE TABLE member
|
|||||||
user_id VARCHAR(255),
|
user_id VARCHAR(255),
|
||||||
guild_id VARCHAR(255),
|
guild_id VARCHAR(255),
|
||||||
global_name VARCHAR(255),
|
global_name VARCHAR(255),
|
||||||
|
points bigint DEFAULT 50,
|
||||||
|
bet_value bigint DEFAULT 50,
|
||||||
avatar TEXT,
|
avatar TEXT,
|
||||||
PRIMARY KEY (user_id),
|
PRIMARY KEY (user_id),
|
||||||
FOREIGN KEY (guild_id) REFERENCES guild (guild_id)
|
FOREIGN KEY (guild_id) REFERENCES guild (guild_id)
|
||||||
@ -41,7 +44,7 @@ CREATE TABLE vote_member
|
|||||||
(
|
(
|
||||||
user_id VARCHAR(255),
|
user_id VARCHAR(255),
|
||||||
guild_id VARCHAR(255),
|
guild_id VARCHAR(255),
|
||||||
vote_id UUID,
|
vote_id VARCHAR(255),
|
||||||
poll_id UUID,
|
poll_id UUID,
|
||||||
FOREIGN KEY (guild_id) REFERENCES guild (guild_id),
|
FOREIGN KEY (guild_id) REFERENCES guild (guild_id),
|
||||||
FOREIGN KEY (user_id) REFERENCES member (user_id),
|
FOREIGN KEY (user_id) REFERENCES member (user_id),
|
||||||
|
2
enums.py
2
enums.py
@ -27,12 +27,14 @@ class MessageComponentType(str, Enum):
|
|||||||
CHANNEL_SELECT = 8
|
CHANNEL_SELECT = 8
|
||||||
MODAL = 9
|
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"
|
||||||
GUILD_CREATE = "GUILD_CREATE"
|
GUILD_CREATE = "GUILD_CREATE"
|
||||||
READY = "READY"
|
READY = "READY"
|
||||||
|
|
||||||
|
|
||||||
class InteractionType(int, Enum):
|
class InteractionType(int, Enum):
|
||||||
PING = 1
|
PING = 1
|
||||||
APPLICATION_COMMAND = 2
|
APPLICATION_COMMAND = 2
|
||||||
|
29
main.py
29
main.py
@ -6,9 +6,8 @@ from uuid import uuid4
|
|||||||
import psycopg2
|
import psycopg2
|
||||||
import requests
|
import requests
|
||||||
import websockets
|
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
|
from enums import OpCode, EventTitle, InteractionType
|
||||||
|
|
||||||
with open('configuration.json', 'r') as file:
|
with open('configuration.json', 'r') as file:
|
||||||
@ -37,8 +36,8 @@ async def create_command():
|
|||||||
"type": 1,
|
"type": 1,
|
||||||
"description": "Des votes sur lesquels gamble !",
|
"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}"})
|
headers={"Authorization": f"Bot {TOKEN}"})
|
||||||
|
|
||||||
|
|
||||||
async def init_commands():
|
async def init_commands():
|
||||||
@ -54,7 +53,8 @@ async def init_commands():
|
|||||||
await create_command()
|
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(
|
question = next(
|
||||||
(comp["components"][0]["value"] for comp in components if comp["components"][0]["custom_id"] == "label"),
|
(comp["components"][0]["value"] for comp in components if comp["components"][0]["custom_id"] == "label"),
|
||||||
"Question du poll"
|
"Question du poll"
|
||||||
@ -71,7 +71,7 @@ async def create_poll(interaction_id: str, interaction_token: str, components: l
|
|||||||
"type": 2,
|
"type": 2,
|
||||||
"label": vote_text,
|
"label": vote_text,
|
||||||
"style": 1,
|
"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)]
|
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:
|
if res.status_code == 400:
|
||||||
print(res.json())
|
print(res.json())
|
||||||
else:
|
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):
|
async def identify(websocket):
|
||||||
@ -140,7 +142,7 @@ async def create_poll_form(interaction_id: str, interaction_token: str, guild_id
|
|||||||
"type": 9,
|
"type": 9,
|
||||||
"data": {
|
"data": {
|
||||||
"title": "Créer un vote",
|
"title": "Créer un vote",
|
||||||
"custom_id": poll_id,
|
"custom_id": str(poll_id),
|
||||||
"components": [
|
"components": [
|
||||||
{
|
{
|
||||||
"type": 1,
|
"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):
|
if not await guild_exists(conn=conn, guild_id=guild_id):
|
||||||
await insert_guild(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 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 = {
|
body = {
|
||||||
"type": 4,
|
"type": 4,
|
||||||
"data": {
|
"data": {
|
||||||
@ -261,10 +264,14 @@ async def get_event(response: Any):
|
|||||||
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 response["d"]["type"] == InteractionType.MODAL_SUBMIT:
|
if response["d"]["type"] == InteractionType.MODAL_SUBMIT:
|
||||||
|
print(response["d"])
|
||||||
await create_poll(
|
await create_poll(
|
||||||
response["d"]["id"],
|
guild_id=response["d"]["guild"]["id"],
|
||||||
response["d"]["token"],
|
creator_id=response["d"]["member"]["id"],
|
||||||
response["d"]["data"]["components"]
|
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:
|
elif response["d"]["type"] == InteractionType.MESSAGE_COMPONENT:
|
||||||
custom_id = response["d"]["data"]["custom_id"]
|
custom_id = response["d"]["data"]["custom_id"]
|
||||||
|
Loading…
Reference in New Issue
Block a user