mirror of
https://github.com/skylanix/MamieHenriette.git
synced 2026-02-06 14:50:34 +01:00
Premlier essai de humble bundle, j'arrive pas à lister les canauxx dans la page de configuration
This commit is contained in:
@@ -9,6 +9,11 @@ class Humeur(db.Model):
|
||||
enable = db.Column(db.Boolean, default=True)
|
||||
text = db.Column(db.String(256))
|
||||
|
||||
class GameBundle(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(256))
|
||||
json = db.Column(db.String(1024))
|
||||
|
||||
class Message(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
enable = db.Column(db.Boolean, default=False)
|
||||
|
||||
@@ -4,6 +4,18 @@ CREATE TABLE IF NOT EXISTS `configuration` (
|
||||
`value` VARCHAR(512) NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `game_bundle` (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name VARCHAR(256) NOT NULL,
|
||||
json VARCHAR(1024) NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `humeur` (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
`enable` BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
`text` VARCHAR(256) NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `message` (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
`enable` BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
@@ -11,8 +23,3 @@ CREATE TABLE IF NOT EXISTS `message` (
|
||||
periodicity INTEGER NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `humeur` (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
`enable` BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
`text` VARCHAR(256) NULL
|
||||
);
|
||||
|
||||
@@ -1,34 +1,54 @@
|
||||
import random
|
||||
import discord
|
||||
# import os
|
||||
import logging
|
||||
import asyncio
|
||||
from webapp import webapp
|
||||
from database.models import Configuration, Humeur
|
||||
import datetime
|
||||
import discord
|
||||
import logging
|
||||
import random
|
||||
import requests
|
||||
|
||||
from database import db
|
||||
from database.models import Configuration, GameBundle, Humeur
|
||||
|
||||
class DiscordBot(discord.Client):
|
||||
async def on_ready(self):
|
||||
logging.info(f'Logged in as {self.user} (ID: {self.user.id})')
|
||||
for c in self.get_all_channels() :
|
||||
logging.info(f'{c.id} {c.name}')
|
||||
# for c in self.get_all_channels() :
|
||||
# print(f'{c.id} {c.name}')
|
||||
|
||||
self.loop.create_task(self.updateStatus())
|
||||
# await self.get_channel(1123512494468644984).send("essai en python")
|
||||
self.loop.create_task(self.updateHumbleBundle())
|
||||
|
||||
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)
|
||||
|
||||
async def updateHumbleBundle(self):
|
||||
response = requests.get("http://hexas.shionn.org/humble-bundle/json", headers={ "Content-Type": "application/json" })
|
||||
if response.status_code == 200:
|
||||
bundle = response.json()
|
||||
if (GameBundle.query.filter_by(id=bundle['id']).first() == None) :
|
||||
choice = bundle['choices'][0]
|
||||
date = datetime.datetime.fromtimestamp(bundle['endDate']/1000,datetime.UTC).strftime("%d %B %Y")
|
||||
message = f"@here **Humble Bundle** propose un pack de jeu [{bundle['name']}]({bundle['url']}) contenant :\n"
|
||||
for game in choice["games"]:
|
||||
message += f"- {game}\n"
|
||||
message += f"Pour {choice['price']}€, disponible jusqu'au {date}."
|
||||
# await self.get_channel(1123512494468644984).send(message)
|
||||
# db.session.add(GameBundle(id=bundle['id'], json = bundle))
|
||||
# db.session.commit()
|
||||
else:
|
||||
logging.error(f"Erreur de connexion {response.status_code}")
|
||||
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')
|
||||
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)
|
||||
|
||||
|
||||
@@ -6,4 +6,5 @@ audioop-lts; python_version>='3.13'
|
||||
|
||||
flask>=2.3.2
|
||||
flask-sqlalchemy>=3.0.3
|
||||
waitress>=3.0.2
|
||||
waitress>=3.0.2
|
||||
requests>=2.32.4
|
||||
21
run-web.py
21
run-web.py
@@ -1,25 +1,32 @@
|
||||
#
|
||||
# import discordbot
|
||||
|
||||
import multiprocessing
|
||||
import locale
|
||||
import logging
|
||||
import multiprocessing
|
||||
from webapp import webapp
|
||||
from discordbot import bot
|
||||
|
||||
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)
|
||||
# webapp.run()
|
||||
|
||||
def start_discord_bot():
|
||||
logging.info("Start Discord Bot")
|
||||
from discordbot import bot
|
||||
bot.begin()
|
||||
with webapp.app_context():
|
||||
bot.begin()
|
||||
|
||||
if __name__ == '__main__':
|
||||
locale.setlocale(locale.LC_TIME, 'fr_FR.UTF-8')
|
||||
|
||||
jobs = []
|
||||
jobs.append(multiprocessing.Process(target=start_server))
|
||||
jobs.append(multiprocessing.Process(target=start_discord_bot))
|
||||
jobs.append(multiprocessing.Process(target=start_server))
|
||||
|
||||
for job in jobs: job.start()
|
||||
|
||||
print(bot.get_all_channels())
|
||||
|
||||
for job in jobs: job.join()
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from flask import Flask
|
||||
from discordbot import bot
|
||||
|
||||
webapp = Flask(__name__)
|
||||
|
||||
|
||||
@@ -2,10 +2,20 @@ from flask import render_template, request, redirect, url_for
|
||||
from webapp import webapp
|
||||
from database import db
|
||||
from database.models import Configuration
|
||||
from discordbot import bot
|
||||
|
||||
@webapp.route("/configurations")
|
||||
def openConfigurations():
|
||||
return render_template("configurations.html")
|
||||
all = Configuration.query.all()
|
||||
configurations = {conf.key: conf for conf in all}
|
||||
|
||||
return render_template("configurations.html", configurations = configurations, channels = bot.get_all_channels())
|
||||
|
||||
@webapp.route("/updateConfiguration", methods=['POST'])
|
||||
def updateConfiguration():
|
||||
|
||||
return redirect(url_for('openConfigurations'))
|
||||
|
||||
|
||||
@webapp.route('/configurations/set/<key>', methods=['POST'])
|
||||
def setConfiguration(key):
|
||||
|
||||
@@ -3,6 +3,19 @@
|
||||
{% block content %}
|
||||
<h1>Configuration de Mamie.</h1>
|
||||
|
||||
<h2>Humble Bundle</h2>
|
||||
<form action="{{ url_for('updateConfiguration') }}" method="POST">
|
||||
<label for="humble_bundle_channel">Api Discord (cachée)</label>
|
||||
{{channels}}
|
||||
<select name="humble_bundle_channel">
|
||||
{% for channel in channels %}
|
||||
<option value="{{channel.id}}">{{channel.name}}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
<input type="Submit" value="Définir">
|
||||
</form>
|
||||
|
||||
|
||||
<h2>Api</h2>
|
||||
<form action="{{ url_for('setConfiguration', key = 'discord_token') }}" method="POST">
|
||||
<label for="value">Api Discord (cachée)</label>
|
||||
|
||||
Reference in New Issue
Block a user