From d242639855de35407eb9eb33068cfad3c6478f9c Mon Sep 17 00:00:00 2001 From: Kepka Ludovic Date: Sat, 9 Aug 2025 16:55:21 +0200 Subject: [PATCH] Resoudre les probleme des 2 thread bloquant --- README.md | 32 +++++++++++++++++++++++++++- database/__init__.py | 1 - database/models.py | 9 +++++--- database/schema.sql | 6 ++++++ discordbot/__init__.py | 16 +++++++++----- requirements.txt | 3 ++- run-web.py | 26 +++++++++++++++++++--- webapp/__init__.py | 2 +- webapp/configurations.py | 19 +++++++++++++++++ webapp/templates/configurations.html | 13 +++++++++++ webapp/templates/template.html | 4 ++-- 11 files changed, 114 insertions(+), 17 deletions(-) create mode 100644 webapp/configurations.py create mode 100644 webapp/templates/configurations.html diff --git a/README.md b/README.md index f43c289..68e01d3 100755 --- a/README.md +++ b/README.md @@ -141,5 +141,35 @@ pip install -r requirements.txt ## Exécution ```bash -TOKEN=votre_token_discord python3 bot.py +python3 run-web.py +``` + +# Structure du projet + +``` +. +|-- database : module de connexion à la BDD +| |-- __init.py__ +| |-- models.py : contient les pojo représentant chaque table +| |-- schema.sql : contient un scrip sql d'initialisation de la bdd, celui-ci doit être réentrant +| +|-- discordbot : module de connexion à discord +| |-- __init.py__ +| +|-- webapp : module du site web d'administration +| |-- static : Ressource fixe directement accessible par le navigateir +| | |-- css +| | |-- ... +| | +| |-- template : Fichier html +| | |-- template.html : structure globale du site +| | |-- commandes.html : page de gestion des commandes +| | |-- ... +| | +| |-- __init.py__ +| |-- index.py : controller de la page d'acceuil +| |-- commandes.py : controller de gestion des commandes +| |-- ... +| +|-- run-web.py : launcher ``` \ No newline at end of file diff --git a/database/__init__.py b/database/__init__.py index 007f2a5..36795f9 100644 --- a/database/__init__.py +++ b/database/__init__.py @@ -1,5 +1,4 @@ from flask_sqlalchemy import SQLAlchemy -from sqlalchemy.sql import text from webapp import webapp webapp.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db' diff --git a/database/models.py b/database/models.py index 9223926..05f1ad5 100644 --- a/database/models.py +++ b/database/models.py @@ -1,13 +1,16 @@ from database import db +class Configuration(db.Model): + key = db.Column(db.String(32), primary_key=True) + value = db.Column(db.String(512)) + class Humeur(db.Model): id = db.Column(db.Integer, primary_key=True) enable = db.Column(db.Boolean, default=True) - text = db.Column(db.String(200)) + text = db.Column(db.String(256)) class Message(db.Model): id = db.Column(db.Integer, primary_key=True) enable = db.Column(db.Boolean, default=False) - text = db.Column(db.String(200)) - # en seconde + text = db.Column(db.String(256)) periodicity = db.Column(db.Integer) diff --git a/database/schema.sql b/database/schema.sql index 01742cf..0ba37a3 100644 --- a/database/schema.sql +++ b/database/schema.sql @@ -1,3 +1,9 @@ + +CREATE TABLE IF NOT EXISTS `configuration` ( + `key` VARCHAR(32) PRIMARY KEY, + `value` VARCHAR(512) NOT NULL +); + CREATE TABLE IF NOT EXISTS `message` ( id INTEGER PRIMARY KEY AUTOINCREMENT, `enable` BOOLEAN NOT NULL DEFAULT FALSE, diff --git a/discordbot/__init__.py b/discordbot/__init__.py index d03faad..6bbee83 100644 --- a/discordbot/__init__.py +++ b/discordbot/__init__.py @@ -1,10 +1,10 @@ import random import discord -import os +# import os import logging import asyncio -from database.models import Humeur from webapp import webapp +from database.models import Configuration, Humeur class DiscordBot(discord.Client): async def on_ready(self): @@ -15,14 +15,20 @@ class DiscordBot(discord.Client): # await self.get_channel(1123512494468644984).send("essai en python") async def updateStatus(self): + # from database.models import Humeur humeur = random.choice(Humeur.query.all()) if humeur != None: logging.info(f'changement de status {humeur.text}') await self.change_presence(status = discord.Status.online, activity = discord.CustomActivity(humeur.text)) await asyncio.sleep(60) + def begin(self) : + with webapp.app_context(): + token = Configuration.query.filter_by(key='discord_token').first() + if token : + self.run(token.value) + else : + logging.error('pas de token on ne lance pas discord') + intents = discord.Intents.default() bot = DiscordBot(intents=intents) - -with webapp.app_context(): - bot.run(os.getenv('TOKEN')) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 4b15b1c..ab7ca91 100755 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,5 @@ aiohttp>=3.7.4,<4 audioop-lts; python_version>='3.13' flask>=2.3.2 -flask-sqlalchemy>=3.0.3 \ No newline at end of file +flask-sqlalchemy>=3.0.3 +waitress>=3.0.2 \ No newline at end of file diff --git a/run-web.py b/run-web.py index 91af726..a619128 100644 --- a/run-web.py +++ b/run-web.py @@ -1,5 +1,25 @@ -from webapp import webapp -import discordbot +# +# import discordbot + +import multiprocessing +import logging + +def start_server(): + logging.info("Start Web Serveur") + from webapp import webapp + from waitress import serve + serve(webapp, host="0.0.0.0", port=5000) + +def start_discord_bot(): + logging.info("Start Discord Bot") + from discordbot import bot + bot.begin() if __name__ == '__main__': - webapp.run(debug=True) \ No newline at end of file + jobs = [] + jobs.append(multiprocessing.Process(target=start_server)) + jobs.append(multiprocessing.Process(target=start_discord_bot)) + + for job in jobs: job.start() + for job in jobs: job.join() + diff --git a/webapp/__init__.py b/webapp/__init__.py index 3adc80b..d011c17 100644 --- a/webapp/__init__.py +++ b/webapp/__init__.py @@ -2,4 +2,4 @@ from flask import Flask webapp = Flask(__name__) -from webapp import commandes, index, humeurs, messages, moderation +from webapp import commandes, configurations, index, humeurs, messages, moderation diff --git a/webapp/configurations.py b/webapp/configurations.py new file mode 100644 index 0000000..d8d6c0f --- /dev/null +++ b/webapp/configurations.py @@ -0,0 +1,19 @@ +from flask import render_template, request, redirect, url_for +from webapp import webapp +from database import db +from database.models import Configuration + +@webapp.route("/configurations") +def openConfigurations(): + return render_template("configurations.html") + +@webapp.route('/configurations/set/', methods=['POST']) +def setConfiguration(key): + conf = Configuration.query.filter_by(key=key).first() + if conf : + conf.value = request.form['value'] + else : + conf = Configuration(key = key, value = request.form['value']) + db.session.add(conf) + db.session.commit() + return redirect(url_for('openConfigurations')) diff --git a/webapp/templates/configurations.html b/webapp/templates/configurations.html new file mode 100644 index 0000000..075c718 --- /dev/null +++ b/webapp/templates/configurations.html @@ -0,0 +1,13 @@ +{% extends "template.html" %} + +{% block content %} +

Configuration de Mamie.

+ +

Api

+
+ + + +

Nécéssite un redémarrage

+
+{% endblock %} \ No newline at end of file diff --git a/webapp/templates/template.html b/webapp/templates/template.html index 3a71819..70bd900 100644 --- a/webapp/templates/template.html +++ b/webapp/templates/template.html @@ -22,6 +22,7 @@
  • Humeurs
  • Messages
  • Modération
  • +
  • Configurations
  • @@ -30,8 +31,7 @@

    -

    Mamie Henriette by Mow, Shionn, Sky, Steve

    +

    Mamie Henriette by la commu discord de 573v3