Adapt parsing according to the command's type

This commit is contained in:
Shadow15510 2023-06-12 00:00:53 +02:00
parent e90c98a75f
commit 7e33a90fb2
2 changed files with 10 additions and 6 deletions

View File

@ -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 <cmnd> pour plus d'info)")
for cmnd in bot.irc.callbacks:
bot.irc.send(msg.to, f" {cmnd.name}")

View File

@ -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)]