2019-05-05 21:54:55 +00:00
|
|
|
import datetime
|
|
|
|
from src import ModuleManager, utils
|
|
|
|
|
2019-05-06 15:26:23 +00:00
|
|
|
DATE_YEAR_FORMAT = "%Y-%m-%d"
|
|
|
|
DATE_FORMAT = "%d-%b"
|
2019-05-05 21:54:55 +00:00
|
|
|
|
|
|
|
def _parse(s):
|
2019-05-06 15:26:23 +00:00
|
|
|
if s.count("-") == 1:
|
|
|
|
try:
|
|
|
|
return False, datetime.datetime.strptime(s, DATE_FORMAT)
|
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
return True, datetime.datetime.strptime(s, DATE_YEAR_FORMAT)
|
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
def _format(years, dt):
|
|
|
|
return datetime.datetime.strftime(dt,
|
|
|
|
DATE_YEAR_FORMAT if years else DATE_FORMAT)
|
2019-05-05 21:54:55 +00:00
|
|
|
def _check(s):
|
|
|
|
parsed = _parse(s)
|
|
|
|
if parsed:
|
2019-05-06 15:26:23 +00:00
|
|
|
years, parsed = parsed
|
|
|
|
return _format(years, parsed)
|
2019-05-05 21:54:55 +00:00
|
|
|
return None
|
|
|
|
|
2019-05-06 15:26:23 +00:00
|
|
|
def _apostrophe(nickname):
|
|
|
|
if nickname[-1].lower() == "s":
|
|
|
|
return "%s'" % nickname
|
|
|
|
return "%s's" % nickname
|
|
|
|
|
2019-05-05 21:54:55 +00:00
|
|
|
@utils.export("set", {"setting": "birthday", "help": "Set your birthday",
|
2019-05-23 09:28:14 +00:00
|
|
|
"validate": _check, "example": "1995-09-15"})
|
2019-05-05 21:54:55 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
|
|
|
@utils.hook("received.command.birthday")
|
|
|
|
def birthday(self, event):
|
|
|
|
"""
|
2019-05-06 18:27:42 +00:00
|
|
|
:help: Get your, or someone else's, birthday
|
2019-05-05 21:54:55 +00:00
|
|
|
:usage: [nickname]
|
|
|
|
"""
|
|
|
|
target_user = event["user"]
|
|
|
|
if event["args"]:
|
|
|
|
target_user = event["server"].get_user(event["args_split"][0])
|
|
|
|
|
|
|
|
birthday = target_user.get_setting("birthday", None)
|
|
|
|
|
|
|
|
if not birthday == None:
|
2019-05-06 15:26:23 +00:00
|
|
|
years, birthday_parsed = _parse(birthday)
|
|
|
|
birthday_parsed = birthday_parsed.date()
|
2019-05-05 21:54:55 +00:00
|
|
|
now = datetime.datetime.utcnow().date()
|
|
|
|
|
|
|
|
next_birthday = datetime.date(year=now.year,
|
|
|
|
month=birthday_parsed.month, day=birthday_parsed.day)
|
|
|
|
if next_birthday < now:
|
|
|
|
next_birthday = next_birthday.replace(year=next_birthday.year+1)
|
|
|
|
days = (next_birthday-now).days
|
|
|
|
age = next_birthday.year-birthday_parsed.year
|
|
|
|
|
|
|
|
if days > 0:
|
2019-05-06 15:26:23 +00:00
|
|
|
if years:
|
|
|
|
event["stdout"].write("%s is %d in %d days" % (
|
|
|
|
target_user.nickname, age, days))
|
|
|
|
else:
|
|
|
|
event["stdout"].write("%s birthday is in %d days" % (
|
|
|
|
_apostrophe(target_user.nickname), days))
|
2019-05-05 21:54:55 +00:00
|
|
|
else:
|
2019-05-06 15:26:23 +00:00
|
|
|
if years:
|
|
|
|
event["stdout"].write("%s is %d today! 🎉" % (
|
|
|
|
target_user.nickname, age))
|
|
|
|
else:
|
|
|
|
event["stdout"].write("%s birthday is today! 🎉" %
|
|
|
|
_apostrophe(target_user.nickname))
|
2019-05-05 21:54:55 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("No birthday set for %s" %
|
|
|
|
target_user.nickname)
|
2019-05-06 13:54:45 +00:00
|
|
|
|
|
|
|
@utils.hook("received.command.birthdays")
|
|
|
|
def birthdays(self, event):
|
|
|
|
birthday_settings = event["server"].get_all_user_settings("birthday")
|
|
|
|
birthdays = {}
|
|
|
|
|
|
|
|
today = datetime.datetime.utcnow().date()
|
|
|
|
for nickname, birthday in birthday_settings:
|
2019-05-06 15:26:23 +00:00
|
|
|
years, birthday_parsed = _parse(birthday)
|
|
|
|
birthday_parsed = birthday_parsed.date()
|
2019-05-06 13:54:45 +00:00
|
|
|
if birthday_parsed.replace(year=today.year) == today:
|
2019-05-06 15:26:23 +00:00
|
|
|
birthdays[nickname] = [years, today.year-birthday_parsed.year]
|
2019-05-06 13:54:45 +00:00
|
|
|
if birthdays:
|
|
|
|
birthdays_str = []
|
2019-05-06 15:26:23 +00:00
|
|
|
for nickname, (years, age) in birthdays.items():
|
2019-05-06 13:54:45 +00:00
|
|
|
nickname = event["server"].get_user(nickname).nickname
|
2019-05-06 15:26:23 +00:00
|
|
|
if years:
|
|
|
|
birthdays_str.append("%s (%d)" % (nickname, age))
|
|
|
|
else:
|
|
|
|
birthdays_str.append(nickname)
|
2019-05-06 13:54:45 +00:00
|
|
|
|
|
|
|
event["stdout"].write("Birthdays today: %s" %
|
|
|
|
", ".join(birthdays_str))
|
|
|
|
else:
|
|
|
|
event["stdout"].write("There are no birthdays today")
|