From 72cb838ad7bafcd061598e296dac30f175e07303 Mon Sep 17 00:00:00 2001 From: Ske Date: Thu, 13 Jun 2019 20:33:17 +0200 Subject: [PATCH] Add system time zone command --- PluralKit.Bot/Commands/SystemCommands.cs | 31 ++++++++++++++++++++++++ PluralKit.Bot/Errors.cs | 4 +++ PluralKit.Core/Utils.cs | 2 +- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/PluralKit.Bot/Commands/SystemCommands.cs b/PluralKit.Bot/Commands/SystemCommands.cs index 031ce9cf..bd36e766 100644 --- a/PluralKit.Bot/Commands/SystemCommands.cs +++ b/PluralKit.Bot/Commands/SystemCommands.cs @@ -1,9 +1,14 @@ using System; using System.Linq; using System.Runtime.Serialization; +using System.Text.RegularExpressions; using System.Threading.Tasks; using Dapper; using Discord.Commands; +using NodaTime; +using NodaTime.Extensions; +using NodaTime.Text; +using NodaTime.TimeZones; namespace PluralKit.Bot.Commands { @@ -140,6 +145,32 @@ namespace PluralKit.Bot.Commands } } + [Command("timezone")] + [Remarks("system timezone [timezone]")] + public async Task SystemTimezone([Remainder] string zoneStr = null) + { + if (zoneStr == null) + { + Context.SenderSystem.UiTz = "UTC"; + await Systems.Save(Context.SenderSystem); + await Context.Channel.SendMessageAsync($"{Emojis.Success} System time zone cleared."); + return; + } + + var zones = DateTimeZoneProviders.Tzdb; + var zone = zones.GetZoneOrNull(zoneStr); + if (zone == null) throw Errors.InvalidTimeZone(zoneStr); + + var currentTime = SystemClock.Instance.GetCurrentInstant().InZone(zone); + var msg = await Context.Channel.SendMessageAsync( + $"This will change the system time zone to {zone.Id}. The current time is {currentTime.ToString(Formats.DateTimeFormat, null)}. Is this correct?"); + if (!await Context.PromptYesNo(msg)) throw Errors.TimezoneChangeCancelled; + Context.SenderSystem.UiTz = zone.Id; + await Systems.Save(Context.SenderSystem); + + await Context.Channel.SendMessageAsync($"System time zone changed to {zone.Id}."); + } + public override async Task ReadContextParameterAsync(string value) { var res = await new PKSystemTypeReader().ReadAsync(Context, value, _services); diff --git a/PluralKit.Bot/Errors.cs b/PluralKit.Bot/Errors.cs index 26d3f0f8..db51581b 100644 --- a/PluralKit.Bot/Errors.cs +++ b/PluralKit.Bot/Errors.cs @@ -56,5 +56,9 @@ namespace PluralKit.Bot { public static PKError SwitchMoveBeforeSecondLast(ZonedDateTime time) => new PKError($"Can't move switch to before last switch time ({time.ToString(Formats.DateTimeFormat, null)}), as it would cause conflicts."); public static PKError SwitchMoveCancelled => new PKError("Switch move cancelled."); public static PKError SwitchDeleteCancelled => new PKError("Switch deletion cancelled."); + public static PKError TimezoneParseError(string timezone) => new PKError($"Could not parse timezone offset {timezone}. Offset must be a value like 'UTC+5' or 'GMT-4:30'."); + + public static PKError InvalidTimeZone(string zoneStr) => new PKError($"Invalid time zone ID '{zoneStr}'. To find your time zone ID, use the following website: "); + public static PKError TimezoneChangeCancelled => new PKError("Time zone change cancelled."); } } \ No newline at end of file diff --git a/PluralKit.Core/Utils.cs b/PluralKit.Core/Utils.cs index d9539bd4..e94a4951 100644 --- a/PluralKit.Core/Utils.cs +++ b/PluralKit.Core/Utils.cs @@ -206,7 +206,7 @@ namespace PluralKit public static class Formats { - public static string DateTimeFormat = "yyyy-MM-dd HH-mm-ss"; + public static string DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; public static string DurationFormat = "D'd' h'h' m'm' s's'"; } } \ No newline at end of file