Add databases minimal management
This commit is contained in:
parent
5a9e792dc9
commit
04b475b892
59
database.py
Normal file
59
database.py
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
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 != None
|
||||||
|
|
||||||
|
|
||||||
|
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 != None
|
||||||
|
|
||||||
|
async def insert_poll(conn, poll_id, guild_id, question):
|
||||||
|
cur = conn.cursor()
|
||||||
|
|
||||||
|
cur.execute(
|
||||||
|
"INSERT INTO "
|
||||||
|
"poll (poll_id, guild_id, question, opened) "
|
||||||
|
"VALUES (%s, %s, %s, true)",
|
||||||
|
(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, global_name, avatar):
|
||||||
|
cur = conn.cursor()
|
||||||
|
|
||||||
|
cur.execute(
|
||||||
|
"INSERT INTO "
|
||||||
|
"member (user_id, guild_id, global_name, avatar) "
|
||||||
|
"VALUES (%s, %s, %s, %s)",
|
||||||
|
(user_id, guild_id, global_name, avatar)
|
||||||
|
)
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
cur.close()
|
39
database.sql
39
database.sql
@ -1,37 +1,50 @@
|
|||||||
DROP TABLE IF EXISTS vote_utilisateur;
|
DROP TABLE IF EXISTS vote_member;
|
||||||
DROP TABLE IF EXISTS utilisateur;
|
DROP TABLE IF EXISTS member;
|
||||||
DROP TABLE IF EXISTS poll;
|
DROP TABLE IF EXISTS poll;
|
||||||
DROP TABLE IF EXISTS vote;
|
DROP TABLE IF EXISTS vote;
|
||||||
|
DROP TABLE IF EXISTS guild;
|
||||||
|
|
||||||
|
CREATE TABLE guild
|
||||||
|
(
|
||||||
|
guild_id VARCHAR(255),
|
||||||
|
PRIMARY KEY (guild_id)
|
||||||
|
);
|
||||||
|
|
||||||
CREATE TABLE vote (
|
CREATE TABLE vote
|
||||||
|
(
|
||||||
vote_id UUID,
|
vote_id UUID,
|
||||||
vote_option VARCHAR(255),
|
vote_option VARCHAR(255),
|
||||||
nbr_vote INT,
|
|
||||||
PRIMARY KEY (vote_id)
|
PRIMARY KEY (vote_id)
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE poll (
|
CREATE TABLE poll
|
||||||
|
(
|
||||||
poll_id UUID,
|
poll_id UUID,
|
||||||
vote_id UUID,
|
guild_id VARCHAR(255),
|
||||||
question VARCHAR(255),
|
question VARCHAR(255),
|
||||||
opened BOOLEAN,
|
opened BOOLEAN,
|
||||||
PRIMARY KEY (poll_id),
|
PRIMARY KEY (poll_id),
|
||||||
FOREIGN KEY (vote_id) REFERENCES vote(vote_id)
|
FOREIGN KEY (guild_id) REFERENCES guild (guild_id)
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE utilisateur (
|
CREATE TABLE member
|
||||||
user_id BIGINT,
|
(
|
||||||
|
user_id VARCHAR(255),
|
||||||
|
guild_id VARCHAR(255),
|
||||||
global_name VARCHAR(255),
|
global_name VARCHAR(255),
|
||||||
avatar TEXT,
|
avatar TEXT,
|
||||||
PRIMARY KEY (user_id)
|
PRIMARY KEY (user_id),
|
||||||
|
FOREIGN KEY (guild_id) REFERENCES guild (guild_id)
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE vote_utilisateur (
|
CREATE TABLE vote_member
|
||||||
user_id BIGINT,
|
(
|
||||||
|
user_id VARCHAR(255),
|
||||||
|
guild_id VARCHAR(255),
|
||||||
vote_id UUID,
|
vote_id UUID,
|
||||||
poll_id UUID,
|
poll_id UUID,
|
||||||
FOREIGN KEY (user_id) REFERENCES utilisateur(user_id),
|
FOREIGN KEY (guild_id) REFERENCES guild (guild_id),
|
||||||
|
FOREIGN KEY (user_id) REFERENCES member (user_id),
|
||||||
FOREIGN KEY (vote_id) REFERENCES vote (vote_id),
|
FOREIGN KEY (vote_id) REFERENCES vote (vote_id),
|
||||||
FOREIGN KEY (poll_id) REFERENCES poll (poll_id)
|
FOREIGN KEY (poll_id) REFERENCES poll (poll_id)
|
||||||
);
|
);
|
83
main.py
83
main.py
@ -1,12 +1,14 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
import json
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
import psycopg2
|
||||||
import requests
|
import requests
|
||||||
import websockets
|
import websockets
|
||||||
import json
|
|
||||||
|
|
||||||
from websockets import Response
|
from websockets import Response
|
||||||
|
|
||||||
|
from database import user_exists, insert_user, guild_exists, insert_guild
|
||||||
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:
|
||||||
@ -17,31 +19,16 @@ GATEWAY_URL: str = "wss://gateway.discord.gg/?v=10&encoding=json"
|
|||||||
API_URL: str = "https://discord.com/api/v10"
|
API_URL: str = "https://discord.com/api/v10"
|
||||||
TOKEN: str = CONFIG["TOKEN"]
|
TOKEN: str = CONFIG["TOKEN"]
|
||||||
|
|
||||||
|
try:
|
||||||
async def create_button():
|
conn = psycopg2.connect(
|
||||||
body: dict = {
|
database=CONFIG["DB_NAME"],
|
||||||
"content": "This is a message with components",
|
user=CONFIG["DB_USER"],
|
||||||
"components": [
|
password=CONFIG["DB_PASSWORD"],
|
||||||
{
|
host=CONFIG["DB_ADDRESS"],
|
||||||
"type": 1,
|
port=CONFIG["DB_PORT"]
|
||||||
"components": [
|
)
|
||||||
{
|
except Exception as e:
|
||||||
"type": 2,
|
raise e
|
||||||
"label": "Click me!",
|
|
||||||
"style": 1,
|
|
||||||
"custom_id": "click_one"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
res = requests.post(f"{API_URL}/channels/840107060901052426/messages", json=body,
|
|
||||||
headers={"Authorization": f"Bot {TOKEN}"})
|
|
||||||
|
|
||||||
if res.status_code != 201:
|
|
||||||
print(res.json())
|
|
||||||
|
|
||||||
|
|
||||||
async def create_command():
|
async def create_command():
|
||||||
@ -52,7 +39,6 @@ async def create_command():
|
|||||||
}
|
}
|
||||||
res: Response = requests.post(f"{API_URL}/applications/{APPLICATION_ID}/commands", json=body,
|
res: Response = requests.post(f"{API_URL}/applications/{APPLICATION_ID}/commands", json=body,
|
||||||
headers={"Authorization": f"Bot {TOKEN}"})
|
headers={"Authorization": f"Bot {TOKEN}"})
|
||||||
print(res)
|
|
||||||
|
|
||||||
|
|
||||||
async def init_commands():
|
async def init_commands():
|
||||||
@ -68,14 +54,12 @@ async def init_commands():
|
|||||||
await create_command()
|
await create_command()
|
||||||
|
|
||||||
|
|
||||||
async def create_poll(interaction_id: int, interaction_token: str, components: list[dict]):
|
async def create_poll(interaction_id: str, interaction_token: str, 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"),
|
||||||
"Question du poll"
|
"Question du poll"
|
||||||
)
|
)
|
||||||
|
|
||||||
print(components)
|
|
||||||
|
|
||||||
vote_options = ""
|
vote_options = ""
|
||||||
buttons = []
|
buttons = []
|
||||||
for index, comp in enumerate(components):
|
for index, comp in enumerate(components):
|
||||||
@ -142,18 +126,21 @@ async def identify(websocket):
|
|||||||
"since": 91879201,
|
"since": 91879201,
|
||||||
"afk": False
|
"afk": False
|
||||||
},
|
},
|
||||||
"intents": 50364416,
|
"intents": 36871,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await websocket.send(json.dumps(payload))
|
await websocket.send(json.dumps(payload))
|
||||||
|
|
||||||
|
|
||||||
async def create_poll_form(interaction_id: int, interaction_token: str):
|
async def create_poll_form(interaction_id: str, interaction_token: str, guild_id: str):
|
||||||
|
poll_id = uuid4()
|
||||||
|
if not await guild_exists(conn=conn, guild_id=guild_id):
|
||||||
|
await insert_guild(conn=conn, guild_id=guild_id)
|
||||||
body = {
|
body = {
|
||||||
"type": 9,
|
"type": 9,
|
||||||
"data": {
|
"data": {
|
||||||
"title": "Créer un vote",
|
"title": "Créer un vote",
|
||||||
"custom_id": "poll_id_create",
|
"custom_id": poll_id,
|
||||||
"components": [
|
"components": [
|
||||||
{
|
{
|
||||||
"type": 1,
|
"type": 1,
|
||||||
@ -242,9 +229,13 @@ async def heartbeat(websocket, interval):
|
|||||||
await asyncio.sleep(interval / 1000)
|
await asyncio.sleep(interval / 1000)
|
||||||
await websocket.send(json.dumps({"op": OpCode.HEARTBEAT, "d": None}))
|
await websocket.send(json.dumps({"op": OpCode.HEARTBEAT, "d": None}))
|
||||||
|
|
||||||
async def send_private_response(interaction_id: int, interaction_token: str, custom_id: str, user_id: str):
|
|
||||||
|
|
||||||
# Récupérer le nom de l'option corespondant à l'id en BDD
|
async def vote_confirmation(interaction_id: str, interaction_token: str, custom_id: str, user_id: str, guild_id: str,
|
||||||
|
avatar: str, global_name: str):
|
||||||
|
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)
|
||||||
body = {
|
body = {
|
||||||
"type": 4,
|
"type": 4,
|
||||||
"data": {
|
"data": {
|
||||||
@ -263,6 +254,7 @@ async def send_private_response(interaction_id: int, interaction_token: str, cus
|
|||||||
else:
|
else:
|
||||||
print(res)
|
print(res)
|
||||||
|
|
||||||
|
|
||||||
async def get_event(response: Any):
|
async def get_event(response: Any):
|
||||||
match response["t"]:
|
match response["t"]:
|
||||||
case EventTitle.MESSAGE_CREATE:
|
case EventTitle.MESSAGE_CREATE:
|
||||||
@ -270,16 +262,25 @@ async def get_event(response: Any):
|
|||||||
case EventTitle.INTERACTION_CREATE:
|
case EventTitle.INTERACTION_CREATE:
|
||||||
if response["d"]["type"] == InteractionType.MODAL_SUBMIT:
|
if response["d"]["type"] == InteractionType.MODAL_SUBMIT:
|
||||||
await create_poll(
|
await create_poll(
|
||||||
int(response["d"]["id"]),
|
response["d"]["id"],
|
||||||
response["d"]["token"],
|
response["d"]["token"],
|
||||||
response["d"]["data"]["components"]
|
response["d"]["data"]["components"]
|
||||||
)
|
)
|
||||||
elif response["d"]["type"] == InteractionType.MESSAGE_COMPONENT:
|
elif response["d"]["type"] == InteractionType.MESSAGE_COMPONENT:
|
||||||
custom_id = response["d"]["data"]["custom_id"]
|
custom_id = response["d"]["data"]["custom_id"]
|
||||||
user_id = response["d"]["member"]["user"]["id"]
|
user_id = response["d"]["member"]["user"]["id"]
|
||||||
await send_private_response(int(response["d"]["id"]), response["d"]["token"], custom_id, user_id)
|
await vote_confirmation(interaction_id=response["d"]["id"],
|
||||||
|
interaction_token=response["d"]["token"],
|
||||||
|
custom_id=custom_id,
|
||||||
|
user_id=user_id,
|
||||||
|
guild_id=response["d"]["guild"]["id"],
|
||||||
|
avatar=response["d"]["member"]["user"]["avatar"],
|
||||||
|
global_name=response["d"]["member"]["user"]["global_name"])
|
||||||
elif response["d"]["type"] == InteractionType.APPLICATION_COMMAND:
|
elif response["d"]["type"] == InteractionType.APPLICATION_COMMAND:
|
||||||
await create_poll_form(int(response["d"]["id"]), response["d"]["token"])
|
print(response["d"])
|
||||||
|
await create_poll_form(interaction_id=response["d"]["id"],
|
||||||
|
interaction_token=response["d"]["token"],
|
||||||
|
guild_id=response["d"]["guild_id"])
|
||||||
case _:
|
case _:
|
||||||
print(response)
|
print(response)
|
||||||
|
|
||||||
@ -305,7 +306,9 @@ 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()
|
||||||
# await create_button()
|
# guild_ids = await get_guilds_ids()
|
||||||
|
# for guild_id in guild_ids:
|
||||||
|
# await get_guild_members(guild_id)
|
||||||
await gateway_connect
|
await gateway_connect
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user