Refactor system list command
This commit is contained in:
parent
48342a2890
commit
883796de16
@ -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"))
|
||||||
|
@ -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,34 +20,43 @@ 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 (anyMembersHidden && authCtx == LookupContext.ByOwner)
|
||||||
|
footer += " Private members have been hidden. Add \"all\" to the command to include them.";
|
||||||
|
eb.WithFooter(footer);
|
||||||
|
|
||||||
|
return renderer(eb, ms);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private Task ShortRenderer(EmbedBuilder eb, IEnumerable<PKMember> members)
|
||||||
|
{
|
||||||
|
eb.Description = string.Join("\n", members.Select((m) =>
|
||||||
{
|
{
|
||||||
if (m.HasProxyTags)
|
if (m.HasProxyTags)
|
||||||
{
|
{
|
||||||
@ -58,60 +70,42 @@ namespace PluralKit.Bot
|
|||||||
return $"[`{m.Hid}`] **{m.Name.SanitizeMentions()}**";
|
return $"[`{m.Hid}`] **{m.Name.SanitizeMentions()}**";
|
||||||
}));
|
}));
|
||||||
|
|
||||||
var footer = $"{memberCountDisplayed} total.";
|
|
||||||
if (anyMembersHidden && authCtx == LookupContext.ByOwner)
|
|
||||||
footer += " Private members have been hidden. type \"pk;system list all\" to include them.";
|
|
||||||
eb.WithFooter(footer);
|
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task MemberLongList(Context ctx, PKSystem system) {
|
private Task LongRenderer(EmbedBuilder eb, IEnumerable<PKMember> members)
|
||||||
if (system == null) throw Errors.NoSystemError;
|
{
|
||||||
ctx.CheckSystemPrivacy(system, system.MemberListPrivacy);
|
foreach (var m in members)
|
||||||
|
{
|
||||||
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 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}";
|
var profile = $"**ID**: {m.Hid}";
|
||||||
if (m.Pronouns != null) profile += $"\n**Pronouns**: {m.Pronouns}";
|
if (m.Pronouns != null) profile += $"\n**Pronouns**: {m.Pronouns}";
|
||||||
if (m.Birthday != null) profile += $"\n**Birthdate**: {m.BirthdayString}";
|
if (m.Birthday != null) profile += $"\n**Birthdate**: {m.BirthdayString}";
|
||||||
if (m.ProxyTags.Count > 0) profile += $"\n**Proxy tags:** {m.ProxyTagsString()}";
|
if (m.ProxyTags.Count > 0) profile += $"\n**Proxy tags:** {m.ProxyTagsString()}";
|
||||||
if (m.Description != null) profile += $"\n\n{m.Description}";
|
if (m.Description != null) profile += $"\n\n{m.Description}";
|
||||||
if (m.MemberPrivacy == PrivacyLevel.Private)
|
if (m.MemberPrivacy == PrivacyLevel.Private)
|
||||||
profile += "*(this member is private)*";
|
profile += "\n*(this member is private)*";
|
||||||
|
|
||||||
eb.AddField(m.Name, profile.Truncate(1024));
|
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;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
public async Task MemberList(Context ctx, PKSystem system)
|
||||||
|
{
|
||||||
|
if (system == null) throw Errors.NoSystemError;
|
||||||
|
ctx.CheckSystemPrivacy(system, system.MemberListPrivacy);
|
||||||
|
|
||||||
|
var embedTitle = system.Name != null
|
||||||
|
? $"Members of {system.Name.SanitizeMentions()} (`{system.Hid}`)"
|
||||||
|
: $"Members of `{system.Hid}`";
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user