2018-12-20 20:46:18 +00:00
|
|
|
import datetime, re
|
2018-12-20 16:28:14 +00:00
|
|
|
from src import ModuleManager, utils
|
|
|
|
|
2018-12-20 20:46:18 +00:00
|
|
|
RE_HUMAN_FORMAT = re.compile(r"(\d\d\d\d)-(\d?\d)-(\d?\d)")
|
|
|
|
HUMAN_FORMAT_HELP = "year-month-day (e.g. 2018-12-29)"
|
2018-12-20 16:28:14 +00:00
|
|
|
DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
|
|
|
|
|
|
|
|
class Module(ModuleManager.BaseModule):
|
|
|
|
def _now(self):
|
2018-12-20 16:29:19 +00:00
|
|
|
return datetime.datetime.utcnow()
|
2018-12-20 16:28:14 +00:00
|
|
|
def _format_datetime(self, dt: datetime.datetime):
|
|
|
|
return datetime.datetime.strftime(dt, DATETIME_FORMAT)
|
|
|
|
def _parse_datetime(self, dt: str):
|
|
|
|
return datetime.datetime.strptime(dt, DATETIME_FORMAT)
|
|
|
|
|
2018-12-20 20:26:42 +00:00
|
|
|
def _round_up_day(self, dt: datetime.datetime):
|
|
|
|
return dt.date()+datetime.timedelta(days=1)
|
2018-12-20 20:28:26 +00:00
|
|
|
def _days_since(self, now: datetime.date, dt: datetime.datetime):
|
|
|
|
return (now-dt.date()).days
|
2018-12-20 16:28:14 +00:00
|
|
|
|
|
|
|
def _get_badges(self, user):
|
|
|
|
return user.get_setting("badges", {})
|
|
|
|
def _set_badges(self, user, badges):
|
|
|
|
user.set_setting("badges", badges)
|
|
|
|
|
2018-12-20 20:30:23 +00:00
|
|
|
def _day_str(self, count: int):
|
|
|
|
return "day" + ("" if count == 1 else "s")
|
|
|
|
|
2018-12-20 21:03:21 +00:00
|
|
|
@utils.hook("received.command.badge", min_args=1)
|
|
|
|
def badge(self, event):
|
|
|
|
badge = event["args"]
|
|
|
|
badge_lower = badge.lower()
|
|
|
|
badges = self._get_badges(event["user"])
|
|
|
|
|
2018-12-20 21:04:45 +00:00
|
|
|
now = self._round_up_day(self._now())
|
|
|
|
|
2018-12-20 21:03:21 +00:00
|
|
|
found_badge = None
|
|
|
|
for badge_name in badges.keys():
|
|
|
|
if badge_name.lower() == badge_lower:
|
|
|
|
found_badge = badge_name
|
|
|
|
break
|
|
|
|
|
|
|
|
if found_badge:
|
|
|
|
days_since = self._days_since(now,
|
|
|
|
self._parse_datetime(badges[found_badge]))
|
|
|
|
event["stdout"].write("(%s) %s: %s %s" % (
|
2018-12-20 21:05:35 +00:00
|
|
|
event["user"].nickname, found_badge, days_since,
|
2018-12-20 21:03:21 +00:00
|
|
|
self._day_str(days_since)))
|
|
|
|
else:
|
|
|
|
event["stderr"].write("You have no '%s' badge" % badge)
|
|
|
|
|
2018-12-20 16:28:14 +00:00
|
|
|
@utils.hook("received.command.badges")
|
|
|
|
def badges(self, event):
|
|
|
|
user = event["user"]
|
|
|
|
if event["args"]:
|
|
|
|
user = event["server"].get_user(event["args_split"][0])
|
|
|
|
|
2018-12-20 20:26:42 +00:00
|
|
|
now = self._round_up_day(self._now())
|
2018-12-20 16:28:14 +00:00
|
|
|
badges = []
|
2018-12-20 16:31:04 +00:00
|
|
|
for badge, date in self._get_badges(user).items():
|
2018-12-20 16:32:04 +00:00
|
|
|
days_since = self._days_since(now, self._parse_datetime(date))
|
2018-12-20 20:30:23 +00:00
|
|
|
badges.append("%s: %s %s" % (
|
|
|
|
badge, days_since, self._day_str(days_since)))
|
2018-12-20 16:28:14 +00:00
|
|
|
|
|
|
|
event["stdout"].write("Badges for %s: %s" % (
|
|
|
|
user.nickname, ", ".join(badges)))
|
|
|
|
|
|
|
|
@utils.hook("received.command.addbadge", min_args=1)
|
|
|
|
def add_badge(self, event):
|
|
|
|
badge = event["args"]
|
|
|
|
badge_lower = badge.lower()
|
|
|
|
badges = self._get_badges(event["user"])
|
|
|
|
|
|
|
|
for badge_name in badges.keys():
|
|
|
|
if badge_name.lower() == badge_lower:
|
|
|
|
raise utils.EventError("You already have a '%s' badge" % badge)
|
2018-12-20 16:30:15 +00:00
|
|
|
badges[badge] = self._format_datetime(self._now())
|
2018-12-20 16:28:14 +00:00
|
|
|
self._set_badges(event["user"], badges)
|
|
|
|
event["stdout"].write("Added '%s' badge" % badge)
|
|
|
|
|
2018-12-20 20:51:03 +00:00
|
|
|
@utils.hook("received.command.removebadge", min_args=1)
|
2018-12-20 16:28:14 +00:00
|
|
|
def remove_badge(self, event):
|
|
|
|
badge = event["args"]
|
|
|
|
badge_lower = badge.lower()
|
|
|
|
badges = self._get_badges(event["user"])
|
|
|
|
|
|
|
|
found_badge = None
|
|
|
|
for badge_name in badges.keys():
|
|
|
|
if badge_name.lower() == badge_lower:
|
|
|
|
found_badge = badge_name
|
|
|
|
break
|
|
|
|
if found_badge:
|
|
|
|
del badges[found_badge]
|
|
|
|
self._set_badges(event["user"], badges)
|
2018-12-20 21:03:04 +00:00
|
|
|
event["stdout"].write("Removed '%s' badge" % found_badge)
|
2018-12-20 16:28:14 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("You have no '%s' badge" % badge)
|
|
|
|
|
2018-12-20 20:46:45 +00:00
|
|
|
@utils.hook("received.command.resetbadge", min_args=1)
|
|
|
|
def reset_badge(self, event):
|
2018-12-20 16:28:14 +00:00
|
|
|
badge = event["args"]
|
|
|
|
badge_lower = badge.lower()
|
|
|
|
badges = self._get_badges(event["user"])
|
|
|
|
|
|
|
|
found_badge = None
|
|
|
|
for badge_name in badges.keys():
|
|
|
|
if badge_name.lower() == badge_lower:
|
|
|
|
found_badge = badge_name
|
|
|
|
break
|
|
|
|
|
|
|
|
if found_badge:
|
2018-12-20 20:46:45 +00:00
|
|
|
badges[found_badge] = self._format_datetime(self._now())
|
2018-12-20 16:28:14 +00:00
|
|
|
self._set_badges(event["user"], badges)
|
2018-12-20 21:03:04 +00:00
|
|
|
event["stdout"].write("Reset badge '%s'" % found_badge)
|
2018-12-20 16:28:14 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("You have no '%s' badge" % badge)
|
2018-12-20 20:46:18 +00:00
|
|
|
|
|
|
|
@utils.hook("received.command.updatebadge", min_args=2)
|
|
|
|
def update_badge(self, event):
|
|
|
|
badge = " ".join(event["args_split"][:-1])
|
|
|
|
badge_lower = badge.lower()
|
|
|
|
badges = self._get_badges(event["user"])
|
|
|
|
|
|
|
|
found_badge = None
|
|
|
|
for badge_name in badges.keys():
|
|
|
|
if badge_name.lower() == badge_lower:
|
|
|
|
found_badge = badge_name
|
|
|
|
break
|
|
|
|
|
|
|
|
value = event["args_split"][-1]
|
|
|
|
if value.lower() == "today":
|
|
|
|
value = self._now()
|
|
|
|
else:
|
|
|
|
match = RE_HUMAN_FORMAT.match(value)
|
|
|
|
if not match:
|
|
|
|
raise utils.EventError("Invalid date format, please use %s" %
|
|
|
|
HUMAN_FORMAT_HELP)
|
|
|
|
value = datetime.datetime(
|
2018-12-20 20:48:28 +00:00
|
|
|
year=int(match.group(1)),
|
|
|
|
month=int(match.group(2)),
|
|
|
|
day=int(match.group(3))
|
|
|
|
)
|
2018-12-20 20:46:18 +00:00
|
|
|
|
|
|
|
badges[found_badge] = self._format_datetime(value)
|
|
|
|
self._set_badges(event["user"], badges)
|
2018-12-20 21:03:04 +00:00
|
|
|
event["stdout"].write("Updated '%s' badge" % found_badge)
|