Add !updatebadge to manually set a date on a badge (badges.py)

This commit is contained in:
jesopo 2018-12-20 20:46:18 +00:00
parent 03a3bdd73d
commit dbee856c4d

View file

@ -1,6 +1,8 @@
import datetime import datetime, re
from src import ModuleManager, utils from src import ModuleManager, utils
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)"
DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ" DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
@ -87,3 +89,29 @@ class Module(ModuleManager.BaseModule):
event["stdout"].write("Reset badge '%s'" % badge) event["stdout"].write("Reset badge '%s'" % badge)
else: else:
event["stderr"].write("You have no '%s' badge" % badge) event["stderr"].write("You have no '%s' badge" % badge)
@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(
year=match.group(1), month=match.group(2), day=match.group(3))
badges[found_badge] = self._format_datetime(value)
self._set_badges(event["user"], badges)