PluralKit/PluralKit.Bot/Services/EmbedService.cs

407 lines
19 KiB
C#
Raw Normal View History

2019-08-13 19:49:43 +00:00
using System;
2019-06-15 10:19:44 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2019-07-14 21:49:14 +00:00
using Humanizer;
using Myriad.Builders;
using Myriad.Cache;
2020-12-25 12:58:45 +00:00
using Myriad.Extensions;
using Myriad.Rest;
2021-01-31 16:56:33 +00:00
using Myriad.Rest.Exceptions;
using Myriad.Types;
2019-06-15 10:19:44 +00:00
using NodaTime;
using PluralKit.Core;
namespace PluralKit.Bot {
public class EmbedService
{
2020-08-29 11:46:27 +00:00
private readonly IDatabase _db;
private readonly ModelRepository _repo;
private readonly IDiscordCache _cache;
private readonly DiscordApiClient _rest;
2020-12-25 12:58:45 +00:00
public EmbedService(IDatabase db, ModelRepository repo, IDiscordCache cache, DiscordApiClient rest)
{
_db = db;
2020-08-29 11:46:27 +00:00
_repo = repo;
_cache = cache;
_rest = rest;
}
private Task<(ulong Id, User? User)[]> GetUsers(IEnumerable<ulong> ids)
{
async Task<(ulong Id, User? User)> Inner(ulong id)
{
2020-12-25 12:58:45 +00:00
var user = await _cache.GetOrFetchUser(_rest, id);
return (id, user);
}
return Task.WhenAll(ids.Select(Inner));
}
Feature/granular member privacy (#174) * Some reasons this needs to exist for it to run on my machine? I don't think it would hurt to have it in other machines so * Add options to member model * Add Privacy to member embed * Added member privacy display list * Update database settings * apparetnly this is nolonger needed? * Fix sql call * Fix more sql errors * Added in settings control * Add all subject to system privacy * Basic API Privacy * Name privacy in logs * update todo * remove CheckReadMemberPermission * Added name privacy to log embed * update todo * Update todo * Update api to handle privacy * update todo * Update systemlist full to respect privacy (as well as system list) * include colour as option for member privacy subject * move todo file (why was it there?) * Update TODO.md * Update TODO.md * Update TODO.md * Deleted to create pr * Update command usage and add to the command tree * Make api respect created privacy * Add editing privacy through the api * Fix pronoun privacy field in api * Fix info leak of display name in api * deprecate privacy field in api * Deprecate privacy diffrently * Update API * Update documentation * Update documentation * Remove comment in yml * Update userguide * Update migration (fix typo in 5.sql too) * Sanatize names * some full stops * Fix after merge * update migration * update schema version * update edit command * update privacy filter * fix a dumb mistake * clarify on what name privacy does * make it easier on someone else * Update docs * Comment out unused code * Add aliases for `member privacy all public` and `member privacy all private`
2020-06-17 19:31:39 +00:00
public async Task<Embed> CreateSystemEmbed(Context cctx, PKSystem system, LookupContext ctx)
2020-06-29 12:54:11 +00:00
{
await using var conn = await _db.Obtain();
// Fetch/render info for all accounts simultaneously
2020-08-29 11:46:27 +00:00
var accounts = await _repo.GetSystemAccounts(conn, system.Id);
var users = (await GetUsers(accounts)).Select(x => x.User?.NameAndMention() ?? $"(deleted account {x.Id})");
var memberCount = cctx.MatchPrivateFlag(ctx) ? await _repo.GetSystemMemberCount(conn, system.Id, PrivacyLevel.Public) : await _repo.GetSystemMemberCount(conn, system.Id);
2021-03-28 10:02:41 +00:00
uint color;
try
{
color = system.Color?.ToDiscordColor() ?? DiscordUtils.Gray;
}
catch (ArgumentException)
{
// There's no API for system colors yet, but defaulting to a blank color in advance can't be a bad idea
color = DiscordUtils.Gray;
}
var eb = new EmbedBuilder()
.Title(system.Name)
.Thumbnail(new(system.AvatarUrl))
.Footer(new($"System ID: {system.Hid} | Created on {system.Created.FormatZoned(system)}"))
2021-03-28 10:02:41 +00:00
.Color(color);
2020-08-29 11:46:27 +00:00
var latestSwitch = await _repo.GetLatestSwitch(conn, system.Id);
2020-01-11 15:49:20 +00:00
if (latestSwitch != null && system.FrontPrivacy.CanAccess(ctx))
2019-07-18 12:05:02 +00:00
{
2020-08-29 11:46:27 +00:00
var switchMembers = await _repo.GetSwitchMembers(conn, latestSwitch.Id).ToListAsync();
if (switchMembers.Count > 0)
eb.Field(new("Fronter".ToQuantity(switchMembers.Count, ShowQuantityAs.None), string.Join(", ", switchMembers.Select(m => m.NameFor(ctx)))));
2019-07-18 12:05:02 +00:00
}
if (system.Tag != null)
2021-03-28 17:22:45 +00:00
eb.Field(new("Tag", system.Tag.EscapeMarkdown(), true));
if (!system.Color.EmptyOrNull()) eb.Field(new("Color", $"#{system.Color}", true));
eb.Field(new("Linked accounts", string.Join("\n", users).Truncate(1000), true));
2020-01-11 15:49:20 +00:00
if (system.MemberListPrivacy.CanAccess(ctx))
{
if (memberCount > 0)
eb.Field(new($"Members ({memberCount})", $"(see `pk;system {system.Hid} list` or `pk;system {system.Hid} list full`)", true));
else
eb.Field(new($"Members ({memberCount})", "Add one with `pk;member new`!", true));
}
if (system.DescriptionFor(ctx) is { } desc)
eb.Field(new("Description", desc.NormalizeLineEndSpacing().Truncate(1024), false));
return eb.Build();
}
public Embed CreateLoggedMessageEmbed(PKSystem system, PKMember member, ulong messageId, ulong originalMsgId, User sender, string content, Channel channel) {
// TODO: pronouns in ?-reacted response using this card
var timestamp = DiscordUtils.SnowflakeToInstant(messageId);
var name = member.NameFor(LookupContext.ByNonOwner);
return new EmbedBuilder()
.Author(new($"#{channel.Name}: {name}", IconUrl: DiscordUtils.WorkaroundForUrlBug(member.AvatarFor(LookupContext.ByNonOwner))))
.Thumbnail(new(member.AvatarFor(LookupContext.ByNonOwner)))
.Description(content?.NormalizeLineEndSpacing())
.Footer(new($"System ID: {system.Hid} | Member ID: {member.Hid} | Sender: {sender.Username}#{sender.Discriminator} ({sender.Id}) | Message ID: {messageId} | Original Message ID: {originalMsgId}"))
.Timestamp(timestamp.ToDateTimeOffset().ToString("O"))
.Build();
}
2019-05-11 22:44:02 +00:00
2021-05-07 16:35:09 +00:00
public Embed CreateEditedMessageEmbed(PKSystem system, PKMember member, ulong messageId, ulong originalMsgId, User sender, string content, string oldContent, Channel channel) {
var timestamp = DiscordUtils.SnowflakeToInstant(messageId);
var name = member.NameFor(LookupContext.ByNonOwner);
return new EmbedBuilder()
.Author(new($"[Edited] #{channel.Name}: {name}", IconUrl: DiscordUtils.WorkaroundForUrlBug(member.AvatarFor(LookupContext.ByNonOwner))))
.Thumbnail(new(member.AvatarFor(LookupContext.ByNonOwner)))
.Field(new("Old message", oldContent?.NormalizeLineEndSpacing().Truncate(1000)))
.Description(content?.NormalizeLineEndSpacing())
.Footer(new($"System ID: {system.Hid} | Member ID: {member.Hid} | Sender: {sender.Username}#{sender.Discriminator} ({sender.Id}) | Message ID: {messageId} | Original Message ID: {originalMsgId}"))
.Timestamp(timestamp.ToDateTimeOffset().ToString("O"))
.Build();
}
2020-12-25 12:58:45 +00:00
public async Task<Embed> CreateMemberEmbed(PKSystem system, PKMember member, Guild guild, LookupContext ctx)
2019-05-11 22:44:02 +00:00
{
Feature/granular member privacy (#174) * Some reasons this needs to exist for it to run on my machine? I don't think it would hurt to have it in other machines so * Add options to member model * Add Privacy to member embed * Added member privacy display list * Update database settings * apparetnly this is nolonger needed? * Fix sql call * Fix more sql errors * Added in settings control * Add all subject to system privacy * Basic API Privacy * Name privacy in logs * update todo * remove CheckReadMemberPermission * Added name privacy to log embed * update todo * Update todo * Update api to handle privacy * update todo * Update systemlist full to respect privacy (as well as system list) * include colour as option for member privacy subject * move todo file (why was it there?) * Update TODO.md * Update TODO.md * Update TODO.md * Deleted to create pr * Update command usage and add to the command tree * Make api respect created privacy * Add editing privacy through the api * Fix pronoun privacy field in api * Fix info leak of display name in api * deprecate privacy field in api * Deprecate privacy diffrently * Update API * Update documentation * Update documentation * Remove comment in yml * Update userguide * Update migration (fix typo in 5.sql too) * Sanatize names * some full stops * Fix after merge * update migration * update schema version * update edit command * update privacy filter * fix a dumb mistake * clarify on what name privacy does * make it easier on someone else * Update docs * Comment out unused code * Add aliases for `member privacy all public` and `member privacy all private`
2020-06-17 19:31:39 +00:00
// string FormatTimestamp(Instant timestamp) => DateTimeFormats.ZonedDateTimeFormat.Format(timestamp.InZone(system.Zone));
var name = member.NameFor(ctx);
if (system.Name != null) name = $"{name} ({system.Name})";
2021-01-15 10:29:43 +00:00
uint color;
2019-08-13 19:49:43 +00:00
try
{
2020-04-28 22:25:01 +00:00
color = member.Color?.ToDiscordColor() ?? DiscordUtils.Gray;
2019-08-13 19:49:43 +00:00
}
catch (ArgumentException)
{
// Bad API use can cause an invalid color string
// TODO: fix that in the API
// for now we just default to a blank color, yolo
2020-04-28 22:25:01 +00:00
color = DiscordUtils.Gray;
2019-08-13 19:49:43 +00:00
}
2020-07-18 11:26:36 +00:00
await using var conn = await _db.Obtain();
2020-08-29 11:46:27 +00:00
var guildSettings = guild != null ? await _repo.GetMemberGuild(conn, guild.Id, member.Id) : null;
2020-02-12 16:42:12 +00:00
var guildDisplayName = guildSettings?.DisplayName;
2020-06-20 14:00:50 +00:00
var avatar = guildSettings?.AvatarUrl ?? member.AvatarFor(ctx);
2019-12-26 19:39:47 +00:00
2020-08-29 11:46:27 +00:00
var groups = await _repo.GetMemberGroups(conn, member.Id)
.Where(g => g.Visibility.CanAccess(ctx))
.OrderBy(g => g.Name, StringComparer.InvariantCultureIgnoreCase)
2020-08-29 11:46:27 +00:00
.ToListAsync();
var eb = new EmbedBuilder()
2019-05-11 22:44:02 +00:00
// TODO: add URL of website when that's up
.Author(new(name, IconUrl: DiscordUtils.WorkaroundForUrlBug(avatar)))
// .WithColor(member.ColorPrivacy.CanAccess(ctx) ? color : DiscordUtils.Gray)
2021-01-15 10:29:43 +00:00
.Color(color)
.Footer(new(
$"System ID: {system.Hid} | Member ID: {member.Hid} {(member.MetadataPrivacy.CanAccess(ctx) ? $"| Created on {member.Created.FormatZoned(system)}" : "")}"));
2019-05-11 22:44:02 +00:00
2020-02-12 16:42:12 +00:00
var description = "";
Feature/granular member privacy (#174) * Some reasons this needs to exist for it to run on my machine? I don't think it would hurt to have it in other machines so * Add options to member model * Add Privacy to member embed * Added member privacy display list * Update database settings * apparetnly this is nolonger needed? * Fix sql call * Fix more sql errors * Added in settings control * Add all subject to system privacy * Basic API Privacy * Name privacy in logs * update todo * remove CheckReadMemberPermission * Added name privacy to log embed * update todo * Update todo * Update api to handle privacy * update todo * Update systemlist full to respect privacy (as well as system list) * include colour as option for member privacy subject * move todo file (why was it there?) * Update TODO.md * Update TODO.md * Update TODO.md * Deleted to create pr * Update command usage and add to the command tree * Make api respect created privacy * Add editing privacy through the api * Fix pronoun privacy field in api * Fix info leak of display name in api * deprecate privacy field in api * Deprecate privacy diffrently * Update API * Update documentation * Update documentation * Remove comment in yml * Update userguide * Update migration (fix typo in 5.sql too) * Sanatize names * some full stops * Fix after merge * update migration * update schema version * update edit command * update privacy filter * fix a dumb mistake * clarify on what name privacy does * make it easier on someone else * Update docs * Comment out unused code * Add aliases for `member privacy all public` and `member privacy all private`
2020-06-17 19:31:39 +00:00
if (member.MemberVisibility == PrivacyLevel.Private) description += "*(this member is hidden)*\n";
2020-02-12 16:42:12 +00:00
if (guildSettings?.AvatarUrl != null)
2020-06-20 14:00:50 +00:00
if (member.AvatarFor(ctx) != null)
2020-02-12 16:42:12 +00:00
description += $"*(this member has a server-specific avatar set; [click here]({member.AvatarUrl}) to see the global avatar)*\n";
else
description += "*(this member has a server-specific avatar set)*\n";
if (description != "") eb.Description(description);
2020-08-16 10:10:54 +00:00
if (avatar != null) eb.Thumbnail(new(avatar));
if (!member.DisplayName.EmptyOrNull() && member.NamePrivacy.CanAccess(ctx)) eb.Field(new("Display Name", member.DisplayName.Truncate(1024), true));
if (guild != null && guildDisplayName != null) eb.Field(new($"Server Nickname (for {guild.Name})", guildDisplayName.Truncate(1024), true));
if (member.BirthdayFor(ctx) != null) eb.Field(new("Birthdate", member.BirthdayString, true));
if (member.PronounsFor(ctx) is {} pronouns && !string.IsNullOrWhiteSpace(pronouns)) eb.Field(new("Pronouns", pronouns.Truncate(1024), true));
if (member.MessageCountFor(ctx) is {} count && count > 0) eb.Field(new("Message Count", member.MessageCount.ToString(), true));
if (member.HasProxyTags) eb.Field(new("Proxy Tags", member.ProxyTagsString("\n").Truncate(1024), true));
Feature/granular member privacy (#174) * Some reasons this needs to exist for it to run on my machine? I don't think it would hurt to have it in other machines so * Add options to member model * Add Privacy to member embed * Added member privacy display list * Update database settings * apparetnly this is nolonger needed? * Fix sql call * Fix more sql errors * Added in settings control * Add all subject to system privacy * Basic API Privacy * Name privacy in logs * update todo * remove CheckReadMemberPermission * Added name privacy to log embed * update todo * Update todo * Update api to handle privacy * update todo * Update systemlist full to respect privacy (as well as system list) * include colour as option for member privacy subject * move todo file (why was it there?) * Update TODO.md * Update TODO.md * Update TODO.md * Deleted to create pr * Update command usage and add to the command tree * Make api respect created privacy * Add editing privacy through the api * Fix pronoun privacy field in api * Fix info leak of display name in api * deprecate privacy field in api * Deprecate privacy diffrently * Update API * Update documentation * Update documentation * Remove comment in yml * Update userguide * Update migration (fix typo in 5.sql too) * Sanatize names * some full stops * Fix after merge * update migration * update schema version * update edit command * update privacy filter * fix a dumb mistake * clarify on what name privacy does * make it easier on someone else * Update docs * Comment out unused code * Add aliases for `member privacy all public` and `member privacy all private`
2020-06-17 19:31:39 +00:00
// --- For when this gets added to the member object itself or however they get added
// if (member.LastMessage != null && member.MetadataPrivacy.CanAccess(ctx)) eb.AddField("Last message:" FormatTimestamp(DiscordUtils.SnowflakeToInstant(m.LastMessage.Value)));
// if (member.LastSwitchTime != null && m.MetadataPrivacy.CanAccess(ctx)) eb.AddField("Last switched in:", FormatTimestamp(member.LastSwitchTime.Value));
// if (!member.Color.EmptyOrNull() && member.ColorPrivacy.CanAccess(ctx)) eb.AddField("Color", $"#{member.Color}", true);
if (!member.Color.EmptyOrNull()) eb.Field(new("Color", $"#{member.Color}", true));
2020-08-16 10:10:54 +00:00
if (groups.Count > 0)
{
// More than 5 groups show in "compact" format without ID
var content = groups.Count > 5
? string.Join(", ", groups.Select(g => g.DisplayName ?? g.Name))
: string.Join("\n", groups.Select(g => $"[`{g.Hid}`] **{g.DisplayName ?? g.Name}**"));
eb.Field(new($"Groups ({groups.Count})", content.Truncate(1000)));
2020-08-16 10:10:54 +00:00
}
if (member.DescriptionFor(ctx) is {} desc)
eb.Field(new("Description", member.Description.NormalizeLineEndSpacing(), false));
2019-05-11 22:44:02 +00:00
2020-11-22 16:57:54 +00:00
return eb.Build();
}
public async Task<Embed> CreateGroupEmbed(Context ctx, PKSystem system, PKGroup target)
2020-11-22 16:57:54 +00:00
{
await using var conn = await _db.Obtain();
var pctx = ctx.LookupContextFor(system);
var memberCount = ctx.MatchPrivateFlag(pctx) ? await _repo.GetGroupMemberCount(conn, target.Id, PrivacyLevel.Public) : await _repo.GetGroupMemberCount(conn, target.Id);
var nameField = target.Name;
if (system.Name != null)
nameField = $"{nameField} ({system.Name})";
2021-03-28 10:02:41 +00:00
uint color;
try
{
color = target.Color?.ToDiscordColor() ?? DiscordUtils.Gray;
}
catch (ArgumentException)
{
// There's no API for group colors yet, but defaulting to a blank color regardless
color = DiscordUtils.Gray;
}
var eb = new EmbedBuilder()
.Author(new(nameField, IconUrl: DiscordUtils.WorkaroundForUrlBug(target.IconFor(pctx))))
2021-03-28 10:02:41 +00:00
.Color(color)
.Footer(new($"System ID: {system.Hid} | Group ID: {target.Hid} | Created on {target.Created.FormatZoned(system)}"));
2020-11-22 16:57:54 +00:00
if (target.DisplayName != null)
2021-03-28 17:22:45 +00:00
eb.Field(new("Display Name", target.DisplayName, true));
if (!target.Color.EmptyOrNull()) eb.Field(new("Color", $"#{target.Color}", true));
2020-11-22 16:57:54 +00:00
if (target.ListPrivacy.CanAccess(pctx))
{
if (memberCount == 0 && pctx == LookupContext.ByOwner)
// Only suggest the add command if this is actually the owner lol
2021-03-28 17:22:45 +00:00
eb.Field(new("Members (0)", $"Add one with `pk;group {target.Reference()} add <member>`!", false));
2020-11-22 16:57:54 +00:00
else
2021-03-28 17:22:45 +00:00
eb.Field(new($"Members ({memberCount})", $"(see `pk;group {target.Reference()} list`)", false));
2020-11-22 16:57:54 +00:00
}
if (target.DescriptionFor(pctx) is { } desc)
eb.Field(new("Description", desc));
2020-11-22 16:57:54 +00:00
if (target.IconFor(pctx) is {} icon)
eb.Thumbnail(new(icon));
2020-11-22 16:57:54 +00:00
2019-05-11 22:44:02 +00:00
return eb.Build();
}
2019-06-15 10:19:44 +00:00
public async Task<Embed> CreateFronterEmbed(PKSwitch sw, DateTimeZone zone, LookupContext ctx)
2019-06-15 10:19:44 +00:00
{
2020-08-29 11:46:27 +00:00
var members = await _db.Execute(c => _repo.GetSwitchMembers(c, sw.Id).ToListAsync().AsTask());
2019-06-15 10:19:44 +00:00
var timeSinceSwitch = SystemClock.Instance.GetCurrentInstant() - sw.Timestamp;
return new EmbedBuilder()
2021-01-15 10:29:43 +00:00
.Color(members.FirstOrDefault()?.Color?.ToDiscordColor() ?? DiscordUtils.Gray)
.Field(new($"Current {"fronter".ToQuantity(members.Count, ShowQuantityAs.None)}", members.Count > 0 ? string.Join(", ", members.Select(m => m.NameFor(ctx))) : "*(no fronter)*"))
.Field(new("Since", $"{sw.Timestamp.FormatZoned(zone)} ({timeSinceSwitch.FormatDuration()} ago)"))
2019-06-15 10:19:44 +00:00
.Build();
}
2020-12-25 12:58:45 +00:00
public async Task<Embed> CreateMessageInfoEmbed(FullMessage msg)
{
2020-12-25 12:58:45 +00:00
var channel = await _cache.GetOrFetchChannel(_rest, msg.Message.Channel);
var ctx = LookupContext.ByNonOwner;
2021-01-31 16:56:33 +00:00
Message serverMsg = null;
try
{
serverMsg = await _rest.GetMessage(msg.Message.Channel, msg.Message.Mid);
}
catch (ForbiddenException)
{
// no permission, couldn't fetch, oh well
}
// Need this whole dance to handle cases where:
// - the user is deleted (userInfo == null)
// - the bot's no longer in the server we're querying (channel == null)
// - the member is no longer in the server we're querying (memberInfo == null)
2020-12-25 12:58:45 +00:00
// TODO: optimize ordering here a bit with new cache impl; and figure what happens if bot leaves server -> channel still cached -> hits this bit and 401s?
GuildMemberPartial memberInfo = null;
User userInfo = null;
if (channel != null)
{
2021-01-31 16:56:33 +00:00
GuildMember member = null;
try
{
member = await _rest.GetGuildMember(channel.GuildId!.Value, msg.Message.Sender);
}
catch (ForbiddenException)
{
// no permission, couldn't fetch, oh well
}
if (member != null)
2020-12-25 12:58:45 +00:00
// Don't do an extra request if we already have this info from the member lookup
2021-01-31 16:56:33 +00:00
userInfo = member.User;
memberInfo = member;
2020-12-25 12:58:45 +00:00
}
else userInfo = await _cache.GetOrFetchUser(_rest, msg.Message.Sender);
// Calculate string displayed under "Sent by"
string userStr;
2020-12-25 12:58:45 +00:00
if (memberInfo != null && memberInfo.Nick != null)
userStr = $"**Username:** {userInfo.NameAndMention()}\n**Nickname:** {memberInfo.Nick}";
else if (userInfo != null) userStr = userInfo.NameAndMention();
else userStr = $"*(deleted user {msg.Message.Sender})*";
// Put it all together
var eb = new EmbedBuilder()
.Author(new(msg.Member.NameFor(ctx), IconUrl: DiscordUtils.WorkaroundForUrlBug(msg.Member.AvatarFor(ctx))))
.Description(serverMsg?.Content?.NormalizeLineEndSpacing() ?? "*(message contents deleted or inaccessible)*")
.Image(new(serverMsg?.Attachments?.FirstOrDefault()?.Url))
.Field(new("System",
msg.System.Name != null ? $"{msg.System.Name} (`{msg.System.Hid}`)" : $"`{msg.System.Hid}`", true))
.Field(new("Member", $"{msg.Member.NameFor(ctx)} (`{msg.Member.Hid}`)", true))
.Field(new("Sent by", userStr, true))
.Timestamp(DiscordUtils.SnowflakeToInstant(msg.Message.Mid).ToDateTimeOffset().ToString("O"));
var roles = memberInfo?.Roles?.ToList();
if (roles != null && roles.Count > 0)
{
2020-12-25 12:58:45 +00:00
// TODO: what if role isn't in cache? figure out a fallback
var rolesString = string.Join(", ", roles.Select(id => _cache.GetRole(id).Name));
eb.Field(new($"Account roles ({roles.Count})", rolesString.Truncate(1024)));
}
return eb.Build();
}
2019-06-30 21:41:01 +00:00
public Task<Embed> CreateFrontPercentEmbed(FrontBreakdown breakdown, PKSystem system, PKGroup group, DateTimeZone tz, LookupContext ctx, string embedTitle, bool ignoreNoFronters, bool showFlat)
2019-06-30 21:41:01 +00:00
{
string color = system.Color;
if (group != null)
{
color = group.Color;
}
uint embedColor;
try
{
embedColor = color?.ToDiscordColor() ?? DiscordUtils.Gray;
}
catch (ArgumentException)
{
embedColor = DiscordUtils.Gray;
}
var eb = new EmbedBuilder()
.Title(embedTitle)
.Color(embedColor);
string footer = $"Since {breakdown.RangeStart.FormatZoned(tz)} ({(breakdown.RangeEnd - breakdown.RangeStart).FormatDuration()} ago)";
Duration period;
if (showFlat)
{
period = Duration.FromTicks(breakdown.MemberSwitchDurations.Values.ToList().Sum(i => i.TotalTicks));
footer += ". Showing flat list (percentages add up to 100%)";
if (!ignoreNoFronters) period += breakdown.NoFronterDuration;
else footer += ", ignoring switch-outs";
}
else if (ignoreNoFronters)
{
period = breakdown.RangeEnd - breakdown.RangeStart - breakdown.NoFronterDuration;
footer += ". Ignoring switch-outs";
}
else
period = breakdown.RangeEnd - breakdown.RangeStart;
eb.Footer(new(footer));
2019-06-30 21:41:01 +00:00
var maxEntriesToDisplay = 24; // max 25 fields allowed in embed - reserve 1 for "others"
// We convert to a list of pairs so we can add the no-fronter value
// Dictionary doesn't allow for null keys so we instead have a pair with a null key ;)
var pairs = breakdown.MemberSwitchDurations.ToList();
if (breakdown.NoFronterDuration != Duration.Zero && !ignoreNoFronters)
pairs.Add(new KeyValuePair<PKMember, Duration>(null, breakdown.NoFronterDuration));
var membersOrdered = pairs.OrderByDescending(pair => pair.Value).Take(maxEntriesToDisplay).ToList();
2019-06-30 21:41:01 +00:00
foreach (var pair in membersOrdered)
{
2021-06-21 14:52:06 +00:00
var frac = pair.Value / period;
eb.Field(new(pair.Key?.NameFor(ctx) ?? "*(no fronter)*", $"{frac*100:F0}% ({pair.Value.FormatDuration()})"));
2019-06-30 21:41:01 +00:00
}
if (membersOrdered.Count > maxEntriesToDisplay)
{
eb.Field(new("(others)",
membersOrdered.Skip(maxEntriesToDisplay)
.Aggregate(Duration.Zero, (prod, next) => prod + next.Value)
.FormatDuration(), true));
2019-06-30 21:41:01 +00:00
}
return Task.FromResult(eb.Build());
2019-06-30 21:41:01 +00:00
}
}
}