diff --git a/glados_cmnds.py b/glados_cmnds.py index 3437b24..6eba275 100644 --- a/glados_cmnds.py +++ b/glados_cmnds.py @@ -55,3 +55,7 @@ def wiki(bot, msg, text: str, limit: int=1): bot.send(msg.to, f"{len(response[1])} résultat{('', 's')[limit > 1]} pour la recherche : '{text}'.") for name, link in zip(response[1], response[3]): bot.send(msg.to, f" {name} : {link}") + +@api.every(5) +def foo(bot): + bot.send("#glados", "foo called") diff --git a/irc_api/api.py b/irc_api/api.py index 081e758..14bec20 100644 --- a/irc_api/api.py +++ b/irc_api/api.py @@ -1,6 +1,9 @@ import logging import re +from functools import wraps from irc_api.irc import IRC, History +from threading import Thread +import time PREFIX = "" @@ -66,6 +69,19 @@ def user(user_name, desc=""): return decorator +def every(time, desc=""): + def decorator(func): + return Command( + name=func.__name__, + func=func, + events=time, + desc=desc, + cmnd_type=2 + ) + + return decorator + + class Command: def __init__(self, name, func, events, desc, cmnd_type): self.name = name @@ -133,6 +149,7 @@ class Bot: self.auth = auth self.callbacks = {} self.commands_help = {} + self.threads = [] if commands_modules: self.add_commands_modules(*commands_modules) @@ -159,8 +176,9 @@ class Bot: # @api.on if callback.cmnd_type == 0: callback(message) + # @api.command - if callback.cmnd_type == 1: + elif callback.cmnd_type == 1: args = check_args(callback.func, *parse(message.text)[1:]) if isinstance(args, list): callback(message, *args) @@ -170,7 +188,6 @@ class Bot: "Erreur : les arguments donnés ne correspondent pas." ) - def send(self, target: str, message: str): """Send a message to the specified target (channel or user). @@ -186,7 +203,18 @@ class Bot: def add_command(self, command, add_to_help=False): command.bot = self - self.callbacks[command.name] = command + + if command.cmnd_type == 2: + def timed_func(bot): + while True: + command.func(bot) + time.sleep(command.events) + + self.threads.append(Thread(target=timed_func, args=(self,))) + self.threads[-1].start() + else: + self.callbacks[command.name] = command + if add_to_help: self.commands_help[command.name] = command