diff --git a/database/models.py b/database/models.py index 1cfca05..5590b51 100644 --- a/database/models.py +++ b/database/models.py @@ -19,6 +19,13 @@ class GameBundle(db.Model): name = db.Column(db.String(256)) json = db.Column(db.String(1024)) +class LiveAlert(db.Model): + id = db.Column(db.Integer, primary_key=True) + enable = db.Column(db.Boolean, default=False) + login = db.Column(db.String(128)) + notify_channel = db.Column(db.Integer) + message = db.Column(db.String(2000)) + class Message(db.Model): id = db.Column(db.Integer, primary_key=True) enable = db.Column(db.Boolean, default=False) diff --git a/database/schema.sql b/database/schema.sql index 24ff873..0989286 100644 --- a/database/schema.sql +++ b/database/schema.sql @@ -22,6 +22,14 @@ CREATE TABLE IF NOT EXISTS `humeur` ( `text` VARCHAR(256) NULL ); +CREATE TABLE IF NOT EXISTS live_alert ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + `enable` BOOLEAN NOT NULL DEFAULT TRUE, + `login` VARCHAR(128) UNIQUE NOT NULL, + `notify_channel` INTEGER NOT NULL, + `message` VARCHAR(2000) 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 9b54326..837161c 100644 --- a/discordbot/__init__.py +++ b/discordbot/__init__.py @@ -6,7 +6,7 @@ import random from database import db from database.helpers import ConfigurationHelper from database.models import Configuration, Humeur, Commande -from discord import Message +from discord import Message, TextChannel from discordbot.humblebundle import checkHumbleBundleAndNotify from protondb import searhProtonDb @@ -36,6 +36,14 @@ class DiscordBot(discord.Client): # toutes les 30 minutes await asyncio.sleep(30*60) + def getAllTextChannel(self) -> list[TextChannel]: + channels = [] + for channel in self.get_all_channels(): + if isinstance(channel, TextChannel): + channels.append(channel) + return channels + + def begin(self) : token = Configuration.query.filter_by(key='discord_token').first() if token : diff --git a/webapp/__init__.py b/webapp/__init__.py index 341f179..2edb4b4 100644 --- a/webapp/__init__.py +++ b/webapp/__init__.py @@ -2,4 +2,4 @@ from flask import Flask webapp = Flask(__name__) -from webapp import commandes, configurations, index, humeurs, protondb +from webapp import commandes, configurations, index, humeurs, protondb, live_alert diff --git a/webapp/configurations.py b/webapp/configurations.py index 691629a..63ee1a8 100644 --- a/webapp/configurations.py +++ b/webapp/configurations.py @@ -3,15 +3,10 @@ from webapp import webapp from database import db from database.helpers import ConfigurationHelper from discordbot import bot -from discord import TextChannel @webapp.route("/configurations") def openConfigurations(): - channels = [] - for channel in bot.get_all_channels(): - if isinstance(channel, TextChannel): - channels.append(channel) - return render_template("configurations.html", configuration = ConfigurationHelper(), channels = channels) + return render_template("configurations.html", configuration = ConfigurationHelper(), channels = bot.getAllTextChannel()) @webapp.route("/configurations/update", methods=['POST']) def updateConfiguration(): diff --git a/webapp/live_alert.py b/webapp/live_alert.py new file mode 100644 index 0000000..bd365ff --- /dev/null +++ b/webapp/live_alert.py @@ -0,0 +1,54 @@ +from flask import render_template, request, redirect, url_for + +from webapp import webapp +from database import db +from database.models import LiveAlert +from discordbot import bot + + +@webapp.route("/live-alert") +def openLiveAlert(): + alerts : list[LiveAlert] = LiveAlert.query.all() + channels = bot.getAllTextChannel() + for alert in alerts : + for channel in channels: + if alert.notify_channel == channel.id : + alert.notify_channel_name = channel.name + return render_template("live-alert.html", alerts = alerts, channels = channels) + +@webapp.route("/live-alert/add", methods=['POST']) +def addLiveAlert(): + alert = LiveAlert(enable = True, login = request.form.get('login'), notify_channel = request.form.get('notify_channel'), message = request.form.get('message')) + db.session.add(alert) + db.session.commit() + return redirect(url_for("openLiveAlert")) + +@webapp.route("/live-alert/toggle/") +def toggleLiveAlert(id): + alert : LiveAlert = LiveAlert.query.get_or_404(id) + alert.enable = not alert.enable + db.session.commit() + return redirect(url_for("openLiveAlert")) + +@webapp.route("/live-alert/edit/") +def openEditLiveAlert(id): + alert = LiveAlert.query.get_or_404(id) + channels = bot.getAllTextChannel() + return render_template("live-alert.html", alert = alert, channels = channels) + +@webapp.route("/live-alert/edit/", methods=['POST']) +def submitEditLiveAlert(id): + alert : LiveAlert = LiveAlert.query.get_or_404(id) + alert.login = request.form.get('login') + alert.notify_channel = request.form.get('notify_channel') + alert.message = request.form.get('message') + db.session.commit() + return redirect(url_for("openLiveAlert")) + + +@webapp.route("/live-alert/del/") +def delLiveAlert(id): + alert = LiveAlert.query.get_or_404(id) + db.session.delete(alert) + db.session.commit() + return redirect(url_for("openLiveAlert")) diff --git a/webapp/static/css/style.css b/webapp/static/css/style.css index d546e72..05fd81a 100644 --- a/webapp/static/css/style.css +++ b/webapp/static/css/style.css @@ -11,3 +11,7 @@ table td { white-space: normal; max-width: 250px; } + +a.icon { + text-decoration: none; +} \ No newline at end of file diff --git a/webapp/templates/commandes.html b/webapp/templates/commandes.html index 99ac545..77568d8 100644 --- a/webapp/templates/commandes.html +++ b/webapp/templates/commandes.html @@ -19,12 +19,12 @@ {{ commande.trigger }} {{ commande.response }} - + {{ '✅' if commande.discord_enable else '❌' }} - + {{ '✅' if commande.twitch_enable else '❌' }} diff --git a/webapp/templates/live-alert.html b/webapp/templates/live-alert.html new file mode 100644 index 0000000..d3f8753 --- /dev/null +++ b/webapp/templates/live-alert.html @@ -0,0 +1,60 @@ +{% extends "template.html" %} + +{% block content %} +

Alerte Live

+ +{% if not alert %} +

Alertes

+ + + + + + + + + + + + {% for alert in alerts %} + + + + + + + + {% endfor %} + +
ChaineCanalMessageActif#
{{alert.login}}{{alert.notify_channel_name}}{{alert.message}} + {{ '✅' if alert.enable else '❌' }} + + + 🗑 +
+{% endif %} + +

{{ 'Editer une alerte' if alert else 'Ajouter une alerte de Live' }}

+
+ + + + + + + +

+ La chaine est le login de la chaine, par exemple chainesteve pour https://www.twitch.tv/chainesteve. +

+

+ Pour le message vous avez acces à ces variables : TODO +

+
+ +{% endblock %} \ No newline at end of file diff --git a/webapp/templates/template.html b/webapp/templates/template.html index 86f6506..beca4fe 100644 --- a/webapp/templates/template.html +++ b/webapp/templates/template.html @@ -18,6 +18,7 @@