support showing time for locations, not just users

This commit is contained in:
jesopo 2019-09-08 18:17:32 +01:00
parent 896b98bbd1
commit 95c007c1da

View file

@ -1,11 +1,16 @@
#--depends-on commands #--depends-on commands
#--depends-on location #--depends-on location
import datetime import datetime, enum
import pytz import pytz
from src import ModuleManager, utils 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): class Module(ModuleManager.BaseModule):
_name = "Time" _name = "Time"
@ -14,9 +19,25 @@ class Module(ModuleManager.BaseModule):
target_user = event["user"] target_user = event["user"]
if event["args"]: 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.hook("received.command.time")
@utils.kwarg("help", "Get the time for you or someone else") @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", "location")
@utils.kwarg("require_setting_unless", "1") @utils.kwarg("require_setting_unless", "1")
def time(self, event): def time(self, event):
target_user, location = self._find_setting(event) type, name, timezone = self._find_setting(event)
if not location == None:
dt = datetime.datetime.now(tz=pytz.timezone(location["timezone"])) if not timezone == None:
dt = datetime.datetime.now(tz=pytz.timezone(timezone))
human = utils.datetime_human(dt) 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: 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.hook("received.command.timezone")
@utils.kwarg("help", "Get the timezone for you or someone else") @utils.kwarg("help", "Get the timezone for you or someone else")