mirror of
https://github.com/skylanix/MamieHenriette.git
synced 2026-02-06 14:50:34 +01:00
Resoudre les probleme des 2 thread bloquant
This commit is contained in:
32
README.md
32
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
|
||||
```
|
||||
@@ -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'
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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'))
|
||||
@@ -6,3 +6,4 @@ audioop-lts; python_version>='3.13'
|
||||
|
||||
flask>=2.3.2
|
||||
flask-sqlalchemy>=3.0.3
|
||||
waitress>=3.0.2
|
||||
26
run-web.py
26
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)
|
||||
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()
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
19
webapp/configurations.py
Normal file
19
webapp/configurations.py
Normal file
@@ -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/<key>', 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'))
|
||||
13
webapp/templates/configurations.html
Normal file
13
webapp/templates/configurations.html
Normal file
@@ -0,0 +1,13 @@
|
||||
{% extends "template.html" %}
|
||||
|
||||
{% block content %}
|
||||
<h1>Configuration de Mamie.</h1>
|
||||
|
||||
<h2>Api</h2>
|
||||
<form action="{{ url_for('setConfiguration', key = 'discord_token') }}" method="POST">
|
||||
<label for="value">Api Discord (cachée)</label>
|
||||
<input name="value" type="password" />
|
||||
<input type="Submit" value="Définir">
|
||||
<p>Nécéssite un redémarrage</p>
|
||||
</form>
|
||||
{% endblock %}
|
||||
@@ -22,6 +22,7 @@
|
||||
<li><a href="/humeurs">Humeurs</a></li>
|
||||
<li><a href="/messages">Messages</a></li>
|
||||
<li><a href="/moderation">Modération</a></li>
|
||||
<li><a href="/configurations">Configurations</a></li>
|
||||
</ul>
|
||||
</nav>
|
||||
</header>
|
||||
@@ -30,8 +31,7 @@
|
||||
</main>
|
||||
<footer>
|
||||
<hr>
|
||||
<p>Mamie Henriette by <a href="mailto:TODO">Mow</a>, <a href="mailto:shionn@gmail.com">Shionn</a>, <a
|
||||
href="mailto:TODO">Sky</a>, <a href="mailto:TODO">Steve</a></p>
|
||||
<p>Mamie Henriette by la commu discord de 573v3</p>
|
||||
</footer>
|
||||
<jsp:invoke fragment="scripts" />
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user