From 883796de161198f4ba5b77d192324b0aa186f052 Mon Sep 17 00:00:00 2001 From: Ske Date: Thu, 13 Feb 2020 16:35:50 +0100 Subject: [PATCH] Refactor system list command --- PluralKit.Bot/Commands/CommandTree.cs | 14 +-- PluralKit.Bot/Commands/SystemList.cs | 148 ++++++++++++-------------- 2 files changed, 73 insertions(+), 89 deletions(-) diff --git a/PluralKit.Bot/Commands/CommandTree.cs b/PluralKit.Bot/Commands/CommandTree.cs index 1ad239b6..aa7f095d 100644 --- a/PluralKit.Bot/Commands/CommandTree.cs +++ b/PluralKit.Bot/Commands/CommandTree.cs @@ -172,12 +172,7 @@ namespace PluralKit.Bot else if (ctx.Match("proxy")) await ctx.Execute(SystemProxy, m => m.SystemProxy(ctx)); else if (ctx.Match("list", "l", "members")) - { - if (ctx.Match("f", "full", "big", "details", "long")) - await ctx.Execute(SystemList, m => m.MemberLongList(ctx, ctx.System)); - else - await ctx.Execute(SystemList, m => m.MemberShortList(ctx, ctx.System)); - } + await ctx.Execute(SystemList, m => m.MemberList(ctx, ctx.System)); else if (ctx.Match("f", "front", "fronter", "fronters")) { 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}"); } else if (ctx.Match("list", "l", "members")) - { - if (ctx.Match("f", "full", "big", "details", "long")) - await ctx.Execute(SystemList, m => m.MemberLongList(ctx, target)); - else - await ctx.Execute(SystemList, m => m.MemberShortList(ctx, target)); - } + await ctx.Execute(SystemList, m => m.MemberList(ctx, target)); else if (ctx.Match("f", "front", "fronter", "fronters")) { if (ctx.Match("h", "history")) diff --git a/PluralKit.Bot/Commands/SystemList.cs b/PluralKit.Bot/Commands/SystemList.cs index 60c6422f..8f26b0ef 100644 --- a/PluralKit.Bot/Commands/SystemList.cs +++ b/PluralKit.Bot/Commands/SystemList.cs @@ -1,7 +1,10 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Discord; + using Humanizer; using PluralKit.Core; @@ -17,101 +20,92 @@ namespace PluralKit.Bot _data = data; } - public async Task MemberShortList(Context ctx, PKSystem system) { - if (system == null) throw Errors.NoSystemError; - ctx.CheckSystemPrivacy(system, system.MemberListPrivacy); - + private async Task RenderMemberList(Context ctx, PKSystem system, bool canShowPrivate, int membersPerPage, string embedTitle, Func filter, + Func, Task> + renderer) + { 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 memberCountAll = _data.GetSystemMemberCount(system, true); - await Task.WhenAll(memberCountPublic, memberCountAll); - - var memberCountDisplayed = shouldShowPrivate ? memberCountAll.Result : memberCountPublic.Result; - - var members = _data.GetSystemMembers(system) + var membersToShowWithPrivacy = membersToShow .Where(m => m.MemberPrivacy == PrivacyLevel.Public || shouldShowPrivate) - .OrderBy(m => m.Name, StringComparer.InvariantCultureIgnoreCase); - var anyMembersHidden = !shouldShowPrivate && memberCountPublic.Result != memberCountAll.Result; - + .ToList(); + + var anyMembersHidden = !shouldShowPrivate && membersToShowWithPrivacy.Count != membersToShow.Count; + await ctx.Paginate( - members, - memberCountDisplayed, - 25, + membersToShowWithPrivacy.ToAsyncEnumerable(), + membersToShowWithPrivacy.Count, + membersPerPage, embedTitle, (eb, ms) => { - eb.Description = string.Join("\n", ms.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()}**"; - })); - - var footer = $"{memberCountDisplayed} total."; + var footer = $"{membersToShowWithPrivacy.Count} total."; 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); - return Task.CompletedTask; + return renderer(eb, ms); }); } - public async Task MemberLongList(Context ctx, PKSystem system) { + private Task ShortRenderer(EmbedBuilder eb, IEnumerable 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 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; ctx.CheckSystemPrivacy(system, system.MemberListPrivacy); - - var authCtx = ctx.LookupContextFor(system); - var shouldShowPrivate = authCtx == LookupContext.ByOwner && ctx.Match("all", "everyone", "private"); - - var embedTitle = system.Name != null ? $"Members of {system.Name} (`{system.Hid}`)" : $"Members of `{system.Hid}`"; - var memberCountPublic = _data.GetSystemMemberCount(system, false); - var memberCountAll = _data.GetSystemMemberCount(system, true); - await Task.WhenAll(memberCountPublic, memberCountAll); + var embedTitle = system.Name != null + ? $"Members of {system.Name.SanitizeMentions()} (`{system.Hid}`)" + : $"Members of `{system.Hid}`"; - 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; - } - ); + var shouldShowLongList = ctx.Match("f", "full", "big", "details", "long"); + var canShowPrivate = ctx.Match("a", "all", "everyone", "private"); + if (shouldShowLongList) + await RenderMemberList(ctx, system, canShowPrivate, 5, embedTitle, _ => true, LongRenderer); + else + await RenderMemberList(ctx, system, canShowPrivate, 25, embedTitle, _ => true, ShortRenderer); } } } \ No newline at end of file