diff --git a/README.md b/README.md index 3d11a8a..697498a 100644 --- a/README.md +++ b/README.md @@ -28,3 +28,4 @@ Readme in progress... - [ ] corriger le problème de seuil - [ ] changer la bet_value après un vote perdant (on se retrouve parfois avec un bet_value > points) +- [ ] update l'username (quand l'utilisateur en a un nouveau...) pareil pour l'avatar....... diff --git a/database.py b/database.py index 80d2a34..5b3eb2b 100644 --- a/database.py +++ b/database.py @@ -95,6 +95,23 @@ async def get_user_vote(conn: connection, user_id: str, poll_id: str): return result +async def get_user_by_id(conn: connection, user_id: str, guild_id: str): + cur: cursor = conn.cursor() + + cur.execute("" + "SELECT username, points, bet_value " + "FROM member " + "WHERE user_id=%s " + "AND guild_id=%s", + (user_id, guild_id) + ) + + result = cur.fetchone() + cur.close() + + return result + + async def get_user_points(conn: connection, user_id: str, guild_id: str): cur: cursor = conn.cursor() @@ -225,14 +242,14 @@ async def insert_guild(conn, guild_id): cur.close() -async def insert_user(conn, user_id, guild_id, username, avatar): +async def insert_user(conn, user_id, guild_id, username): 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) + "member (user_id, guild_id, username) " + "VALUES (%s, %s, %s)", + (user_id, guild_id, username) ) conn.commit() diff --git a/database.sql b/database.sql index 83f0763..f5d8435 100644 --- a/database.sql +++ b/database.sql @@ -37,7 +37,6 @@ CREATE TABLE member username VARCHAR(255), points bigint DEFAULT 50 NOT NULL CHECK (points >= 50), bet_value bigint DEFAULT 50, - avatar TEXT, PRIMARY KEY (user_id), FOREIGN KEY (guild_id) REFERENCES guild (guild_id) ); diff --git a/enums.py b/enums.py index 52faf75..bdc720d 100644 --- a/enums.py +++ b/enums.py @@ -41,3 +41,17 @@ class InteractionType(int, Enum): MESSAGE_COMPONENT = 3 APPLICATION_COMMAND_AUTOCOMPLETE = 4 MODAL_SUBMIT = 5 + + +class ApplicationCommand(int, Enum): + SUB_COMMAND = 1 + SUB_COMMAND_GROUP = 2 + STRING = 3 + INTEGER = 4 + BOOLEAN = 5 + USER = 6 + CHANNEL = 7 + ROLE = 8 + MENTIONABLE = 9 + NUMBER = 10 + ATTACHMENT = 11 diff --git a/main.py b/main.py index 0eba655..4d201b8 100644 --- a/main.py +++ b/main.py @@ -11,8 +11,9 @@ from database import (user_exists, insert_user, guild_exists, insert_guild, insert_poll, insert_vote_options, bet, get_user_vote, change_bet, is_last_poll_created_opened, get_poll_id_opened, close_poll, get_bet_value, get_bet_value_multipliers, get_vote_id_by_vote_option_and_poll_id, - get_users_and_bet_value_by_vote_id, add_user_points, minus_user_points, get_user_points) -from enums import OpCode, EventTitle, InteractionType + get_users_and_bet_value_by_vote_id, add_user_points, minus_user_points, get_user_points, + get_user_by_id) +from enums import OpCode, EventTitle, InteractionType, ApplicationCommand with open('configuration.json', 'r') as file: CONFIG = json.load(file) @@ -38,12 +39,12 @@ async def create_command(): bodies = [ { "name": "poll", - "type": 1, + "type": ApplicationCommand.SUB_COMMAND, "description": "Des votes sur lesquels gamble !", }, { "name": "bet", - "type": 1, + "type": ApplicationCommand.SUB_COMMAND, "description": "Pour pouvoir planifier le prochain pari !", "options": [ { @@ -51,27 +52,35 @@ async def create_command(): "required": True, "min_value": 0, "description": "La somme du prochain pari", - "type": 4 + "type": ApplicationCommand.INTEGER } ] }, { "name": "close", - "type": 1, + "type": ApplicationCommand.SUB_COMMAND, "description": "Pour clôturer un sondage", "options": [ { "name": "option", "description": "L'issue du sondage", "required": True, - "type": 3 + "type": ApplicationCommand.STRING } ] }, - { # à améliorer en /profil <@user_id> - "name": "me", - "type": 1, - "description": "Afficher des informations sur vous", + { + "name": "profil", + "type": ApplicationCommand.SUB_COMMAND, + "description": "Afficher des informations sur un utilisateur", + "options": [ + { + "name": "user", + "description": "La personne concernée", + "required": False, + "type": ApplicationCommand.USER + } + ] } ] @@ -180,13 +189,12 @@ async def identify(websocket): await websocket.send(json.dumps(payload)) -async def create_poll_form(interaction_id: str, interaction_token: str, guild_id: str, user_id: str, username: str, - avatar: str): +async def create_poll_form(interaction_id: str, interaction_token: str, guild_id: str, user_id: str, username: str): poll_id = uuid4() if not await guild_exists(conn=conn, guild_id=guild_id): await insert_guild(conn=conn, guild_id=guild_id) if not await user_exists(conn=conn, user_id=user_id, guild_id=guild_id): - await insert_user(conn=conn, user_id=user_id, guild_id=guild_id, username=username, avatar=avatar) + await insert_user(conn=conn, user_id=user_id, guild_id=guild_id, username=username) if not await is_last_poll_created_opened(conn=conn, creator=user_id): body = { "type": 9, @@ -298,8 +306,7 @@ async def heartbeat(websocket, interval): async def vote_confirmation(interaction_id: str, interaction_token: str, custom_id: str, user_id: str, - guild_id: str, - avatar: str, username: str): + guild_id: str, username: str): user_vote = await get_user_vote(conn=conn, user_id=user_id, poll_id=custom_id.split("_")[0]) if user_vote is not None: body = { @@ -313,7 +320,7 @@ async def vote_confirmation(interaction_id: str, interaction_token: str, custom_ 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, username=username) + await insert_user(conn=conn, user_id=user_id, guild_id=guild_id, username=username) bet_value = await get_bet_value(conn=conn, user_id=user_id, guild_id=guild_id) await bet(conn=conn, guild_id=guild_id, user_id=user_id, vote_id=custom_id, poll_id=custom_id.split("_")[0], bet_value=bet_value) @@ -338,12 +345,11 @@ async def vote_confirmation(interaction_id: str, interaction_token: str, custom_ async def close_poll_cmd(guild_id: str, user_id: str, interaction_id: str, issue: str, interaction_token: str, - username: str, - avatar: str): + username: str): if not await guild_exists(conn=conn, guild_id=guild_id): await insert_guild(conn=conn, guild_id=guild_id) if not await user_exists(conn=conn, user_id=user_id, guild_id=guild_id): - await insert_user(conn=conn, user_id=user_id, guild_id=guild_id, username=username, avatar=avatar) + await insert_user(conn=conn, user_id=user_id, guild_id=guild_id, username=username) poll: tuple = await get_poll_id_opened(conn=conn, creator=user_id) if poll is not None: winner_id = await get_vote_id_by_vote_option_and_poll_id(conn=conn, vote_option=issue, poll_id=poll[1]) @@ -417,7 +423,6 @@ async def get_event(response: Any): custom_id=custom_id, user_id=user_id, guild_id=response["d"]["guild"]["id"], - avatar=response["d"]["member"]["user"]["avatar"], username=response["d"]["member"]["user"]["username"]) elif (response["d"]["type"] == InteractionType.APPLICATION_COMMAND and response["d"]["data"]["name"] == 'poll'): @@ -425,7 +430,6 @@ async def get_event(response: Any): user_id=response["d"]["member"]["user"]["id"], interaction_token=response["d"]["token"], guild_id=response["d"]["guild_id"], - avatar=response["d"]["member"]["user"]["avatar"], username=response["d"]["member"]["user"]["username"]) elif (response["d"]["type"] == InteractionType.APPLICATION_COMMAND and response["d"]["data"]["name"] == 'close'): @@ -434,7 +438,6 @@ async def get_event(response: Any): interaction_id=response["d"]["id"], interaction_token=response["d"]["token"], issue=response["d"]["data"]["options"][0]["value"], - avatar=response["d"]["member"]["user"]["avatar"], username=response["d"]["member"]["user"]["username"]) elif (response["d"]["type"] == InteractionType.APPLICATION_COMMAND and response["d"]["data"]["name"] == 'bet'): @@ -443,18 +446,72 @@ async def get_event(response: Any): command_option=command_option, user_id=response["d"]["member"]["user"]["id"], username=response["d"]["member"]["user"]["username"], - avatar=response["d"]["member"]["user"]["avatar"], interaction_id=response["d"]["id"], interaction_token=response["d"]["token"]) + elif (response["d"]["type"] == InteractionType.APPLICATION_COMMAND and + response["d"]["data"]["name"] == 'profil'): + command_option: str = '' + user_avatar: str = '' + username: str = '' + try: + command_option = response["d"]["data"]["options"][0]["value"] + user_avatar = response["d"]["data"]["resolved"]["users"][str(command_option)]["avatar"] + username = response["d"]["data"]["resolved"]["users"][str(command_option)]["username"] + except Exception as e: + pass + if command_option == '': + command_option = response["d"]["member"]["user"]["id"] + if user_avatar == '': + user_avatar = response["d"]["member"]["user"]["avatar"] + username = response["d"]["member"]["user"]["username"] + await show_user_profile(guild_id=response["d"]["guild_id"], + interaction_id=response["d"]["id"], + interaction_token=response["d"]["token"], + avatar=user_avatar, + concerned_user_id=command_option, + username=username) case _: print(response) -async def change_bet_cmd(guild_id: str, command_option: int, user_id: str, username: str, avatar: str, +async def show_user_profile(guild_id: str, interaction_id: str, interaction_token: str, + concerned_user_id: str, avatar: str, username: str): + if not await guild_exists(conn=conn, guild_id=guild_id): + await insert_guild(conn=conn, guild_id=guild_id) + if not await user_exists(conn=conn, user_id=concerned_user_id, guild_id=guild_id): + await insert_user(conn=conn, user_id=concerned_user_id, guild_id=guild_id, username=username) + + concerned_user = await get_user_by_id(conn=conn, user_id=concerned_user_id, guild_id=guild_id) + body = { + "type": 4, + "data": { + "embeds": [ + { + "title": f"Profil de {concerned_user[0]}", + "description": f"Solde : {concerned_user[1]}\n" + f"Valeur du prochain pari: {concerned_user[2]}", + "image": { + "url": f"https://cdn.discordapp.com/avatars/{concerned_user_id}/{avatar}" + }, + "color": 11141375 + } + ], + } + } + + res = requests.post(f"{API_URL}/interactions/{interaction_id}/{interaction_token}/callback", json=body, + headers={"Authorization": f"Bot {TOKEN}"}) + if res.status_code == 400: + print(res.json()) + else: + print(res) + + +async def change_bet_cmd(guild_id: str, command_option: int, user_id: str, username: str, interaction_id: str, interaction_token: str): if not await guild_exists(conn=conn, guild_id=guild_id): - await insert_user(conn=conn, guild_id=guild_id, user_id=user_id, username=username, avatar=avatar) + await insert_user(conn=conn, guild_id=guild_id, user_id=user_id, username=username) if not await user_exists(conn=conn, user_id=user_id, guild_id=guild_id): await insert_guild(conn=conn, guild_id=guild_id) @@ -514,9 +571,6 @@ async def connect(): async def main(): gateway_connect = asyncio.create_task(connect()) # await init_commands() - # guild_ids = await get_guilds_ids() - # for guild_id in guild_ids: - # await get_guild_members(guild_id) await gateway_connect