diff --git a/database.py b/database.py index 5b3eb2b..680835b 100644 --- a/database.py +++ b/database.py @@ -291,8 +291,8 @@ async def minus_user_points(conn: connection, user_id: str, bet_value: int): cur.execute( "UPDATE member " "SET points = points - %s " - "WHERE user_id=%s", - (bet_value, user_id) + "WHERE user_id = %s AND points - %s >= 50", + (bet_value, user_id, bet_value) ) conn.commit() diff --git a/database.sql b/database.sql index f5d8435..aff685d 100644 --- a/database.sql +++ b/database.sql @@ -35,7 +35,7 @@ CREATE TABLE member user_id VARCHAR(255), guild_id VARCHAR(255), username VARCHAR(255), - points bigint DEFAULT 50 NOT NULL CHECK (points >= 50), + points bigint DEFAULT 50, bet_value bigint DEFAULT 50, PRIMARY KEY (user_id), FOREIGN KEY (guild_id) REFERENCES guild (guild_id) diff --git a/main.py b/main.py index 4d201b8..23f06c7 100644 --- a/main.py +++ b/main.py @@ -542,30 +542,51 @@ async def change_bet_cmd(guild_id: str, command_option: int, user_id: str, usern async def connect(): - async with websockets.connect(GATEWAY_URL) as websocket: - response = json.loads(await websocket.recv()) - heartbeat_interval = response["d"]["heartbeat_interval"] + while True: + try: + async with websockets.connect(GATEWAY_URL) as websocket: + response = json.loads(await websocket.recv()) + heartbeat_interval = response["d"]["heartbeat_interval"] - asyncio.create_task(heartbeat(websocket, heartbeat_interval)) + heartbeat_task = asyncio.create_task(heartbeat(websocket, heartbeat_interval)) + await identify(websocket) - await identify(websocket) + while True: + try: + response = json.loads(await websocket.recv()) + op = response.get("op") - connected = True - while connected: - response = json.loads(await websocket.recv()) - match response["op"]: - case OpCode.DISPATCH: - await get_event(response) - case OpCode.RECONNECT: - print("Reconnexion...") - connected = False - case _: + match op: + case OpCode.DISPATCH: + await get_event(response) + case OpCode.RECONNECT: + print("Reconnexion demandée...") + break + case OpCode.INVALID_SESSION: + print("Session invalide, réidentification...") + await asyncio.sleep(1) + await identify(websocket) + case _: + pass + + except websockets.exceptions.ConnectionClosed as e: + print(f"Connexion fermée : {e}") + break + except Exception as e: + print(f"Erreur dans le loop principal : {e}") + break + + heartbeat_task.cancel() + try: + await heartbeat_task + except asyncio.CancelledError: pass - # Reconnexion - await websocket.close() + except Exception as e: + print(f"Erreur de connexion WebSocket : {e}") + + print("Tentative de reconnexion dans 5 secondes...") await asyncio.sleep(5) - await connect() async def main():