Starting vote management
This commit is contained in:
parent
2b3443e48a
commit
ee017f740c
3
.gitignore
vendored
3
.gitignore
vendored
@ -7,4 +7,5 @@
|
|||||||
/dataSources/
|
/dataSources/
|
||||||
/dataSources.local.xml
|
/dataSources.local.xml
|
||||||
configuration.json
|
configuration.json
|
||||||
.idea/
|
.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):
|
async def user_exists(conn, user_id, guild_id):
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
|
|
||||||
@ -23,6 +26,28 @@ async def insert_vote_options(conn, vote_id, vote_option):
|
|||||||
cur.close()
|
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):
|
async def bet(conn, vote_id: str, poll_id: str, user_id: str, guild_id: str):
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
|
|
||||||
|
108
main.py
108
main.py
@ -7,7 +7,8 @@ import psycopg2
|
|||||||
import requests
|
import requests
|
||||||
import websockets
|
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
|
from enums import OpCode, EventTitle, InteractionType
|
||||||
|
|
||||||
with open('configuration.json', 'r') as file:
|
with open('configuration.json', 'r') as file:
|
||||||
@ -31,17 +32,36 @@ except Exception as e:
|
|||||||
|
|
||||||
|
|
||||||
async def create_command():
|
async def create_command():
|
||||||
body: dict = {
|
bodies = [
|
||||||
"name": "poll",
|
{
|
||||||
"type": 1,
|
"name": "poll",
|
||||||
"description": "Des votes sur lesquels gamble !",
|
"type": 1,
|
||||||
}
|
"description": "Des votes sur lesquels gamble !",
|
||||||
requests.post(f"{API_URL}/applications/{APPLICATION_ID}/commands", json=body,
|
},
|
||||||
headers={"Authorization": f"Bot {TOKEN}"})
|
{
|
||||||
|
"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():
|
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)
|
commands = json.loads(res.content)
|
||||||
for command in commands:
|
for command in commands:
|
||||||
response = requests.delete(f"{API_URL}/applications/{APPLICATION_ID}/commands/{command['id']}",
|
response = requests.delete(f"{API_URL}/applications/{APPLICATION_ID}/commands/{command['id']}",
|
||||||
@ -53,7 +73,8 @@ async def init_commands():
|
|||||||
await create_command()
|
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]):
|
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"),
|
||||||
@ -104,7 +125,8 @@ async def create_poll(guild_id: str, creator_id: str, poll_created_id: str, inte
|
|||||||
if res.status_code == 400:
|
if res.status_code == 400:
|
||||||
print(res.json())
|
print(res.json())
|
||||||
else:
|
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:
|
for option in buttons:
|
||||||
await insert_vote_options(conn=conn, vote_id=option["custom_id"], vote_option=option["label"])
|
await insert_vote_options(conn=conn, vote_id=option["custom_id"], vote_option=option["label"])
|
||||||
|
|
||||||
@ -232,20 +254,32 @@ async def heartbeat(websocket, interval):
|
|||||||
await websocket.send(json.dumps({"op": OpCode.HEARTBEAT, "d": None}))
|
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):
|
avatar: str, global_name: str):
|
||||||
if not await user_exists(conn=conn, user_id=user_id, guild_id=guild_id):
|
user_vote = await get_user_vote(conn=conn, user_id=user_id, poll_id=custom_id.split("_")[0])
|
||||||
if not await guild_exists(conn=conn, guild_id=guild_id):
|
if user_vote:
|
||||||
await insert_guild(conn=conn, guild_id=guild_id)
|
body = {
|
||||||
await insert_user(conn=conn, user_id=user_id, avatar=avatar, guild_id=guild_id, global_name=global_name)
|
"type": 4,
|
||||||
await bet(conn=conn, guild_id=guild_id, user_id=user_id, vote_id=custom_id, poll_id=custom_id.split("_")[0])
|
"data": {
|
||||||
body = {
|
"content": f"<@{user_id}>, tu as déjà voté pour **{user_vote[0].split('_')[1]}**",
|
||||||
"type": 4,
|
"flags": 64
|
||||||
"data": {
|
}
|
||||||
"content": f"<@{user_id}>, tu as misé 50 sur l'option **{custom_id.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])
|
||||||
|
body = {
|
||||||
|
"type": 4,
|
||||||
|
"data": {
|
||||||
|
"content": f"<@{user_id}>, tu as misé 50 sur l'option **{custom_id.split('_')[1]}** ! 🎉",
|
||||||
|
"flags": 64
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
res = requests.post(
|
res = requests.post(
|
||||||
f"{API_URL}/interactions/{interaction_id}/{interaction_token}/callback",
|
f"{API_URL}/interactions/{interaction_id}/{interaction_token}/callback",
|
||||||
@ -264,10 +298,9 @@ 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(
|
||||||
guild_id=response["d"]["guild"]["id"],
|
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"],
|
poll_created_id=response["d"]["data"]["custom_id"],
|
||||||
interaction_id=response["d"]["id"],
|
interaction_id=response["d"]["id"],
|
||||||
interaction_token=response["d"]["token"],
|
interaction_token=response["d"]["token"],
|
||||||
@ -283,15 +316,36 @@ async def get_event(response: Any):
|
|||||||
guild_id=response["d"]["guild"]["id"],
|
guild_id=response["d"]["guild"]["id"],
|
||||||
avatar=response["d"]["member"]["user"]["avatar"],
|
avatar=response["d"]["member"]["user"]["avatar"],
|
||||||
global_name=response["d"]["member"]["user"]["global_name"])
|
global_name=response["d"]["member"]["user"]["global_name"])
|
||||||
elif response["d"]["type"] == InteractionType.APPLICATION_COMMAND:
|
elif response["d"]["type"] == InteractionType.APPLICATION_COMMAND and response["d"]["data"][
|
||||||
print(response["d"])
|
"name"] == 'poll':
|
||||||
await create_poll_form(interaction_id=response["d"]["id"],
|
await create_poll_form(interaction_id=response["d"]["id"],
|
||||||
interaction_token=response["d"]["token"],
|
interaction_token=response["d"]["token"],
|
||||||
guild_id=response["d"]["guild_id"])
|
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 _:
|
case _:
|
||||||
print(response)
|
print(response)
|
||||||
|
|
||||||
|
|
||||||
|
async def bet_error_message(message: str):
|
||||||
|
print(message)
|
||||||
|
|
||||||
|
|
||||||
async def connect():
|
async def connect():
|
||||||
async with websockets.connect(GATEWAY_URL) as websocket:
|
async with websockets.connect(GATEWAY_URL) as websocket:
|
||||||
response = json.loads(await websocket.recv())
|
response = json.loads(await websocket.recv())
|
||||||
|
Loading…
Reference in New Issue
Block a user