'/profil' command

This commit is contained in:
guams 2025-03-30 19:20:40 +02:00
parent d7bb8aa270
commit 2449d0955b
5 changed files with 119 additions and 34 deletions

View File

@ -28,3 +28,4 @@ Readme in progress...
- [ ] corriger le problème de seuil - [ ] corriger le problème de seuil
- [ ] changer la bet_value après un vote perdant (on se retrouve parfois avec un bet_value > points) - [ ] 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.......

View File

@ -95,6 +95,23 @@ async def get_user_vote(conn: connection, user_id: str, poll_id: str):
return result 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): async def get_user_points(conn: connection, user_id: str, guild_id: str):
cur: cursor = conn.cursor() cur: cursor = conn.cursor()
@ -225,14 +242,14 @@ async def insert_guild(conn, guild_id):
cur.close() 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 = conn.cursor()
cur.execute( cur.execute(
"INSERT INTO " "INSERT INTO "
"member (user_id, guild_id, username, avatar) " "member (user_id, guild_id, username) "
"VALUES (%s, %s, %s, %s)", "VALUES (%s, %s, %s)",
(user_id, guild_id, username, avatar) (user_id, guild_id, username)
) )
conn.commit() conn.commit()

View File

@ -37,7 +37,6 @@ CREATE TABLE member
username VARCHAR(255), username VARCHAR(255),
points bigint DEFAULT 50 NOT NULL CHECK (points >= 50), points bigint DEFAULT 50 NOT NULL CHECK (points >= 50),
bet_value bigint DEFAULT 50, bet_value bigint DEFAULT 50,
avatar TEXT,
PRIMARY KEY (user_id), PRIMARY KEY (user_id),
FOREIGN KEY (guild_id) REFERENCES guild (guild_id) FOREIGN KEY (guild_id) REFERENCES guild (guild_id)
); );

View File

@ -41,3 +41,17 @@ class InteractionType(int, Enum):
MESSAGE_COMPONENT = 3 MESSAGE_COMPONENT = 3
APPLICATION_COMMAND_AUTOCOMPLETE = 4 APPLICATION_COMMAND_AUTOCOMPLETE = 4
MODAL_SUBMIT = 5 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

112
main.py
View File

@ -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, 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, 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_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) get_users_and_bet_value_by_vote_id, add_user_points, minus_user_points, get_user_points,
from enums import OpCode, EventTitle, InteractionType get_user_by_id)
from enums import OpCode, EventTitle, InteractionType, ApplicationCommand
with open('configuration.json', 'r') as file: with open('configuration.json', 'r') as file:
CONFIG = json.load(file) CONFIG = json.load(file)
@ -38,12 +39,12 @@ async def create_command():
bodies = [ bodies = [
{ {
"name": "poll", "name": "poll",
"type": 1, "type": ApplicationCommand.SUB_COMMAND,
"description": "Des votes sur lesquels gamble !", "description": "Des votes sur lesquels gamble !",
}, },
{ {
"name": "bet", "name": "bet",
"type": 1, "type": ApplicationCommand.SUB_COMMAND,
"description": "Pour pouvoir planifier le prochain pari !", "description": "Pour pouvoir planifier le prochain pari !",
"options": [ "options": [
{ {
@ -51,27 +52,35 @@ async def create_command():
"required": True, "required": True,
"min_value": 0, "min_value": 0,
"description": "La somme du prochain pari", "description": "La somme du prochain pari",
"type": 4 "type": ApplicationCommand.INTEGER
} }
] ]
}, },
{ {
"name": "close", "name": "close",
"type": 1, "type": ApplicationCommand.SUB_COMMAND,
"description": "Pour clôturer un sondage", "description": "Pour clôturer un sondage",
"options": [ "options": [
{ {
"name": "option", "name": "option",
"description": "L'issue du sondage", "description": "L'issue du sondage",
"required": True, "required": True,
"type": 3 "type": ApplicationCommand.STRING
} }
] ]
}, },
{ # à améliorer en /profil <@user_id> {
"name": "me", "name": "profil",
"type": 1, "type": ApplicationCommand.SUB_COMMAND,
"description": "Afficher des informations sur vous", "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)) 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, async def create_poll_form(interaction_id: str, interaction_token: str, guild_id: str, user_id: str, username: str):
avatar: str):
poll_id = uuid4() poll_id = uuid4()
if not await guild_exists(conn=conn, 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_guild(conn=conn, guild_id=guild_id)
if not await user_exists(conn=conn, user_id=user_id, 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): if not await is_last_poll_created_opened(conn=conn, creator=user_id):
body = { body = {
"type": 9, "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, async def vote_confirmation(interaction_id: str, interaction_token: str, custom_id: str, user_id: str,
guild_id: str, guild_id: str, username: str):
avatar: str, username: str):
user_vote = await get_user_vote(conn=conn, user_id=user_id, poll_id=custom_id.split("_")[0]) user_vote = await get_user_vote(conn=conn, user_id=user_id, poll_id=custom_id.split("_")[0])
if user_vote is not None: if user_vote is not None:
body = { 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 user_exists(conn=conn, user_id=user_id, guild_id=guild_id):
if not await guild_exists(conn=conn, 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_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) 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, 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) 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, async def close_poll_cmd(guild_id: str, user_id: str, interaction_id: str, issue: str, interaction_token: str,
username: str, username: str):
avatar: str):
if not await guild_exists(conn=conn, 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_guild(conn=conn, guild_id=guild_id)
if not await user_exists(conn=conn, user_id=user_id, 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) poll: tuple = await get_poll_id_opened(conn=conn, creator=user_id)
if poll is not None: 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]) 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, custom_id=custom_id,
user_id=user_id, user_id=user_id,
guild_id=response["d"]["guild"]["id"], guild_id=response["d"]["guild"]["id"],
avatar=response["d"]["member"]["user"]["avatar"],
username=response["d"]["member"]["user"]["username"]) username=response["d"]["member"]["user"]["username"])
elif (response["d"]["type"] == InteractionType.APPLICATION_COMMAND and elif (response["d"]["type"] == InteractionType.APPLICATION_COMMAND and
response["d"]["data"]["name"] == 'poll'): response["d"]["data"]["name"] == 'poll'):
@ -425,7 +430,6 @@ async def get_event(response: Any):
user_id=response["d"]["member"]["user"]["id"], user_id=response["d"]["member"]["user"]["id"],
interaction_token=response["d"]["token"], interaction_token=response["d"]["token"],
guild_id=response["d"]["guild_id"], guild_id=response["d"]["guild_id"],
avatar=response["d"]["member"]["user"]["avatar"],
username=response["d"]["member"]["user"]["username"]) username=response["d"]["member"]["user"]["username"])
elif (response["d"]["type"] == InteractionType.APPLICATION_COMMAND and elif (response["d"]["type"] == InteractionType.APPLICATION_COMMAND and
response["d"]["data"]["name"] == 'close'): response["d"]["data"]["name"] == 'close'):
@ -434,7 +438,6 @@ async def get_event(response: Any):
interaction_id=response["d"]["id"], interaction_id=response["d"]["id"],
interaction_token=response["d"]["token"], interaction_token=response["d"]["token"],
issue=response["d"]["data"]["options"][0]["value"], issue=response["d"]["data"]["options"][0]["value"],
avatar=response["d"]["member"]["user"]["avatar"],
username=response["d"]["member"]["user"]["username"]) username=response["d"]["member"]["user"]["username"])
elif (response["d"]["type"] == InteractionType.APPLICATION_COMMAND and elif (response["d"]["type"] == InteractionType.APPLICATION_COMMAND and
response["d"]["data"]["name"] == 'bet'): response["d"]["data"]["name"] == 'bet'):
@ -443,18 +446,72 @@ async def get_event(response: Any):
command_option=command_option, command_option=command_option,
user_id=response["d"]["member"]["user"]["id"], user_id=response["d"]["member"]["user"]["id"],
username=response["d"]["member"]["user"]["username"], username=response["d"]["member"]["user"]["username"],
avatar=response["d"]["member"]["user"]["avatar"],
interaction_id=response["d"]["id"], interaction_id=response["d"]["id"],
interaction_token=response["d"]["token"]) 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 _: case _:
print(response) 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): interaction_id: str, interaction_token: str):
if not await guild_exists(conn=conn, guild_id=guild_id): 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): if not await user_exists(conn=conn, user_id=user_id, guild_id=guild_id):
await insert_guild(conn=conn, guild_id=guild_id) await insert_guild(conn=conn, guild_id=guild_id)
@ -514,9 +571,6 @@ async def connect():
async def main(): async def main():
gateway_connect = asyncio.create_task(connect()) gateway_connect = asyncio.create_task(connect())
# await init_commands() # await init_commands()
# guild_ids = await get_guilds_ids()
# for guild_id in guild_ids:
# await get_guild_members(guild_id)
await gateway_connect await gateway_connect