From c00e0b491d047e84d21ed7047f1d4966fecb4beb Mon Sep 17 00:00:00 2001 From: Kepka Ludovic Date: Mon, 11 Aug 2025 13:28:20 +0200 Subject: [PATCH] notification humble bundle --- discordbot/__init__.py | 51 +++++++++++++++++++++++----------------- webapp/configurations.py | 2 -- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/discordbot/__init__.py b/discordbot/__init__.py index ff5d9ad..a5799c8 100644 --- a/discordbot/__init__.py +++ b/discordbot/__init__.py @@ -1,11 +1,13 @@ import asyncio import datetime import discord +import json import logging import random import requests from database import db +from database.helpers import ConfigurationHelper from database.models import Configuration, GameBundle, Humeur class DiscordBot(discord.Client): @@ -18,29 +20,36 @@ class DiscordBot(discord.Client): self.loop.create_task(self.updateHumbleBundle()) async def updateStatus(self): - 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) + while not self.is_closed(): + 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)) + # 10 minutes TODO à rendre configurable + await asyncio.sleep(10*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) + while not self.is_closed(): + if ConfigurationHelper().getValue('humble_bundle_enable') and ConfigurationHelper().getIntValue('humble_bundle_channel') != 0 : + 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(ConfigurationHelper().getIntValue('humble_bundle_channel')).send(message) + db.session.add(GameBundle(id=bundle['id'], name=bundle['name'], json = json.dumps(bundle))) + db.session.commit() + else: + logging.error(f"Erreur de connexion {response.status_code}") + else: + logging.info('Humble bundle est désactivé') + # toute les 30 minutes + await asyncio.sleep(30*60) def begin(self) : token = Configuration.query.filter_by(key='discord_token').first() diff --git a/webapp/configurations.py b/webapp/configurations.py index 17817f3..e629e69 100644 --- a/webapp/configurations.py +++ b/webapp/configurations.py @@ -1,14 +1,12 @@ from flask import render_template, request, redirect, url_for from webapp import webapp from database import db -from database.models import Configuration from database.helpers import ConfigurationHelper from discordbot import bot from discord import TextChannel @webapp.route("/configurations") def openConfigurations(): - all = Configuration.query.all() channels = [] for channel in bot.get_all_channels(): if isinstance(channel, TextChannel):