Add system time zone command

This commit is contained in:
Ske 2019-06-13 20:33:17 +02:00
parent 6cfa4cb2e5
commit 72cb838ad7
3 changed files with 36 additions and 1 deletions

View File

@ -1,9 +1,14 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using Dapper; using Dapper;
using Discord.Commands; using Discord.Commands;
using NodaTime;
using NodaTime.Extensions;
using NodaTime.Text;
using NodaTime.TimeZones;
namespace PluralKit.Bot.Commands 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<PKSystem> ReadContextParameterAsync(string value) public override async Task<PKSystem> ReadContextParameterAsync(string value)
{ {
var res = await new PKSystemTypeReader().ReadAsync(Context, value, _services); var res = await new PKSystemTypeReader().ReadAsync(Context, value, _services);

View File

@ -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 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 SwitchMoveCancelled => new PKError("Switch move cancelled.");
public static PKError SwitchDeleteCancelled => new PKError("Switch deletion 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: <https://xske.github.io/tz>");
public static PKError TimezoneChangeCancelled => new PKError("Time zone change cancelled.");
} }
} }

View File

@ -206,7 +206,7 @@ namespace PluralKit
public static class Formats 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'"; public static string DurationFormat = "D'd' h'h' m'm' s's'";
} }
} }