Add front history command

This commit is contained in:
Ske 2019-06-15 12:43:35 +02:00
parent f4a53ce815
commit 5dafc4fbd4
2 changed files with 55 additions and 7 deletions

View File

@ -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")]

View File

@ -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<Embed> CreateSystemEmbed(PKSystem system) {
@ -71,8 +73,9 @@ namespace PluralKit.Bot {
return eb.Build();
}
public Embed CreateFronterEmbed(PKSwitch sw, ICollection<PKMember> members, DateTimeZone zone)
public async Task<Embed> 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<Embed> CreateFrontHistoryEmbed(IEnumerable<PKSwitch> 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();
}
}
}