nemubot/modules/birthday.py

123 lines
4.2 KiB
Python
Raw Normal View History

# coding=utf-8
2014-08-27 23:39:31 +00:00
"""People birthdays and ages"""
import re
import sys
2014-10-09 05:30:04 +00:00
from datetime import date, datetime
from nemubot.hooks import hook
from nemubot.tools.countdown import countdown_format
from nemubot.tools.date import extractDate
from nemubot.tools.xmlparser.node import ModuleState
2014-08-13 13:53:55 +00:00
nemubotversion = 3.4
from more import Response
2014-11-13 01:51:49 +00:00
def load(context):
2012-11-04 03:39:54 +00:00
global DATAS
DATAS.setIndex("name", "birthday")
2014-08-11 12:55:25 +00:00
def help_full():
2014-11-13 01:51:49 +00:00
return ("!anniv /who/: gives the remaining time before the anniversary of "
"/who/\n!age /who/: gives the age of /who/\nIf /who/ is not given,"
" gives the remaining time before your anniversary.\n\n To set you"
"r birthday, say it to nemubot :)")
2012-07-23 10:07:26 +00:00
def findName(msg):
2014-11-13 01:51:49 +00:00
if (len(msg.cmds) < 2 or msg.cmds[1].lower() == "moi" or
msg.cmds[1].lower() == "me"):
2012-11-04 03:39:54 +00:00
name = msg.nick.lower()
else:
name = msg.cmds[1].lower()
2012-07-23 10:07:26 +00:00
2012-11-04 03:39:54 +00:00
matches = []
2012-11-04 03:39:54 +00:00
if name in DATAS.index:
matches.append(name)
else:
2014-11-13 01:51:49 +00:00
for k in DATAS.index.keys():
if k.find(name) == 0:
matches.append(k)
2012-11-04 03:39:54 +00:00
return (matches, name)
2012-07-23 10:07:26 +00:00
@hook("cmd_hook", "anniv")
def cmd_anniv(msg):
(matches, name) = findName(msg)
if len(matches) == 1:
name = matches[0]
tyd = DATAS.index[name].getDate("born")
tyd = datetime(date.today().year, tyd.month, tyd.day)
if (tyd.day == datetime.today().day and
tyd.month == datetime.today().month):
return Response(countdown_format(
2014-11-13 01:51:49 +00:00
DATAS.index[name].getDate("born"), "",
"C'est aujourd'hui l'anniversaire de %s !"
" Il a %s. Joyeux anniversaire :)" % (name, "%s")),
msg.channel)
else:
if tyd < datetime.today():
tyd = datetime(date.today().year + 1, tyd.month, tyd.day)
return Response(countdown_format(tyd,
"Il reste %s avant l'anniversaire de %s !" % ("%s",
name), ""),
msg.channel)
else:
return Response("désolé, je ne connais pas la date d'anniversaire"
" de %s. Quand est-il né ?" % name,
msg.channel, msg.nick)
2014-11-13 01:51:49 +00:00
@hook("cmd_hook", "age")
def cmd_age(msg):
(matches, name) = findName(msg)
if len(matches) == 1:
name = matches[0]
d = DATAS.index[name].getDate("born")
return Response(countdown_format(d,
"%s va naître dans %s." % (name, "%s"),
"%s a %s." % (name, "%s")),
msg.channel)
else:
return Response("désolé, je ne connais pas l'âge de %s."
" Quand est-il né ?" % name, msg.channel, msg.nick)
return True
2014-11-13 01:51:49 +00:00
@hook("ask_default")
def parseask(msg):
res = re.match(r"^(\S+)\s*('s|suis|est|is|was|were)?\s+(birthday|geburtstag|née? |nee? le|born on).*$", msg.text, re.I)
2014-08-21 13:16:25 +00:00
if res is not None:
2012-11-04 03:39:54 +00:00
try:
extDate = extractDate(msg.text)
2012-11-04 03:39:54 +00:00
if extDate is None or extDate.year > datetime.now().year:
return Response("la date de naissance ne paraît pas valide...",
2012-11-04 03:39:54 +00:00
msg.channel,
msg.nick)
else:
2014-08-21 13:16:25 +00:00
nick = res.group(1)
if nick == "my" or nick == "I" or nick == "i" or nick == "je" or nick == "mon" or nick == "ma":
nick = msg.nick
if nick.lower() in DATAS.index:
DATAS.index[nick.lower()]["born"] = extDate
2012-11-04 03:39:54 +00:00
else:
ms = ModuleState("birthday")
2014-08-21 13:16:25 +00:00
ms.setAttribute("name", nick.lower())
2012-11-04 03:39:54 +00:00
ms.setAttribute("born", extDate)
DATAS.addChild(ms)
save()
return Response("ok, c'est noté, %s est né le %s"
2014-08-21 13:16:25 +00:00
% (nick, extDate.strftime("%A %d %B %Y à %H:%M")),
2012-11-04 03:39:54 +00:00
msg.channel,
msg.nick)
except:
2014-08-21 13:16:25 +00:00
raise IRCException("la date de naissance ne paraît pas valide.")