Refactor system list command

This commit is contained in:
Ske 2020-02-13 16:35:50 +01:00
parent 48342a2890
commit 883796de16
2 changed files with 73 additions and 89 deletions

View File

@ -172,12 +172,7 @@ namespace PluralKit.Bot
else if (ctx.Match("proxy")) else if (ctx.Match("proxy"))
await ctx.Execute<SystemEdit>(SystemProxy, m => m.SystemProxy(ctx)); await ctx.Execute<SystemEdit>(SystemProxy, m => m.SystemProxy(ctx));
else if (ctx.Match("list", "l", "members")) else if (ctx.Match("list", "l", "members"))
{ await ctx.Execute<SystemList>(SystemList, m => m.MemberList(ctx, ctx.System));
if (ctx.Match("f", "full", "big", "details", "long"))
await ctx.Execute<SystemList>(SystemList, m => m.MemberLongList(ctx, ctx.System));
else
await ctx.Execute<SystemList>(SystemList, m => m.MemberShortList(ctx, ctx.System));
}
else if (ctx.Match("f", "front", "fronter", "fronters")) else if (ctx.Match("f", "front", "fronter", "fronters"))
{ {
if (ctx.Match("h", "history")) if (ctx.Match("h", "history"))
@ -212,12 +207,7 @@ namespace PluralKit.Bot
$"{Emojis.Error} {await CreateSystemNotFoundError(ctx)}\n\nPerhaps you meant to use one of the following commands?\n{list}"); $"{Emojis.Error} {await CreateSystemNotFoundError(ctx)}\n\nPerhaps you meant to use one of the following commands?\n{list}");
} }
else if (ctx.Match("list", "l", "members")) else if (ctx.Match("list", "l", "members"))
{ await ctx.Execute<SystemList>(SystemList, m => m.MemberList(ctx, target));
if (ctx.Match("f", "full", "big", "details", "long"))
await ctx.Execute<SystemList>(SystemList, m => m.MemberLongList(ctx, target));
else
await ctx.Execute<SystemList>(SystemList, m => m.MemberShortList(ctx, target));
}
else if (ctx.Match("f", "front", "fronter", "fronters")) else if (ctx.Match("f", "front", "fronter", "fronters"))
{ {
if (ctx.Match("h", "history")) if (ctx.Match("h", "history"))

View File

@ -1,7 +1,10 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord;
using Humanizer; using Humanizer;
using PluralKit.Core; using PluralKit.Core;
@ -17,101 +20,92 @@ namespace PluralKit.Bot
_data = data; _data = data;
} }
public async Task MemberShortList(Context ctx, PKSystem system) { private async Task RenderMemberList(Context ctx, PKSystem system, bool canShowPrivate, int membersPerPage, string embedTitle, Func<PKMember, bool> filter,
if (system == null) throw Errors.NoSystemError; Func<EmbedBuilder, IEnumerable<PKMember>, Task>
ctx.CheckSystemPrivacy(system, system.MemberListPrivacy); renderer)
{
var authCtx = ctx.LookupContextFor(system); var authCtx = ctx.LookupContextFor(system);
var shouldShowPrivate = authCtx == LookupContext.ByOwner && ctx.Match("all", "everyone", "private"); var shouldShowPrivate = authCtx == LookupContext.ByOwner && canShowPrivate;
var embedTitle = system.Name != null ? $"Members of {system.Name.SanitizeMentions()} (`{system.Hid}`)" : $"Members of `{system.Hid}`"; var membersToShow = await _data.GetSystemMembers(system)
.Where(filter)
.OrderBy(m => m.Name, StringComparer.InvariantCultureIgnoreCase)
.ToListAsync();
var memberCountPublic = _data.GetSystemMemberCount(system, false); var membersToShowWithPrivacy = membersToShow
var memberCountAll = _data.GetSystemMemberCount(system, true);
await Task.WhenAll(memberCountPublic, memberCountAll);
var memberCountDisplayed = shouldShowPrivate ? memberCountAll.Result : memberCountPublic.Result;
var members = _data.GetSystemMembers(system)
.Where(m => m.MemberPrivacy == PrivacyLevel.Public || shouldShowPrivate) .Where(m => m.MemberPrivacy == PrivacyLevel.Public || shouldShowPrivate)
.OrderBy(m => m.Name, StringComparer.InvariantCultureIgnoreCase); .ToList();
var anyMembersHidden = !shouldShowPrivate && memberCountPublic.Result != memberCountAll.Result;
var anyMembersHidden = !shouldShowPrivate && membersToShowWithPrivacy.Count != membersToShow.Count;
await ctx.Paginate( await ctx.Paginate(
members, membersToShowWithPrivacy.ToAsyncEnumerable(),
memberCountDisplayed, membersToShowWithPrivacy.Count,
25, membersPerPage,
embedTitle, embedTitle,
(eb, ms) => (eb, ms) =>
{ {
eb.Description = string.Join("\n", ms.Select((m) => var footer = $"{membersToShowWithPrivacy.Count} total.";
{
if (m.HasProxyTags)
{
var proxyTagsString = m.ProxyTagsString().SanitizeMentions();
if (proxyTagsString.Length > 100) // arbitrary threshold for now, tweak?
proxyTagsString = "tags too long, see member card";
return $"[`{m.Hid}`] **{m.Name.SanitizeMentions()}** *({proxyTagsString})*";
}
return $"[`{m.Hid}`] **{m.Name.SanitizeMentions()}**";
}));
var footer = $"{memberCountDisplayed} total.";
if (anyMembersHidden && authCtx == LookupContext.ByOwner) if (anyMembersHidden && authCtx == LookupContext.ByOwner)
footer += " Private members have been hidden. type \"pk;system list all\" to include them."; footer += " Private members have been hidden. Add \"all\" to the command to include them.";
eb.WithFooter(footer); eb.WithFooter(footer);
return Task.CompletedTask; return renderer(eb, ms);
}); });
} }
public async Task MemberLongList(Context ctx, PKSystem system) { private Task ShortRenderer(EmbedBuilder eb, IEnumerable<PKMember> members)
{
eb.Description = string.Join("\n", members.Select((m) =>
{
if (m.HasProxyTags)
{
var proxyTagsString = m.ProxyTagsString().SanitizeMentions();
if (proxyTagsString.Length > 100) // arbitrary threshold for now, tweak?
proxyTagsString = "tags too long, see member card";
return $"[`{m.Hid}`] **{m.Name.SanitizeMentions()}** *({proxyTagsString})*";
}
return $"[`{m.Hid}`] **{m.Name.SanitizeMentions()}**";
}));
return Task.CompletedTask;
}
private Task LongRenderer(EmbedBuilder eb, IEnumerable<PKMember> members)
{
foreach (var m in members)
{
var profile = $"**ID**: {m.Hid}";
if (m.Pronouns != null) profile += $"\n**Pronouns**: {m.Pronouns}";
if (m.Birthday != null) profile += $"\n**Birthdate**: {m.BirthdayString}";
if (m.ProxyTags.Count > 0) profile += $"\n**Proxy tags:** {m.ProxyTagsString()}";
if (m.Description != null) profile += $"\n\n{m.Description}";
if (m.MemberPrivacy == PrivacyLevel.Private)
profile += "\n*(this member is private)*";
eb.AddField(m.Name, profile.Truncate(1024));
}
return Task.CompletedTask;
}
public async Task MemberList(Context ctx, PKSystem system)
{
if (system == null) throw Errors.NoSystemError; if (system == null) throw Errors.NoSystemError;
ctx.CheckSystemPrivacy(system, system.MemberListPrivacy); ctx.CheckSystemPrivacy(system, system.MemberListPrivacy);
var authCtx = ctx.LookupContextFor(system); var embedTitle = system.Name != null
var shouldShowPrivate = authCtx == LookupContext.ByOwner && ctx.Match("all", "everyone", "private"); ? $"Members of {system.Name.SanitizeMentions()} (`{system.Hid}`)"
: $"Members of `{system.Hid}`";
var embedTitle = system.Name != null ? $"Members of {system.Name} (`{system.Hid}`)" : $"Members of `{system.Hid}`"; var shouldShowLongList = ctx.Match("f", "full", "big", "details", "long");
var canShowPrivate = ctx.Match("a", "all", "everyone", "private");
var memberCountPublic = _data.GetSystemMemberCount(system, false); if (shouldShowLongList)
var memberCountAll = _data.GetSystemMemberCount(system, true); await RenderMemberList(ctx, system, canShowPrivate, 5, embedTitle, _ => true, LongRenderer);
await Task.WhenAll(memberCountPublic, memberCountAll); else
await RenderMemberList(ctx, system, canShowPrivate, 25, embedTitle, _ => true, ShortRenderer);
var memberCountDisplayed = shouldShowPrivate ? memberCountAll.Result : memberCountPublic.Result;
var members = _data.GetSystemMembers(system)
.Where(m => m.MemberPrivacy == PrivacyLevel.Public || shouldShowPrivate)
.OrderBy(m => m.Name, StringComparer.InvariantCultureIgnoreCase);
var anyMembersHidden = !shouldShowPrivate && memberCountPublic.Result != memberCountAll.Result;
await ctx.Paginate(
members,
memberCountDisplayed,
5,
embedTitle,
(eb, ms) => {
foreach (var m in ms) {
var profile = $"**ID**: {m.Hid}";
if (m.Pronouns != null) profile += $"\n**Pronouns**: {m.Pronouns}";
if (m.Birthday != null) profile += $"\n**Birthdate**: {m.BirthdayString}";
if (m.ProxyTags.Count > 0) profile += $"\n**Proxy tags:** {m.ProxyTagsString()}";
if (m.Description != null) profile += $"\n\n{m.Description}";
if (m.MemberPrivacy == PrivacyLevel.Private)
profile += "*(this member is private)*";
eb.AddField(m.Name, profile.Truncate(1024));
}
var footer = $"{memberCountDisplayed} total.";
if (anyMembersHidden && authCtx == LookupContext.ByOwner)
footer += " Private members have been hidden. type \"pk;system list full all\" to include them.";
eb.WithFooter(footer);
return Task.CompletedTask;
}
);
} }
} }
} }