From 95c007c1dad69264d515831bf18a57b3a5417eed Mon Sep 17 00:00:00 2001 From: jesopo Date: Sun, 8 Sep 2019 18:17:32 +0100 Subject: [PATCH] support showing time for locations, not just users --- modules/user_time.py | 53 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/modules/user_time.py b/modules/user_time.py index fd8c42d7..065d971d 100644 --- a/modules/user_time.py +++ b/modules/user_time.py @@ -1,11 +1,16 @@ #--depends-on commands #--depends-on location -import datetime +import datetime, enum import pytz from src import ModuleManager, utils -NOLOCATION = "%s doesn't have a location set" +NOLOCATION_USER = "%s doesn't have a location set" +NOLOCATION_NAME = "Unknown location '%s'" + +class LocationType(enum.Enum): + USER = 1 + NAME = 2 class Module(ModuleManager.BaseModule): _name = "Time" @@ -14,9 +19,25 @@ class Module(ModuleManager.BaseModule): target_user = event["user"] if event["args"]: - target_user = event["server"].get_user(event["args_split"][0]) + if len(event["args_split"]) == 1 and event["server"].has_user_id( + event["args_split"][0]): + target_user = event["server"].get_user(event["args_split"][0]) + else: + location = self.exports.get_one("get-location")(event["args"]) + if location: + return (LocationType.NAME, location["name"], + location["timezone"]) + else: + return LocationType.NAME, event["args"], None + + if target_user: + location = target_user.get_setting("location", None) + if location: + return (LocationType.USER, target_user.nickname, + location["timezone"]) + else: + return LocationType.USER, target_user.nickname, None - return target_user, target_user.get_setting("location", None) @utils.hook("received.command.time") @utils.kwarg("help", "Get the time for you or someone else") @@ -24,14 +45,26 @@ class Module(ModuleManager.BaseModule): @utils.kwarg("require_setting", "location") @utils.kwarg("require_setting_unless", "1") def time(self, event): - target_user, location = self._find_setting(event) - if not location == None: - dt = datetime.datetime.now(tz=pytz.timezone(location["timezone"])) + type, name, timezone = self._find_setting(event) + + if not timezone == None: + dt = datetime.datetime.now(tz=pytz.timezone(timezone)) human = utils.datetime_human(dt) - event["stdout"].write("Time for %s: %s" % (target_user.nickname, - human)) + + out = None + if type == LocationType.USER: + out = "Time for %s: %s" % (name, human) + else: + out = "It is %s in %s" % (human, name) + event["stdout"].write(out) else: - event["stderr"].write(NOLOCATION % target_user.nickname) + out = None + if type == LocationType.USER: + out = NOLOCATION_USER + else: + out = NOLOCATION_NAME + + event["stderr"].write(out % name) @utils.hook("received.command.timezone") @utils.kwarg("help", "Get the timezone for you or someone else")