Add system time zone designation. Closes #21.

This commit is contained in:
Ske
2018-12-18 19:38:53 +01:00
parent e8d1c5bf90
commit 570899928a
8 changed files with 89 additions and 13 deletions

View File

@@ -1,4 +1,5 @@
import asyncio
from datetime import datetime
import discord
import re
@@ -37,14 +38,15 @@ class CommandError(Exception):
class CommandContext:
def __init__(self, client: discord.Client, message: discord.Message, conn, args: str):
def __init__(self, client: discord.Client, message: discord.Message, conn, args: str, system: Optional[System]):
self.client = client
self.message = message
self.conn = conn
self.args = args
self._system = system
async def get_system(self) -> Optional[System]:
return await db.get_system_by_account(self.conn, self.message.author.id)
return self._system
async def ensure_system(self) -> System:
system = await self.get_system()
@@ -57,6 +59,11 @@ class CommandContext:
def has_next(self) -> bool:
return bool(self.args)
def format_time(self, dt: datetime):
if self._system:
return self._system.format_time(dt)
return dt.isoformat(sep=" ", timespec="seconds") + " UTC"
def pop_str(self, error: CommandError = None) -> Optional[str]:
if not self.args:
if error:
@@ -211,7 +218,8 @@ async def command_dispatch(client: discord.Client, message: discord.Message, con
client=client,
message=message,
conn=conn,
args=remaining_string
args=remaining_string,
system=await System.get_by_account(conn, message.author.id)
)
await run_command(ctx, command_root)
return True

View File

@@ -124,14 +124,14 @@ async def switch_move(ctx: CommandContext):
last_fronters = await last_switch.fetch_members(ctx.conn)
members = ", ".join([member.name for member in last_fronters]) or "nobody"
last_absolute = last_switch.timestamp.isoformat(sep=" ", timespec="seconds")
last_absolute = ctx.format_time(last_switch.timestamp)
last_relative = display_relative(last_switch.timestamp)
new_absolute = new_time.isoformat(sep=" ", timespec="seconds")
new_absolute = ctx.format_time(new_time)
new_relative = display_relative(new_time)
# Confirm with user
switch_confirm_message = await ctx.reply(
"This will move the latest switch ({}) from {} UTC ({} ago) to {} UTC ({} ago). Is this OK?".format(members,
"This will move the latest switch ({}) from {} ({} ago) to {} ({} ago). Is this OK?".format(members,
last_absolute,
last_relative,
new_absolute,

View File

@@ -2,6 +2,7 @@ from datetime import datetime, timedelta
import dateparser
import humanize
import pytz
import pluralkit.bot.embeds
from pluralkit.bot.commands import *
@@ -29,6 +30,8 @@ async def system_root(ctx: CommandContext):
await system_fronthistory(ctx, await ctx.ensure_system())
elif ctx.match("frontpercent") or ctx.match("frontbreakdown") or ctx.match("frontpercentage"):
await system_frontpercent(ctx, await ctx.ensure_system())
elif ctx.match("timezone") or ctx.match("tz"):
await system_timezone(ctx)
elif ctx.match("set"):
await system_set(ctx)
elif not ctx.has_next():
@@ -95,6 +98,16 @@ async def system_description(ctx: CommandContext):
await ctx.reply_ok("System description {}.".format("updated" if new_description else "cleared"))
async def system_timezone(ctx: CommandContext):
system = await ctx.ensure_system()
new_tz = ctx.remaining() or None
tz = await system.set_time_zone(ctx.conn, new_tz)
offset = tz.utcoffset(datetime.utcnow())
offset_str = "UTC{:+02d}:{:02d}".format(int(offset.total_seconds() // 3600), int(offset.total_seconds() // 60 % 60))
await ctx.reply_ok("System time zone set to {} ({}, {}).".format(tz.tzname(datetime.utcnow()), offset_str, tz.zone))
async def system_tag(ctx: CommandContext):
system = await ctx.ensure_system()
new_tag = ctx.remaining() or None
@@ -196,14 +209,14 @@ async def system_fronthistory(ctx: CommandContext, system: System):
name = ", ".join([member.name for member in members])
# Make proper date string
time_text = timestamp.isoformat(sep=" ", timespec="seconds")
time_text = ctx.format_time(timestamp)
rel_text = display_relative(timestamp)
delta_text = ""
if i > 0:
last_switch_time = front_history[i - 1][0]
delta_text = ", for {}".format(display_relative(timestamp - last_switch_time))
lines.append("**{}** ({} UTC, {} ago{})".format(name, time_text, rel_text, delta_text))
lines.append("**{}** ({}, {} ago{})".format(name, time_text, rel_text, delta_text))
embed = embeds.status("\n".join(lines) or "(none)")
embed.title = "Past switches"
@@ -302,6 +315,6 @@ async def system_frontpercent(ctx: CommandContext, system: System):
embed.add_field(name=member.name if member else "(no fronter)",
value="{}% ({})".format(percent, humanize.naturaldelta(front_time)))
embed.set_footer(text="Since {} UTC ({} ago)".format(span_start.isoformat(sep=" ", timespec="seconds"),
embed.set_footer(text="Since {} ({} ago)".format(ctx.format_time(span_start),
display_relative(span_start)))
await ctx.reply(embed=embed)