164 lines
3.7 KiB
Python
164 lines
3.7 KiB
Python
from psycopg2.extensions import connection, cursor
|
|
|
|
|
|
async def user_exists(conn, user_id, guild_id):
|
|
cur = conn.cursor()
|
|
|
|
cur.execute("SELECT user_id FROM member WHERE user_id=%s AND guild_id=%s", (user_id, guild_id))
|
|
result = cur.fetchone()
|
|
|
|
cur.close()
|
|
|
|
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 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))
|
|
|
|
conn.commit()
|
|
cur.close()
|
|
|
|
|
|
async def is_last_poll_created_opened(conn: connection, creator: str):
|
|
cur: cursor = conn.cursor()
|
|
|
|
cur.execute("select opened "
|
|
"from poll "
|
|
"where creator=%s "
|
|
"and opened=true",
|
|
(creator,)
|
|
)
|
|
result = cur.fetchone()
|
|
cur.close()
|
|
|
|
if result is not None:
|
|
return result[0]
|
|
return
|
|
|
|
|
|
async def close_poll(conn: connection, poll_id: str):
|
|
cur: cursor = conn.cursor()
|
|
|
|
cur.execute("UPDATE poll SET opened = false WHERE poll_id=%s",
|
|
(poll_id,))
|
|
|
|
conn.commit()
|
|
cur.close()
|
|
|
|
|
|
async def get_poll_id_opened(conn: connection, creator: str):
|
|
cur: cursor = conn.cursor()
|
|
|
|
cur.execute("select opened, poll_id, question "
|
|
"from poll "
|
|
"where creator=%s "
|
|
"and opened=true",
|
|
(creator,)
|
|
)
|
|
result = cur.fetchone()
|
|
cur.close()
|
|
|
|
return result
|
|
|
|
|
|
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)
|
|
)
|
|
|
|
result = cur.fetchone()
|
|
cur.close()
|
|
|
|
return result
|
|
|
|
|
|
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):
|
|
cur = conn.cursor()
|
|
|
|
cur.execute("SELECT guild_id FROM guild WHERE guild_id=%s", (guild_id,))
|
|
result = cur.fetchone()
|
|
|
|
cur.close()
|
|
|
|
return result is not None
|
|
|
|
|
|
async def insert_poll(conn, creator, poll_id, guild_id, question):
|
|
cur = conn.cursor()
|
|
|
|
cur.execute(
|
|
"INSERT INTO "
|
|
"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(
|
|
"INSERT INTO "
|
|
"guild (guild_id) "
|
|
"VALUES (%s)",
|
|
(guild_id,)
|
|
)
|
|
|
|
conn.commit()
|
|
cur.close()
|
|
|
|
|
|
async def insert_user(conn, user_id, guild_id, username, avatar):
|
|
cur = conn.cursor()
|
|
|
|
cur.execute(
|
|
"INSERT INTO "
|
|
"member (user_id, guild_id, username, avatar) "
|
|
"VALUES (%s, %s, %s, %s)",
|
|
(user_id, guild_id, username, avatar)
|
|
)
|
|
|
|
conn.commit()
|
|
cur.close()
|
|
|
|
# nombre de vote par vote_id pour calculer l'issue du vote (et potentiellement la côte)
|
|
# select vote_id, count(distinct vote_member.user_id) from vote_member group by vote_id |