2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on commands
|
|
|
|
|
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
|
|
|
|
|
|
|
class Module(ModuleManager.BaseModule):
|
2020-02-15 01:30:19 +00:00
|
|
|
def on_load(self):
|
|
|
|
self.exports.add("command-spec.marginstring", self._marginstring_spec)
|
|
|
|
def _marginstring_spec(self, server, channel, user, args):
|
|
|
|
if len(args) > 1:
|
|
|
|
new_args = args[:-1]
|
|
|
|
return " ".join(new_args), len(new_args)
|
2018-12-22 09:34:13 +00:00
|
|
|
else:
|
2020-02-15 01:30:19 +00:00
|
|
|
return None, 1
|
2018-12-22 00:10:44 +00:00
|
|
|
|
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)
|
2019-02-13 16:31:21 +00:00
|
|
|
def _del_badges(self, user):
|
|
|
|
user.del_setting("badges")
|
2018-12-20 16:28:14 +00:00
|
|
|
|
2020-02-15 01:30:19 +00:00
|
|
|
@utils.hook("received.command.badge")
|
|
|
|
@utils.spec("!'list ?<nickname>ouser")
|
|
|
|
@utils.spec("!'add !<name>marginstring !'now")
|
|
|
|
@utils.spec("!'add !<name>marginstring !date")
|
|
|
|
@utils.spec("!'remove !<name>string")
|
2018-12-20 21:03:21 +00:00
|
|
|
def badge(self, event):
|
2020-02-15 01:30:19 +00:00
|
|
|
if event["spec"][0] == "list":
|
|
|
|
target = event["spec"][1] or event["user"]
|
|
|
|
badges = self._get_badges(target)
|
|
|
|
if not badges:
|
|
|
|
raise utils.EventError("%s has no badges" % target.nickname)
|
|
|
|
|
|
|
|
now = self._round_up_day(utils.datetime.utcnow())
|
|
|
|
|
|
|
|
outs = []
|
|
|
|
for name in sorted(badges.keys()):
|
|
|
|
dt = utils.datetime.parse.iso8601(badges[name])
|
|
|
|
days_since = self._days_since(now, dt)
|
|
|
|
human = utils.datetime.format.date_human(dt)
|
|
|
|
outs.append("%s on day %d (%s)"
|
|
|
|
% (name, days_since, human))
|
|
|
|
event["stdout"].write("badges for %s: %s"
|
|
|
|
% (target.nickname, ", ".join(outs)))
|
2018-12-20 16:28:14 +00:00
|
|
|
|
|
|
|
else:
|
2020-02-15 01:30:19 +00:00
|
|
|
badges = self._get_badges(event["user"])
|
|
|
|
if event["spec"][0] == "add":
|
|
|
|
if event["spec"][2] == "now":
|
|
|
|
dt = utils.datetime.utcnow()
|
|
|
|
else:
|
|
|
|
dt = event["spec"][2]
|
|
|
|
badges[event["spec"][1]] = utils.datetime.format.iso8601(dt)
|
|
|
|
human = utils.datetime.format.date_human(dt)
|
|
|
|
event["stdout"].write("%s: added badge %s (%s)"
|
|
|
|
% (event["user"].nickname, event["spec"][1], human))
|
|
|
|
|
|
|
|
elif event["spec"][0] == "remove":
|
|
|
|
if not event["spec"][1] in badges:
|
|
|
|
raise utils.EventError("%s: you don't have a '%s' badge"
|
|
|
|
% (event["user"].nickname, event["spec"][1]))
|
|
|
|
|
|
|
|
human = utils.datetime.format.date_human(
|
|
|
|
utils.datetime.parse.iso8601(badges.pop(event["spec"][1])))
|
|
|
|
event["stdout"].write("%s: removed badge '%s' (%s)"
|
|
|
|
% (event["user"].nickname, event["spec"][1], human))
|
2018-12-20 16:28:14 +00:00
|
|
|
self._set_badges(event["user"], badges)
|