feat: pk;config

This commit is contained in:
spiral
2021-11-29 21:35:21 -05:00
parent d195c80d92
commit 56d07e0f2d
41 changed files with 648 additions and 313 deletions

View File

@@ -509,39 +509,6 @@ public class SystemEdit
await ctx.Reply($"Message proxying in {serverText} is now **disabled** for your system.");
}
public async Task SystemTimezone(Context ctx)
{
if (ctx.System == null) throw Errors.NoSystemError;
if (await ctx.MatchClear())
{
await _repo.UpdateSystem(ctx.System.Id, new SystemPatch { UiTz = "UTC" });
await ctx.Reply($"{Emojis.Success} System time zone cleared (set to UTC).");
return;
}
var zoneStr = ctx.RemainderOrNull();
if (zoneStr == null)
{
await ctx.Reply(
$"Your current system time zone is set to **{ctx.System.UiTz}**. It is currently **{SystemClock.Instance.GetCurrentInstant().FormatZoned(ctx.System)}** in that time zone. To change your system time zone, type `pk;s tz <zone>`.");
return;
}
var zone = await FindTimeZone(ctx, zoneStr);
if (zone == null) throw Errors.InvalidTimeZone(zoneStr);
var currentTime = SystemClock.Instance.GetCurrentInstant().InZone(zone);
var msg =
$"This will change the system time zone to **{zone.Id}**. The current time is **{currentTime.FormatZoned()}**. Is this correct?";
if (!await ctx.PromptYesNo(msg, "Change Timezone")) throw Errors.TimezoneChangeCancelled;
await _repo.UpdateSystem(ctx.System.Id, new SystemPatch { UiTz = zone.Id });
await ctx.Reply($"System time zone changed to **{zone.Id}**.");
}
public async Task SystemPrivacy(Context ctx)
{
ctx.CheckSystem();
@@ -609,96 +576,4 @@ public class SystemEdit
else
await SetLevel(ctx.PopSystemPrivacySubject(), ctx.PopPrivacyLevel());
}
public async Task SystemPing(Context ctx)
{
ctx.CheckSystem();
if (!ctx.HasNext())
{
if (ctx.System.PingsEnabled)
await ctx.Reply(
"Reaction pings are currently **enabled** for your system. To disable reaction pings, type `pk;s ping disable`.");
else
await ctx.Reply(
"Reaction pings are currently **disabled** for your system. To enable reaction pings, type `pk;s ping enable`.");
}
else
{
if (ctx.Match("on", "enable"))
{
await _repo.UpdateSystem(ctx.System.Id, new SystemPatch { PingsEnabled = true });
await ctx.Reply("Reaction pings have now been enabled.");
}
if (ctx.Match("off", "disable"))
{
await _repo.UpdateSystem(ctx.System.Id, new SystemPatch { PingsEnabled = false });
await ctx.Reply("Reaction pings have now been disabled.");
}
}
}
public async Task<DateTimeZone> FindTimeZone(Context ctx, string zoneStr)
{
// First, if we're given a flag emoji, we extract the flag emoji code from it.
zoneStr = StringUtils.ExtractCountryFlag(zoneStr) ?? zoneStr;
// Then, we find all *locations* matching either the given country code or the country name.
var locations = TzdbDateTimeZoneSource.Default.Zone1970Locations;
var matchingLocations = locations.Where(l => l.Countries.Any(c =>
string.Equals(c.Code, zoneStr, StringComparison.InvariantCultureIgnoreCase) ||
string.Equals(c.Name, zoneStr, StringComparison.InvariantCultureIgnoreCase)));
// Then, we find all (unique) time zone IDs that match.
var matchingZones = matchingLocations.Select(l => DateTimeZoneProviders.Tzdb.GetZoneOrNull(l.ZoneId))
.Distinct().ToList();
// If the set of matching zones is empty (ie. we didn't find anything), we try a few other things.
if (matchingZones.Count == 0)
{
// First, we try to just find the time zone given directly and return that.
var givenZone = DateTimeZoneProviders.Tzdb.GetZoneOrNull(zoneStr);
if (givenZone != null) return givenZone;
// If we didn't find anything there either, we try parsing the string as an offset, then
// find all possible zones that match that offset. For an offset like UTC+2, this doesn't *quite*
// work, since there are 57(!) matching zones (as of 2019-06-13) - but for less populated time zones
// this could work nicely.
var inputWithoutUtc = zoneStr.Replace("UTC", "").Replace("GMT", "");
var res = OffsetPattern.CreateWithInvariantCulture("+H").Parse(inputWithoutUtc);
if (!res.Success) res = OffsetPattern.CreateWithInvariantCulture("+H:mm").Parse(inputWithoutUtc);
// If *this* didn't parse correctly, fuck it, bail.
if (!res.Success) return null;
var offset = res.Value;
// To try to reduce the count, we go by locations from the 1970+ database instead of just the full database
// This elides regions that have been identical since 1970, omitting small distinctions due to Ancient History(tm).
var allZones = TzdbDateTimeZoneSource.Default.Zone1970Locations.Select(l => l.ZoneId).Distinct();
matchingZones = allZones.Select(z => DateTimeZoneProviders.Tzdb.GetZoneOrNull(z))
.Where(z => z.GetUtcOffset(SystemClock.Instance.GetCurrentInstant()) == offset).ToList();
}
// If we have a list of viable time zones, we ask the user which is correct.
// If we only have one, return that one.
if (matchingZones.Count == 1)
return matchingZones.First();
// Otherwise, prompt and return!
return await ctx.Choose(
"There were multiple matches for your time zone query. Please select the region that matches you the closest:",
matchingZones,
z =>
{
if (TzdbDateTimeZoneSource.Default.Aliases.Contains(z.Id))
return $"**{z.Id}**, {string.Join(", ", TzdbDateTimeZoneSource.Default.Aliases[z.Id])}";
return $"**{z.Id}**";
});
}
}