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 = (
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 settings
SCHEDULER_DELAY = 60
EXECUTE_EVERY = 1
EXECUTE_AT = 2
EXECUTE_AFTER = 3
@dataclass
class Event:
def __init__(self):
self.scheduler = None
self.callback = None
self.event_id = 1
self.last_update = time.gmtime()
callback : str = ""
scheduler_type : int = 1
scheduler_delay : int = 60
def init_scheduler(self, scheduler, callback):
def init(self, scheduler, callback_func):
self.scheduler = scheduler
self.callback = callback
self.scheduler.execute_every(SCHEDULER_DELAY, self.do_something)
self.callback_func = callback_func
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):
now = time.localtime()
# Gestion du rappel avant la revue hebdomadaire
for reminder in settings.CRON_REVIEW_REMINDERS:
if now.tm_hour == reminder[0] and now.tm_min == reminder[1]:
# 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)
def _run(self):
self.run(time.localtime())
def run(self, now):
pass

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()
for event in events:
event.init_scheduler(self.reactor.scheduler, self.callback)
event.init(self.reactor.scheduler, getattr(self, event.callback))
logger.info(
"--==============================INIT===="
@ -28,17 +28,16 @@ class HebdoBot(IrcBot):
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.
"""
self.send(self.channel, f"Revue hebdomadaire dans {minutes} minutes.")
def mastodon(self):
"""
Envoi d'une notification sur Mastodon.
"""
if minutes == 15:
# Envoi sur IRC
self.send(
self.channel,
f"Plus que {minutes} minutes avant le début de la revue hebdomdaire.",
)
# Envoi sur Mastodon
# TODO : Gérer mastodon
# ~ mastodon.send(
# ~ "Revue hebdomadaire April à 12h sur notre salon : "
@ -47,6 +46,7 @@ class HebdoBot(IrcBot):
# ~ "et à venir.\nVous pouvez aussi juste nous faire un coucou. Et ça met "
# ~ "en appétit :)",
# ~ )
pass
def on_privmsg(self, connection, event):
"""
@ -73,6 +73,12 @@ class HebdoBot(IrcBot):
if hook.process(self, channel, sender, message):
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):
if self.review.is_started and target == self.channel:
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"))
# Review
REVIEW_HOUR = (16, 30)
REVIEW_HOUR = (12, 00)
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"))
@ -31,9 +31,10 @@ PASTEBIN_URL = "https://paste.chapril.org/"
# Values : 5min, 10min, 1hour, 1day, 1week, 1month, 1year, never
PASTEBIN_EXPIRATION = "1week"
# Cron
CRON_REVIEW_REMINDERS = [(11, 45)]
CRON_STOP_BOT = [(16, 15)]
# Scheduler
REVIEW_REMINDERS = [(11, 30), (11, 45), (11, 55)]
REVIEW_MASTODON = (11, 45)
STOP_BOT = (14, 00)
# Mastodon
MASTODON_SERVER = ""