Add front percent command

This commit is contained in:
Ske
2019-06-30 23:41:01 +02:00
parent 7eeaea39fe
commit 42147fd9cc
5 changed files with 138 additions and 4 deletions

View File

@@ -172,6 +172,22 @@ namespace PluralKit.Bot.Commands
await Context.Channel.SendMessageAsync(embed: await EmbedService.CreateFrontHistoryEmbed(sws, system.Zone));
}
[Command("frontpercent")]
public async Task SystemFrontPercent(string durationStr = "30d")
{
var system = ContextEntity ?? Context.SenderSystem;
if (system == null) throw Errors.NoSystemError;
var duration = PluralKit.Utils.ParsePeriod(durationStr);
if (duration == null) throw Errors.InvalidDateTime(durationStr);
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)));
}
[Command("timezone")]
[Remarks("system timezone [timezone]")]
[MustHaveSystem]

View File

@@ -65,5 +65,7 @@ namespace PluralKit.Bot {
public static PKError InvalidImportFile => new PKError("Imported data file invalid. Make sure this is a .json file directly exported from PluralKit or Tupperbox.");
public static PKError ImportCancelled => new PKError("Import cancelled.");
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}' as a valid duration. Try a format such as `30d`, `1d3h` or `20m30s`.");
}
}

View File

@@ -136,5 +136,32 @@ namespace PluralKit.Bot {
.WithTimestamp(SnowflakeUtils.FromSnowflake(msg.Message.Mid))
.Build();
}
public async Task<Embed> CreateFrontPercentEmbed(IDictionary<PKMember, Duration> frontpercent, ZonedDateTime startingFrom)
{
var totalDuration = SystemClock.Instance.GetCurrentInstant() - startingFrom.ToInstant();
var eb = new EmbedBuilder()
.WithColor(Color.Blue)
.WithFooter($"Since {Formats.ZonedDateTimeFormat.Format(startingFrom)} ({Formats.DurationFormat.Format(totalDuration)} ago)");
var maxEntriesToDisplay = 24; // max 25 fields allowed in embed - reserve 1 for "others"
var membersOrdered = frontpercent.OrderBy(pair => pair.Value).Take(maxEntriesToDisplay).ToList();
foreach (var pair in membersOrdered)
{
var frac = pair.Value / totalDuration;
eb.AddField(pair.Key.Name, $"{frac*100:F0}% ({Formats.DurationFormat.Format(pair.Value)})");
}
if (membersOrdered.Count > maxEntriesToDisplay)
{
eb.AddField("(others)",
Formats.DurationFormat.Format(membersOrdered.Skip(maxEntriesToDisplay)
.Aggregate(Duration.Zero, (prod, next) => prod + next.Value)), true);
}
return eb.Build();
}
}
}