diff --git a/PluralKit.Bot/Commands/SystemCommands.cs b/PluralKit.Bot/Commands/SystemCommands.cs index 543d9dbb..080c0eda 100644 --- a/PluralKit.Bot/Commands/SystemCommands.cs +++ b/PluralKit.Bot/Commands/SystemCommands.cs @@ -214,15 +214,15 @@ namespace PluralKit.Bot.Commands { var system = ContextEntity ?? Context.SenderSystem; if (system == null) throw Errors.NoSystemError; + + var now = SystemClock.Instance.GetCurrentInstant(); - var duration = PluralKit.Utils.ParsePeriod(durationStr); - if (duration == null) throw Errors.InvalidDateTime(durationStr); + var rangeStart = PluralKit.Utils.ParseDateTime(durationStr); + if (rangeStart == null) throw Errors.InvalidDateTime(durationStr); + if (rangeStart.Value.ToInstant() > now) throw Errors.FrontPercentTimeInFuture; - var rangeEnd = SystemClock.Instance.GetCurrentInstant(); - var rangeStart = rangeEnd - duration.Value; - - var frontpercent = await Switches.GetPerMemberSwitchDuration(system, rangeEnd - duration.Value, rangeEnd); - await Context.Channel.SendMessageAsync(embed: await EmbedService.CreateFrontPercentEmbed(frontpercent, rangeStart.InZone(system.Zone))); + var frontpercent = await Switches.GetPerMemberSwitchDuration(system, rangeStart.Value.ToInstant(), now); + await Context.Channel.SendMessageAsync(embed: await EmbedService.CreateFrontPercentEmbed(frontpercent, system.Zone)); } [Command("timezone")] diff --git a/PluralKit.Bot/Errors.cs b/PluralKit.Bot/Errors.cs index 3ffad1b8..7425f31c 100644 --- a/PluralKit.Bot/Errors.cs +++ b/PluralKit.Bot/Errors.cs @@ -70,5 +70,6 @@ namespace PluralKit.Bot { public static PKError MessageNotFound(ulong id) => new PKError($"Message with ID '{id}' not found. Are you sure it's a message proxied by PluralKit?"); public static PKError DurationParseError(string durationStr) => new PKError($"Could not parse '{durationStr.Sanitize()}' as a valid duration. Try a format such as `30d`, `1d3h` or `20m30s`."); + public static PKError FrontPercentTimeInFuture => new PKError("Cannot get the front percent between now and a time in the future."); } } \ No newline at end of file diff --git a/PluralKit.Bot/Services/EmbedService.cs b/PluralKit.Bot/Services/EmbedService.cs index 46c98ef3..35b1f3e4 100644 --- a/PluralKit.Bot/Services/EmbedService.cs +++ b/PluralKit.Bot/Services/EmbedService.cs @@ -146,12 +146,12 @@ namespace PluralKit.Bot { .Build(); } - public async Task CreateFrontPercentEmbed(SwitchStore.PerMemberSwitchDuration frontpercent, ZonedDateTime startingFrom) + public async Task CreateFrontPercentEmbed(SwitchStore.PerMemberSwitchDuration frontpercent, DateTimeZone tz) { var actualPeriod = frontpercent.RangeEnd - frontpercent.RangeStart; var eb = new EmbedBuilder() .WithColor(Color.Blue) - .WithFooter($"Since {Formats.ZonedDateTimeFormat.Format(startingFrom)} ({Formats.DurationFormat.Format(actualPeriod)} ago)"); + .WithFooter($"Since {Formats.ZonedDateTimeFormat.Format(frontpercent.RangeStart.InZone(tz))} ({Formats.DurationFormat.Format(actualPeriod)} ago)"); var maxEntriesToDisplay = 24; // max 25 fields allowed in embed - reserve 1 for "others" diff --git a/PluralKit.Core/Stores.cs b/PluralKit.Core/Stores.cs index ce909066..347d68e6 100644 --- a/PluralKit.Core/Stores.cs +++ b/PluralKit.Core/Stores.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using System.Data; using System.Linq; +using System.Threading; using System.Threading.Tasks; using Dapper; using Dapper.Contrib.Extensions; @@ -325,7 +326,6 @@ namespace PluralKit { var switchStartClamped = switchInRange.Timestamp; if (switchStartClamped < periodStart) switchStartClamped = periodStart; - var span = endTime - switchStartClamped; outList.Add(new SwitchListEntry { Members = (await GetSwitchMemberIds(switchInRange)).Select(id => memberObjects[id]).ToList(), @@ -373,7 +373,7 @@ namespace PluralKit { if (sw.Members.Count == 0) noFronterDuration += span; if (sw.TimespanStart < actualStart) actualStart = sw.TimespanStart; - if (sw.TimespanEnd < actualStart) actualStart = sw.TimespanEnd; + if (sw.TimespanEnd > actualEnd) actualEnd = sw.TimespanEnd; } return new PerMemberSwitchDuration