Avoid catchall DirectAsk

This commit is contained in:
nemunaire 2015-11-14 16:17:25 +01:00
parent 7ae7e381c3
commit e83c4091bf
2 changed files with 60 additions and 50 deletions

View File

@ -14,9 +14,11 @@ from nemubot.tools.xmlparser.node import ModuleState
from more import Response from more import Response
def help_full (): def help_full ():
return "This module store a lot of events: ny, we, " + (", ".join(context.datas.index.keys())) + "\n!eventslist: gets list of timer\n!start /something/: launch a timer" return "This module store a lot of events: ny, we, " + (", ".join(context.datas.index.keys())) + "\n!eventslist: gets list of timer\n!start /something/: launch a timer"
def load(context): def load(context):
#Define the index #Define the index
context.data.setIndex("name") context.data.setIndex("name")
@ -35,6 +37,7 @@ def fini(d, strend):
context.data.delChild(context.data.index[strend["name"]]) context.data.delChild(context.data.index[strend["name"]])
context.save() context.save()
@hook.command("goûter") @hook.command("goûter")
def cmd_gouter(msg): def cmd_gouter(msg):
ndate = datetime.now(timezone.utc) ndate = datetime.now(timezone.utc)
@ -44,6 +47,7 @@ def cmd_gouter(msg):
"Nous avons %s de retard pour le goûter :("), "Nous avons %s de retard pour le goûter :("),
channel=msg.channel) channel=msg.channel)
@hook.command("week-end") @hook.command("week-end")
def cmd_we(msg): def cmd_we(msg):
ndate = datetime.now(timezone.utc) + timedelta(5 - datetime.today().weekday()) ndate = datetime.now(timezone.utc) + timedelta(5 - datetime.today().weekday())
@ -53,6 +57,7 @@ def cmd_we(msg):
"Youhou, on est en week-end depuis %s."), "Youhou, on est en week-end depuis %s."),
channel=msg.channel) channel=msg.channel)
@hook.command("start") @hook.command("start")
def start_countdown(msg): def start_countdown(msg):
"""!start /something/: launch a timer""" """!start /something/: launch a timer"""
@ -132,6 +137,7 @@ def start_countdown(msg):
msg.date.strftime("%A %d %B %Y à %H:%M:%S")), msg.date.strftime("%A %d %B %Y à %H:%M:%S")),
nick=msg.frm) nick=msg.frm)
@hook.command("end") @hook.command("end")
@hook.command("forceend") @hook.command("forceend")
def end_countdown(msg): def end_countdown(msg):
@ -151,6 +157,7 @@ def end_countdown(msg):
else: else:
return Response("%s n'est pas un compteur connu."% (msg.args[0]), channel=msg.channel, nick=msg.nick) return Response("%s n'est pas un compteur connu."% (msg.args[0]), channel=msg.channel, nick=msg.nick)
@hook.command("eventslist") @hook.command("eventslist")
def liste(msg): def liste(msg):
"""!eventslist: gets list of timer""" """!eventslist: gets list of timer"""
@ -166,6 +173,7 @@ def liste(msg):
else: else:
return Response("Compteurs connus : %s." % ", ".join(context.data.index.keys()), channel=msg.channel) return Response("Compteurs connus : %s." % ", ".join(context.data.index.keys()), channel=msg.channel)
@hook.command(match=lambda msg: isinstance(msg, Command) and msg.cmd in context.data.index) @hook.command(match=lambda msg: isinstance(msg, Command) and msg.cmd in context.data.index)
def parseanswer(msg): def parseanswer(msg):
res = Response(channel=msg.channel) res = Response(channel=msg.channel)
@ -183,59 +191,59 @@ def parseanswer(msg):
res.append_message(countdown_format(context.data.index[msg.cmd].getDate("start"), context.data.index[msg.cmd]["msg_before"], context.data.index[msg.cmd]["msg_after"])) res.append_message(countdown_format(context.data.index[msg.cmd].getDate("start"), context.data.index[msg.cmd]["msg_before"], context.data.index[msg.cmd]["msg_after"]))
return res return res
RGXP_ask = re.compile(r"^.*((create|new)\s+(a|an|a\s*new|an\s*other)?\s*(events?|commande?)|(nouvel(le)?|ajoute|cr[ée]{1,3})\s+(un)?\s*([eé]v[ée]nements?|commande?)).*$", re.I) RGXP_ask = re.compile(r"^.*((create|new)\s+(a|an|a\s*new|an\s*other)?\s*(events?|commande?)|(nouvel(le)?|ajoute|cr[ée]{1,3})\s+(un)?\s*([eé]v[ée]nements?|commande?)).*$", re.I)
@hook.ask() @hook.ask(match=lambda msg: RGXP_ask.match(msg.text))
def parseask(msg): def parseask(msg):
if RGXP_ask.match(msg.text) is not None: name = re.match("^.*!([^ \"'@!]+).*$", msg.text)
name = re.match("^.*!([^ \"'@!]+).*$", msg.text) if name is None:
if name is None: raise IMException("il faut que tu attribues une commande à l'événement.")
raise IMException("il faut que tu attribues une commande à l'événement.") if name.group(1) in context.data.index:
if name.group(1) in context.data.index: raise IMException("un événement portant ce nom existe déjà.")
raise IMException("un événement portant ce nom existe déjà.")
texts = re.match("^[^\"]*(avant|après|apres|before|after)?[^\"]*\"([^\"]+)\"[^\"]*((avant|après|apres|before|after)?.*\"([^\"]+)\".*)?$", msg.text, re.I) texts = re.match("^[^\"]*(avant|après|apres|before|after)?[^\"]*\"([^\"]+)\"[^\"]*((avant|après|apres|before|after)?.*\"([^\"]+)\".*)?$", msg.text, re.I)
if texts is not None and texts.group(3) is not None: if texts is not None and texts.group(3) is not None:
extDate = extractDate(msg.text) extDate = extractDate(msg.text)
if extDate is None or extDate == "": if extDate is None or extDate == "":
raise IMException("la date de l'événement est invalide !") raise IMException("la date de l'événement est invalide !")
if texts.group(1) is not None and (texts.group(1) == "après" or texts.group(1) == "apres" or texts.group(1) == "after"): if texts.group(1) is not None and (texts.group(1) == "après" or texts.group(1) == "apres" or texts.group(1) == "after"):
msg_after = texts.group (2) msg_after = texts.group(2)
msg_before = texts.group (5) msg_before = texts.group(5)
if (texts.group(4) is not None and (texts.group(4) == "après" or texts.group(4) == "apres" or texts.group(4) == "after")) or texts.group(1) is None: if (texts.group(4) is not None and (texts.group(4) == "après" or texts.group(4) == "apres" or texts.group(4) == "after")) or texts.group(1) is None:
msg_before = texts.group (2) msg_before = texts.group(2)
msg_after = texts.group (5) msg_after = texts.group(5)
if msg_before.find("%s") == -1 or msg_after.find("%s") == -1: if msg_before.find("%s") == -1 or msg_after.find("%s") == -1:
raise IMException("Pour que l'événement soit valide, ajouter %s à" raise IMException("Pour que l'événement soit valide, ajouter %s à"
" l'endroit où vous voulez que soit ajouté le" " l'endroit où vous voulez que soit ajouté le"
" compte à rebours.") " compte à rebours.")
evt = ModuleState("event") evt = ModuleState("event")
evt["server"] = msg.server evt["server"] = msg.server
evt["channel"] = msg.channel evt["channel"] = msg.channel
evt["proprio"] = msg.nick evt["proprio"] = msg.nick
evt["name"] = name.group(1) evt["name"] = name.group(1)
evt["start"] = extDate evt["start"] = extDate
evt["msg_after"] = msg_after evt["msg_after"] = msg_after
evt["msg_before"] = msg_before evt["msg_before"] = msg_before
context.data.addChild(evt) context.data.addChild(evt)
context.save() context.save()
return Response("Nouvel événement !%s ajouté avec succès." % name.group(1), return Response("Nouvel événement !%s ajouté avec succès." % name.group(1),
channel=msg.channel) channel=msg.channel)
elif texts is not None and texts.group (2) is not None: elif texts is not None and texts.group(2) is not None:
evt = ModuleState("event") evt = ModuleState("event")
evt["server"] = msg.server evt["server"] = msg.server
evt["channel"] = msg.channel evt["channel"] = msg.channel
evt["proprio"] = msg.nick evt["proprio"] = msg.nick
evt["name"] = name.group(1) evt["name"] = name.group(1)
evt["msg_before"] = texts.group (2) evt["msg_before"] = texts.group (2)
context.data.addChild(evt) context.data.addChild(evt)
context.save() context.save()
return Response("Nouvelle commande !%s ajoutée avec succès." % name.group(1), return Response("Nouvelle commande !%s ajoutée avec succès." % name.group(1),
channel=msg.channel) channel=msg.channel)
else: else:
raise IMException("Veuillez indiquez les messages d'attente et d'après événement entre guillemets.") raise IMException("Veuillez indiquez les messages d'attente et d'après événement entre guillemets.")

View File

@ -72,9 +72,11 @@ class Bot(threading.Thread):
import re import re
def in_ping(msg): def in_ping(msg):
if re.match("^ *(m[' ]?entends?[ -]+tu|h?ear me|do you copy|ping)", msg.message, re.I) is not None: return msg.respond("pong")
return msg.respond("pong") self.treater.hm.add_hook(nemubot.hooks.Message(in_ping,
self.treater.hm.add_hook(nemubot.hooks.Message(in_ping), "in", "DirectAsk") match=lambda msg: re.match("^ *(m[' ]?entends?[ -]+tu|h?ear me|do you copy|ping)",
msg.message, re.I)),
"in", "DirectAsk")
def in_echo(msg): def in_echo(msg):
from nemubot.message import Text from nemubot.message import Text