From 7e33a90fb26cc6b0adc90282237c975c178f5966 Mon Sep 17 00:00:00 2001 From: Shadow15510 Date: Mon, 12 Jun 2023 00:00:53 +0200 Subject: [PATCH] Adapt parsing according to the command's type --- irc_api/api.py | 12 ++++++++---- irc_api/irc.py | 4 ++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/irc_api/api.py b/irc_api/api.py index 5dda5d3..0fca241 100644 --- a/irc_api/api.py +++ b/irc_api/api.py @@ -12,6 +12,7 @@ def command(name, desc=""): func=func, events=[lambda m: m.text.startswith(PREFIX + name)], desc=desc, + cmnd_type=1 ) return decorator @@ -22,7 +23,8 @@ def on(event, desc=""): name=func.__name__, func=func, events=[event], - desc=desc + desc=desc, + cmnd_type=0 ) return decorator @@ -37,17 +39,19 @@ def channel(channel_name, desc=""): name=func_or_cmnd.__name__, func=func_or_cmnd, events=[lambda m: m.to == channel_name], - desc=desc + desc=desc, + cmnd_type=0 ) return decorator class Command: - def __init__(self, name, func, events, desc): + def __init__(self, name, func, events, desc, cmnd_type): self.name = name self.func = func self.events = events + self.cmnd_type = cmnd_type if desc: self.desc = desc @@ -136,6 +140,6 @@ def auto_help(bot, msg, *args): bot.send(msg.to, f"Aide sur la commande : {args[0]}") bot.send(msg.to, f" {known_cmnds[args[0]].desc}") else: - bot.irc.send(msg.to, f"Aide des commandes") + bot.irc.send(msg.to, f"Liste des commandes ({PREFIX}aide pour plus d'info)") for cmnd in bot.irc.callbacks: bot.irc.send(msg.to, f" – {cmnd.name}") diff --git a/irc_api/irc.py b/irc_api/irc.py index 75dc95b..0bbb4cf 100644 --- a/irc_api/irc.py +++ b/irc_api/irc.py @@ -111,7 +111,7 @@ class IRC: for callback in self.callbacks: if not False in [event(message) for event in callback.events]: logging.info("matched %s", callback.name) - callback(message, *parse(message.text)[1:]) + callback(message, *parse(message.text)[callback.cmnd_type:]) def send(self, target: str, message: str): """Send a message to the specified target (channel or user). @@ -236,4 +236,4 @@ class Message: def parse(message): pattern = re.compile(r"(((\"|\')[\w\ \-]+(\"|\')\ *)|([\w\'\-]+\ *))") - return [match[0].strip("\" ").rstrip("\" ") for match in re.findall(pattern, message)] + return [match[0].strip("\"' ").rstrip("\"' ") for match in re.findall(pattern, message)]