Starting vote management
This commit is contained in:
parent
2b3443e48a
commit
ee017f740c
1
.gitignore
vendored
1
.gitignore
vendored
@ -8,3 +8,4 @@
|
||||
/dataSources.local.xml
|
||||
configuration.json
|
||||
.idea/
|
||||
.venv/*
|
||||
|
25
database.py
25
database.py
@ -1,3 +1,6 @@
|
||||
from psycopg2.extensions import connection, cursor
|
||||
|
||||
|
||||
async def user_exists(conn, user_id, guild_id):
|
||||
cur = conn.cursor()
|
||||
|
||||
@ -23,6 +26,28 @@ async def insert_vote_options(conn, vote_id, vote_option):
|
||||
cur.close()
|
||||
|
||||
|
||||
async def change_bet(conn: connection, user_id: str, guild_id: str, new_bet_amount: int):
|
||||
cur: cursor = conn.cursor()
|
||||
|
||||
cur.execute("UPDATE member SET bet_value = %s WHERE user_id=%s AND guild_id=%s",
|
||||
(new_bet_amount, user_id, guild_id))
|
||||
|
||||
|
||||
async def get_user_vote(conn: connection, user_id: str, poll_id: str):
|
||||
cur: cursor = conn.cursor()
|
||||
|
||||
cur.execute(""
|
||||
"SELECT vote_id "
|
||||
"FROM vote_member "
|
||||
"WHERE user_id=%s "
|
||||
"AND poll_id=%s",
|
||||
(user_id, poll_id)
|
||||
)
|
||||
|
||||
conn.commit()
|
||||
cur.close()
|
||||
|
||||
|
||||
async def bet(conn, vote_id: str, poll_id: str, user_id: str, guild_id: str):
|
||||
cur = conn.cursor()
|
||||
|
||||
|
76
main.py
76
main.py
@ -7,7 +7,8 @@ import psycopg2
|
||||
import requests
|
||||
import websockets
|
||||
|
||||
from database import user_exists, insert_user, guild_exists, insert_guild, insert_poll, insert_vote_options, bet
|
||||
from database import (user_exists, insert_user, guild_exists, insert_guild,
|
||||
insert_poll, insert_vote_options, bet, get_user_vote, change_bet)
|
||||
from enums import OpCode, EventTitle, InteractionType
|
||||
|
||||
with open('configuration.json', 'r') as file:
|
||||
@ -31,17 +32,36 @@ except Exception as e:
|
||||
|
||||
|
||||
async def create_command():
|
||||
body: dict = {
|
||||
bodies = [
|
||||
{
|
||||
"name": "poll",
|
||||
"type": 1,
|
||||
"description": "Des votes sur lesquels gamble !",
|
||||
},
|
||||
{
|
||||
"name": "bet",
|
||||
"type": 1,
|
||||
"description": "Pour pouvoir planifier le prochain pari !",
|
||||
"required": True,
|
||||
"min_value": 0,
|
||||
"options": [
|
||||
{
|
||||
"name": "somme",
|
||||
"description": "La somme du prochain pari",
|
||||
"type": 3
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
for body in bodies:
|
||||
requests.post(f"{API_URL}/applications/{APPLICATION_ID}/commands", json=body,
|
||||
headers={"Authorization": f"Bot {TOKEN}"})
|
||||
|
||||
|
||||
async def init_commands():
|
||||
res = requests.get(f"{API_URL}/applications/{APPLICATION_ID}/commands", headers={"Authorization": f"Bot {TOKEN}"})
|
||||
res = requests.get(f"{API_URL}/applications/{APPLICATION_ID}/commands",
|
||||
headers={"Authorization": f"Bot {TOKEN}"})
|
||||
commands = json.loads(res.content)
|
||||
for command in commands:
|
||||
response = requests.delete(f"{API_URL}/applications/{APPLICATION_ID}/commands/{command['id']}",
|
||||
@ -53,7 +73,8 @@ async def init_commands():
|
||||
await create_command()
|
||||
|
||||
|
||||
async def create_poll(guild_id: str, creator_id: str, poll_created_id: str, interaction_id: str, interaction_token: str,
|
||||
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"),
|
||||
@ -104,7 +125,8 @@ async def create_poll(guild_id: str, creator_id: str, poll_created_id: str, inte
|
||||
if res.status_code == 400:
|
||||
print(res.json())
|
||||
else:
|
||||
await insert_poll(conn=conn, creator=creator_id, poll_id=poll_created_id, guild_id=guild_id, question=question)
|
||||
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"])
|
||||
|
||||
@ -232,13 +254,25 @@ async def heartbeat(websocket, interval):
|
||||
await websocket.send(json.dumps({"op": OpCode.HEARTBEAT, "d": None}))
|
||||
|
||||
|
||||
async def vote_confirmation(interaction_id: str, interaction_token: str, custom_id: str, user_id: str, guild_id: str,
|
||||
async def vote_confirmation(interaction_id: str, interaction_token: str, custom_id: str, user_id: str,
|
||||
guild_id: str,
|
||||
avatar: str, global_name: str):
|
||||
user_vote = await get_user_vote(conn=conn, user_id=user_id, poll_id=custom_id.split("_")[0])
|
||||
if user_vote:
|
||||
body = {
|
||||
"type": 4,
|
||||
"data": {
|
||||
"content": f"<@{user_id}>, tu as déjà voté pour **{user_vote[0].split('_')[1]}**",
|
||||
"flags": 64
|
||||
}
|
||||
}
|
||||
else:
|
||||
if not await user_exists(conn=conn, user_id=user_id, 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_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])
|
||||
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": {
|
||||
@ -264,10 +298,9 @@ 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(
|
||||
guild_id=response["d"]["guild"]["id"],
|
||||
creator_id=response["d"]["member"]["id"],
|
||||
creator_id=response["d"]["member"]["user"]["id"],
|
||||
poll_created_id=response["d"]["data"]["custom_id"],
|
||||
interaction_id=response["d"]["id"],
|
||||
interaction_token=response["d"]["token"],
|
||||
@ -283,15 +316,36 @@ async def get_event(response: Any):
|
||||
guild_id=response["d"]["guild"]["id"],
|
||||
avatar=response["d"]["member"]["user"]["avatar"],
|
||||
global_name=response["d"]["member"]["user"]["global_name"])
|
||||
elif response["d"]["type"] == InteractionType.APPLICATION_COMMAND:
|
||||
print(response["d"])
|
||||
elif response["d"]["type"] == InteractionType.APPLICATION_COMMAND and response["d"]["data"][
|
||||
"name"] == 'poll':
|
||||
await create_poll_form(interaction_id=response["d"]["id"],
|
||||
interaction_token=response["d"]["token"],
|
||||
guild_id=response["d"]["guild_id"])
|
||||
elif response["d"]["type"] == InteractionType.APPLICATION_COMMAND and response["d"]["data"][
|
||||
"name"] == 'bet':
|
||||
try:
|
||||
command_option: str = response["d"]["data"]["options"][0]["value"]
|
||||
if await guild_exists(conn=conn, guild_id=response["d"]["guild_id"]):
|
||||
if command_option.isnumeric() and await user_exists(conn=conn,
|
||||
user_id=response["d"]["member"]["user"][
|
||||
"id"],
|
||||
guild_id=response["d"]["guild_id"]):
|
||||
await change_bet(conn=conn,
|
||||
guild_id=response["d"]["guild_id"],
|
||||
user_id=response["d"]["member"]["user"]["id"],
|
||||
new_bet_amount=response["d"]["data"]["name"])
|
||||
else:
|
||||
await bet_error_message("Vous devez rentrer un nombre")
|
||||
except Exception:
|
||||
await bet_error_message("Vous avez oublié de donner une somme !")
|
||||
case _:
|
||||
print(response)
|
||||
|
||||
|
||||
async def bet_error_message(message: str):
|
||||
print(message)
|
||||
|
||||
|
||||
async def connect():
|
||||
async with websockets.connect(GATEWAY_URL) as websocket:
|
||||
response = json.loads(await websocket.recv())
|
||||
|
Loading…
Reference in New Issue
Block a user