Reprise de la gestion des événements

This commit is contained in:
Mindiell 2024-04-06 22:27:24 +02:00
parent 950c92c48f
commit fdf37cddc2
6 changed files with 105 additions and 45 deletions

View File

@ -1,5 +1,8 @@
from .event import Event from .notification import Notification
from .mastodon import Mastodon
events = ( events = (
Event(), Notification(),
Mastodon(),
) )

View File

@ -1,31 +1,32 @@
""" """
Script used to... Script used to send notifications for review on IRC.
""" """
from dataclasses import dataclass
import time import time
import settings EXECUTE_EVERY = 1
EXECUTE_AT = 2
SCHEDULER_DELAY = 60 EXECUTE_AFTER = 3
@dataclass
class Event: class Event:
def __init__(self): callback : str = ""
self.scheduler = None scheduler_type : int = 1
self.callback = None scheduler_delay : int = 60
self.event_id = 1
self.last_update = time.gmtime()
def init_scheduler(self, scheduler, callback): def init(self, scheduler, callback_func):
self.scheduler = scheduler self.scheduler = scheduler
self.callback = callback self.callback_func = callback_func
self.scheduler.execute_every(SCHEDULER_DELAY, self.do_something) if self.scheduler_type == EXECUTE_EVERY:
self.scheduler.execute_every(self.scheduler_delay, self._run)
elif self.scheduler_type == EXECUTE_AT:
self.scheduler.execute_at(self.scheduler_delay, self._run)
elif self.scheduler_type == EXECUTE_AFTER:
self.scheduler.execute_after(self.scheduler_delay, self._run)
def do_something(self): def _run(self):
now = time.localtime() self.run(time.localtime())
# Gestion du rappel avant la revue hebdomadaire
for reminder in settings.CRON_REVIEW_REMINDERS: def run(self, now):
if now.tm_hour == reminder[0] and now.tm_min == reminder[1]: pass
# On calcule le nombre de minutes avant la revue hebdomadaire
minutes = (settings.REVIEW_HOUR[0] - now.tm_hour) * 60
minutes += settings.REVIEW_HOUR[1] - now.tm_min
self.callback(minutes)

22
events/mastodon.py Normal file
View File

@ -0,0 +1,22 @@
"""
Script used to send message for review on Mastodon.
"""
from dataclasses import dataclass
from events import event
import settings
@dataclass
class Mastodon(event.Event):
callback : str = "mastodon"
def run(self, now):
"""
Gestion du message avant la revue hebdomadaire
"""
if (
now.tm_hour == settings.REVIEW_MASTODON[0]
and now.tm_min == settings.REVIEW_MASTODON[1]
):
self.callback_func()

27
events/notification.py Normal file
View File

@ -0,0 +1,27 @@
"""
Script used to send notifications for review on IRC.
"""
from dataclasses import dataclass
from events import event
import settings
SCHEDULER_DELAY = 60
@dataclass
class Notification(event.Event):
callback : str = "notifications"
scheduler_type : int = event.EXECUTE_EVERY
scheduler_delay : int = SCHEDULER_DELAY
def run(self, now):
"""
Gestion du rappel avant la revue hebdomadaire
"""
for reminder in settings.REVIEW_REMINDERS:
if now.tm_hour == reminder[0] and now.tm_min == reminder[1]:
# On renvoie le nombre de minutes avant la revue hebdomadaire
minutes = (settings.REVIEW_HOUR[0] - now.tm_hour) * 60
minutes += settings.REVIEW_HOUR[1] - now.tm_min
self.callback_func(minutes)

View File

@ -19,7 +19,7 @@ class HebdoBot(IrcBot):
self.review = Review() self.review = Review()
for event in events: for event in events:
event.init_scheduler(self.reactor.scheduler, self.callback) event.init(self.reactor.scheduler, getattr(self, event.callback))
logger.info( logger.info(
"--==============================INIT====" "--==============================INIT===="
@ -28,25 +28,25 @@ class HebdoBot(IrcBot):
logger.info(f"Hebdobot {self.VERSION}") logger.info(f"Hebdobot {self.VERSION}")
def callback(self, minutes): def notifications(self, minutes):
""" """
Envoi des notification sur IRC et les réseaux sociaux. Envoi des notifications sur IRC.
""" """
if minutes == 15: self.send(self.channel, f"Revue hebdomadaire dans {minutes} minutes.")
# Envoi sur IRC
self.send( def mastodon(self):
self.channel, """
f"Plus que {minutes} minutes avant le début de la revue hebdomdaire.", Envoi d'une notification sur Mastodon.
) """
# Envoi sur Mastodon # TODO : Gérer mastodon
# TODO : Gérer mastodon # ~ mastodon.send(
# ~ mastodon.send( # ~ "Revue hebdomadaire April à 12h sur notre salon : "
# ~ "Revue hebdomadaire April à 12h sur notre salon : " # ~ "http://april.org/salon-irc-de-lapril\nCette revue ne dure qu'une "
# ~ "http://april.org/salon-irc-de-lapril\nCette revue ne dure qu'une " # ~ "quinzaine de minutes et permet d'avoir une idée des actions en cours "
# ~ "quinzaine de minutes et permet d'avoir une idée des actions en cours " # ~ "et à venir.\nVous pouvez aussi juste nous faire un coucou. Et ça met "
# ~ "et à venir.\nVous pouvez aussi juste nous faire un coucou. Et ça met " # ~ "en appétit :)",
# ~ "en appétit :)", # ~ )
# ~ ) pass
def on_privmsg(self, connection, event): def on_privmsg(self, connection, event):
""" """
@ -73,6 +73,12 @@ class HebdoBot(IrcBot):
if hook.process(self, channel, sender, message): if hook.process(self, channel, sender, message):
break break
def on_join(self, connection, event):
"""
Lorsqu'il se connecte, le bot dit bonjour.
"""
self.send(event.target, "Bonjour tout le monde, je suis là :)")
def send(self, target, message): def send(self, target, message):
if self.review.is_started and target == self.channel: if self.review.is_started and target == self.channel:
self.review.add_message(self.nickname, message) self.review.add_message(self.nickname, message)

View File

@ -22,7 +22,7 @@ IRC_DELAY = float(os.environ.get("IRC_DELAY", 0.5)) # Délai entre plusieurs mes
USER_ALIASES = os.environ.get("USER_ALIASES", os.path.join(BASE_DIR, "users.conf")) USER_ALIASES = os.environ.get("USER_ALIASES", os.path.join(BASE_DIR, "users.conf"))
# Review # Review
REVIEW_HOUR = (16, 30) REVIEW_HOUR = (12, 00)
REVIEW_DIRECTORY = os.environ.get("REVIEW_DIRECTORY", os.path.join(BASE_DIR, "reviews")) REVIEW_DIRECTORY = os.environ.get("REVIEW_DIRECTORY", os.path.join(BASE_DIR, "reviews"))
REVIEW_STATS = os.environ.get("REVIEW_STATS", os.path.join(REVIEW_DIRECTORY, "reviewstats.csv")) REVIEW_STATS = os.environ.get("REVIEW_STATS", os.path.join(REVIEW_DIRECTORY, "reviewstats.csv"))
@ -31,9 +31,10 @@ PASTEBIN_URL = "https://paste.chapril.org/"
# Values : 5min, 10min, 1hour, 1day, 1week, 1month, 1year, never # Values : 5min, 10min, 1hour, 1day, 1week, 1month, 1year, never
PASTEBIN_EXPIRATION = "1week" PASTEBIN_EXPIRATION = "1week"
# Cron # Scheduler
CRON_REVIEW_REMINDERS = [(11, 45)] REVIEW_REMINDERS = [(11, 30), (11, 45), (11, 55)]
CRON_STOP_BOT = [(16, 15)] REVIEW_MASTODON = (11, 45)
STOP_BOT = (14, 00)
# Mastodon # Mastodon
MASTODON_SERVER = "" MASTODON_SERVER = ""