From 5dafc4fbd4bd1ec285875c14d30706da60cc7080 Mon Sep 17 00:00:00 2001 From: Ske Date: Sat, 15 Jun 2019 12:43:35 +0200 Subject: [PATCH] Add front history command --- PluralKit.Bot/Commands/SystemCommands.cs | 15 +++++++- PluralKit.Bot/Services/EmbedService.cs | 47 +++++++++++++++++++++--- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/PluralKit.Bot/Commands/SystemCommands.cs b/PluralKit.Bot/Commands/SystemCommands.cs index 6678034c..5c81660f 100644 --- a/PluralKit.Bot/Commands/SystemCommands.cs +++ b/PluralKit.Bot/Commands/SystemCommands.cs @@ -157,8 +157,19 @@ namespace PluralKit.Bot.Commands var sw = await Switches.GetLatestSwitch(system); if (sw == null) throw Errors.NoRegisteredSwitches; - var members = await Switches.GetSwitchMembers(sw); - await Context.Channel.SendMessageAsync(embed: EmbedService.CreateFronterEmbed(sw, members.ToList(), system.Zone)); + await Context.Channel.SendMessageAsync(embed: await EmbedService.CreateFronterEmbed(sw, system.Zone)); + } + + [Command("fronthistory")] + public async Task SystemFrontHistory() + { + var system = ContextEntity ?? Context.SenderSystem; + if (system == null) throw Errors.NoSystemError; + + var sws = (await Switches.GetSwitches(system, 10)).ToList(); + if (sws.Count == 0) throw Errors.NoRegisteredSwitches; + + await Context.Channel.SendMessageAsync(embed: await EmbedService.CreateFrontHistoryEmbed(sws, system.Zone)); } [Command("timezone")] diff --git a/PluralKit.Bot/Services/EmbedService.cs b/PluralKit.Bot/Services/EmbedService.cs index 941e7a20..e7d6fb4c 100644 --- a/PluralKit.Bot/Services/EmbedService.cs +++ b/PluralKit.Bot/Services/EmbedService.cs @@ -9,13 +9,15 @@ namespace PluralKit.Bot { public class EmbedService { private SystemStore _systems; private MemberStore _members; + private SwitchStore _switches; private IDiscordClient _client; - public EmbedService(SystemStore systems, MemberStore members, IDiscordClient client) + public EmbedService(SystemStore systems, MemberStore members, IDiscordClient client, SwitchStore switches) { - this._systems = systems; - this._members = members; - this._client = client; + _systems = systems; + _members = members; + _client = client; + _switches = switches; } public async Task CreateSystemEmbed(PKSystem system) { @@ -71,8 +73,9 @@ namespace PluralKit.Bot { return eb.Build(); } - public Embed CreateFronterEmbed(PKSwitch sw, ICollection members, DateTimeZone zone) + public async Task CreateFronterEmbed(PKSwitch sw, DateTimeZone zone) { + var members = (await _switches.GetSwitchMembers(sw)).ToList(); var timeSinceSwitch = SystemClock.Instance.GetCurrentInstant() - sw.Timestamp; return new EmbedBuilder() .WithColor(members.FirstOrDefault()?.Color?.ToDiscordColor() ?? Color.Blue) @@ -80,5 +83,39 @@ namespace PluralKit.Bot { .AddField("Since", $"{Formats.ZonedDateTimeFormat.Format(sw.Timestamp.InZone(zone))} ({Formats.DurationFormat.Format(timeSinceSwitch)} ago)", true) .Build(); } + + public async Task CreateFrontHistoryEmbed(IEnumerable sws, DateTimeZone zone) + { + var outputStr = ""; + + PKSwitch lastSw = null; + foreach (var sw in sws) + { + // Fetch member list and format + var members = (await _switches.GetSwitchMembers(sw)).ToList(); + var membersStr = members.Any() ? string.Join(", ", members.Select(m => m.Name)) : "no fronter"; + + var switchSince = SystemClock.Instance.GetCurrentInstant() - sw.Timestamp; + + // If this isn't the latest switch, we also show duration + if (lastSw != null) + { + // Calculate the time between the last switch (that we iterated - ie. the next one on the timeline) and the current one + var switchDuration = lastSw.Timestamp - sw.Timestamp; + outputStr += $"**{membersStr}** ({Formats.ZonedDateTimeFormat.Format(sw.Timestamp.InZone(zone))}, {Formats.DurationFormat.Format(switchSince)} ago, for {Formats.DurationFormat.Format(switchDuration)})\n"; + } + else + { + outputStr += $"**{membersStr}** ({Formats.ZonedDateTimeFormat.Format(sw.Timestamp.InZone(zone))}, {Formats.DurationFormat.Format(switchSince)} ago)\n"; + } + + lastSw = sw; + } + + return new EmbedBuilder() + .WithTitle("Past switches") + .WithDescription(outputStr) + .Build(); + } } } \ No newline at end of file