diff --git a/PluralKit.Bot/CommandMeta/CommandTree.cs b/PluralKit.Bot/CommandMeta/CommandTree.cs index f381ce28..e5af4704 100644 --- a/PluralKit.Bot/CommandMeta/CommandTree.cs +++ b/PluralKit.Bot/CommandMeta/CommandTree.cs @@ -219,14 +219,14 @@ public partial class CommandTree if (ctx.Match("h", "history")) await ctx.Execute(SystemFrontHistory, m => m.SystemFrontHistory(ctx, target)); else if (ctx.Match("p", "percent", "%")) - await ctx.Execute(SystemFrontPercent, m => m.SystemFrontPercent(ctx, target)); + await ctx.Execute(SystemFrontPercent, m => m.FrontPercent(ctx, system: target)); else await ctx.Execute(SystemFronter, m => m.SystemFronter(ctx, target)); } else if (ctx.Match("fh", "fronthistory", "history", "switches")) await ctx.Execute(SystemFrontHistory, m => m.SystemFrontHistory(ctx, target)); else if (ctx.Match("fp", "frontpercent", "front%", "frontbreakdown")) - await ctx.Execute(SystemFrontPercent, m => m.SystemFrontPercent(ctx, target)); + await ctx.Execute(SystemFrontPercent, m => m.FrontPercent(ctx, system: target)); else if (ctx.Match("info", "view", "show")) await ctx.Execute(SystemInfo, m => m.Query(ctx, target)); else if (ctx.Match("groups", "gs")) @@ -354,7 +354,7 @@ public partial class CommandTree else if (ctx.Match("banner", "splash", "cover")) await ctx.Execute(GroupBannerImage, g => g.GroupBannerImage(ctx, target)); else if (ctx.Match("fp", "frontpercent", "front%", "frontbreakdown")) - await ctx.Execute(GroupFrontPercent, g => g.GroupFrontPercent(ctx, target)); + await ctx.Execute(GroupFrontPercent, g => g.FrontPercent(ctx, group: target)); else if (ctx.Match("color", "colour")) await ctx.Execute(GroupColor, g => g.GroupColor(ctx, target)); else if (!ctx.HasNext()) diff --git a/PluralKit.Bot/Commands/Groups.cs b/PluralKit.Bot/Commands/Groups.cs index 77dfa31c..a4851210 100644 --- a/PluralKit.Bot/Commands/Groups.cs +++ b/PluralKit.Bot/Commands/Groups.cs @@ -575,36 +575,6 @@ public class Groups await ctx.Reply($"{Emojis.Success} Group deleted."); } - public async Task GroupFrontPercent(Context ctx, PKGroup target) - { - var targetSystem = await GetGroupSystem(ctx, target); - ctx.CheckSystemPrivacy(targetSystem.Id, targetSystem.FrontHistoryPrivacy); - - var totalSwitches = await _repo.GetSwitchCount(targetSystem.Id); - if (totalSwitches == 0) throw Errors.NoRegisteredSwitches; - - var durationStr = ctx.RemainderOrNull() ?? "30d"; - - var now = SystemClock.Instance.GetCurrentInstant(); - - var rangeStart = DateUtils.ParseDateTime(durationStr, true, ctx.Zone); - if (rangeStart == null) throw Errors.InvalidDateTime(durationStr); - if (rangeStart.Value.ToInstant() > now) throw Errors.FrontPercentTimeInFuture; - - var title = new StringBuilder($"Frontpercent of {target.DisplayName ?? target.Name} (`{target.Hid}`) in "); - if (targetSystem.Name != null) - title.Append($"{targetSystem.Name} (`{targetSystem.Hid}`)"); - else - title.Append($"`{targetSystem.Hid}`"); - - var ignoreNoFronters = ctx.MatchFlag("fo", "fronters-only"); - var showFlat = ctx.MatchFlag("flat"); - var frontpercent = await _db.Execute(c => - _repo.GetFrontBreakdown(c, targetSystem.Id, target.Id, rangeStart.Value.ToInstant(), now)); - await ctx.Reply(embed: await _embeds.CreateFrontPercentEmbed(frontpercent, targetSystem, target, - ctx.Zone, ctx.LookupContextFor(targetSystem.Id), title.ToString(), ignoreNoFronters, showFlat)); - } - private async Task GetGroupSystem(Context ctx, PKGroup target) { var system = ctx.System; diff --git a/PluralKit.Bot/Commands/SystemFront.cs b/PluralKit.Bot/Commands/SystemFront.cs index f0606626..a599172a 100644 --- a/PluralKit.Bot/Commands/SystemFront.cs +++ b/PluralKit.Bot/Commands/SystemFront.cs @@ -95,14 +95,19 @@ public class SystemFront ); } - public async Task SystemFrontPercent(Context ctx, PKSystem system) + public async Task FrontPercent(Context ctx, PKSystem? system = null, PKGroup? group = null) { - if (system == null) throw Errors.NoSystemError; + if (system == null && group == null) throw Errors.NoSystemError; + if (system == null) system = await GetGroupSystem(ctx, group); + ctx.CheckSystemPrivacy(system.Id, system.FrontHistoryPrivacy); var totalSwitches = await _repo.GetSwitchCount(system.Id); if (totalSwitches == 0) throw Errors.NoRegisteredSwitches; + var ignoreNoFronters = ctx.MatchFlag("fo", "fronters-only"); + var showFlat = ctx.MatchFlag("flat"); + var durationStr = ctx.RemainderOrNull() ?? "30d"; // Picked the UNIX epoch as a random date @@ -118,19 +123,26 @@ public class SystemFront if (rangeStart.Value.ToInstant() > now) throw Errors.FrontPercentTimeInFuture; var title = new StringBuilder("Frontpercent of "); - if (system.Name != null) + if (group != null) + title.Append($"{group.NameFor(ctx)} (`{group.Hid}`)"); + else if (system.Name != null) title.Append($"{system.Name} (`{system.Hid}`)"); else title.Append($"`{system.Hid}`"); - var ignoreNoFronters = ctx.MatchFlag("fo", "fronters-only"); - var showFlat = ctx.MatchFlag("flat"); - var frontpercent = await _db.Execute(c => - _repo.GetFrontBreakdown(c, system.Id, null, rangeStart.Value.ToInstant(), now)); - await ctx.Reply(embed: await _embeds.CreateFrontPercentEmbed(frontpercent, system, null, ctx.Zone, + var frontpercent = await _db.Execute(c => _repo.GetFrontBreakdown(c, system.Id, group?.Id, rangeStart.Value.ToInstant(), now)); + await ctx.Reply(embed: await _embeds.CreateFrontPercentEmbed(frontpercent, system, group, ctx.Zone, ctx.LookupContextFor(system.Id), title.ToString(), ignoreNoFronters, showFlat)); } + private async Task GetGroupSystem(Context ctx, PKGroup target) + { + var system = ctx.System; + if (system?.Id == target.System) + return system; + return await _repo.GetSystem(target.System)!; + } + private struct FrontHistoryEntry { public readonly Instant? LastTime;