1
0
Fork 0
nemubot/modules/bonneannee.py

75 lines
2.8 KiB
Python
Raw Normal View History

2014-08-27 23:39:31 +00:00
"""Wishes Happy New Year when the time comes"""
2015-10-05 21:54:38 +00:00
# PYTHON STUFFS #######################################################
2014-09-30 21:51:14 +00:00
from datetime import datetime, timezone
2012-12-31 19:19:07 +00:00
from nemubot.event import ModuleEvent
from nemubot.hooks import hook
from nemubot.tools.countdown import countdown_format
from nemubot.module.more import Response
2015-10-05 21:54:38 +00:00
# GLOBALS #############################################################
2014-09-30 21:51:14 +00:00
yr = datetime.now(timezone.utc).year
yrn = datetime.now(timezone.utc).year + 1
2012-12-31 19:19:07 +00:00
2015-10-05 21:54:38 +00:00
# LOADING #############################################################
def load(context):
2015-02-11 17:12:39 +00:00
if not context.config or not context.config.hasNode("sayon"):
print("You can append in your configuration some balise to "
"automaticaly wish an happy new year on some channels like:\n"
"<sayon hostid=\"nemubot@irc.freenode.net:6667\" "
"channel=\"#nemutest\" />")
def bonneannee():
txt = "Bonne année %d !" % yrn
print(txt)
2015-02-11 17:12:39 +00:00
if context.config and context.config.hasNode("sayon"):
for sayon in context.config.getNodes("sayon"):
if "hostid" not in sayon or "channel" not in sayon:
print("Error: missing hostif or channel")
continue
srv = sayon["hostid"]
chan = sayon["channel"]
2015-02-11 17:12:39 +00:00
context.send_response(srv, Response(txt, chan))
d = datetime(yrn, 1, 1, 0, 0, 0, 0,
timezone.utc) - datetime.now(timezone.utc)
2015-02-11 17:12:39 +00:00
context.add_event(ModuleEvent(interval=0, offset=d.total_seconds(),
call=bonneannee))
2012-12-31 19:19:07 +00:00
2015-10-05 21:54:38 +00:00
# MODULE INTERFACE ####################################################
2015-11-02 19:19:12 +00:00
@hook.command("newyear",
2015-10-05 21:54:38 +00:00
help="Display the remaining time before the next new year")
2015-11-02 19:19:12 +00:00
@hook.command(str(yrn),
2015-10-05 21:54:38 +00:00
help="Display the remaining time before %d" % yrn)
def cmd_newyear(msg):
return Response(countdown_format(datetime(yrn, 1, 1, 0, 0, 1, 0,
timezone.utc),
"Il reste %s avant la nouvelle année.",
"Nous faisons déjà la fête depuis %s !"),
2012-12-31 19:19:07 +00:00
channel=msg.channel)
2015-11-02 19:19:12 +00:00
@hook.command(data=yrn, regexp="^[0-9]{4}$",
2015-10-05 21:54:38 +00:00
help="Calculate time remaining/passed before/since the requested year")
2012-12-31 19:19:07 +00:00
def cmd_timetoyear(msg, cur):
2015-07-07 10:34:00 +00:00
yr = int(msg.cmd)
2012-12-31 19:19:07 +00:00
if yr == cur:
return None
return Response(countdown_format(datetime(yr, 1, 1, 0, 0, 1, 0,
timezone.utc),
"Il reste %s avant %d." % ("%s", yr),
"Le premier janvier %d est passé "
"depuis %s !" % (yr, "%s")),
2012-12-31 19:19:07 +00:00
channel=msg.channel)