From 8a28d836c73f152dfe23d99e5f6f5f4b915f3b7f Mon Sep 17 00:00:00 2001 From: Ske Date: Tue, 7 Jul 2020 19:34:23 +0200 Subject: [PATCH] Add group member list command --- PluralKit.Bot/Commands/CommandTree.cs | 3 +++ PluralKit.Bot/Commands/Groups.cs | 22 +++++++++++++++++++ .../Commands/Lists/MemberListOptions.cs | 4 +++- .../Database/Views/DatabaseViewsExt.cs | 9 ++++++-- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/PluralKit.Bot/Commands/CommandTree.cs b/PluralKit.Bot/Commands/CommandTree.cs index dcc5904a..f219ea18 100644 --- a/PluralKit.Bot/Commands/CommandTree.cs +++ b/PluralKit.Bot/Commands/CommandTree.cs @@ -48,6 +48,7 @@ namespace PluralKit.Bot public static Command GroupInfo = new Command("group", "group ", "Looks up information about a group"); public static Command GroupNew = new Command("group new", "group new ", "Creates a new group"); public static Command GroupList = new Command("group list", "group list", "Lists all groups in this system"); + public static Command GroupMemberList = new Command("group members", "group list", "Lists all members in a group"); public static Command GroupRename = new Command("group rename", "group name ", "Renames a group"); public static Command GroupDesc = new Command("group description", "group description [description]", "Changes a group's description"); public static Command GroupAdd = new Command("group add", "group add [member 2] [member 3...]", "Adds one or more members to a group"); @@ -339,6 +340,8 @@ namespace PluralKit.Bot await ctx.Execute(GroupAdd,g => g.AddRemoveMembers(ctx, target, Groups.AddRemoveOperation.Add)); else if (ctx.Match("remove", "rem", "r")) await ctx.Execute(GroupRemove, g => g.AddRemoveMembers(ctx, target, Groups.AddRemoveOperation.Remove)); + else if (ctx.Match("members", "list", "ms", "l")) + await ctx.Execute(GroupMemberList, g => g.ListGroupMembers(ctx, target)); else if (!ctx.HasNext()) await ctx.Execute(GroupInfo, g => g.ShowGroupCard(ctx, target)); else diff --git a/PluralKit.Bot/Commands/Groups.cs b/PluralKit.Bot/Commands/Groups.cs index a0e32ded..8c1631d8 100644 --- a/PluralKit.Bot/Commands/Groups.cs +++ b/PluralKit.Bot/Commands/Groups.cs @@ -5,6 +5,8 @@ using System.Threading.Tasks; using DSharpPlus.Entities; +using NodaTime; + using PluralKit.Core; namespace PluralKit.Bot @@ -176,6 +178,26 @@ namespace PluralKit.Bot } } + public async Task ListGroupMembers(Context ctx, PKGroup target) + { + await using var conn = await _db.Obtain(); + var targetSystem = await GetGroupSystem(ctx, target, conn); + ctx.CheckSystemPrivacy(targetSystem, targetSystem.MemberListPrivacy); + + var opts = ctx.ParseMemberListOptions(ctx.LookupContextFor(target.System)); + opts.GroupFilter = target.Id; + + var title = new StringBuilder($"Members of {target.Name} (`{target.Hid}`) in "); + if (targetSystem.Name != null) + title.Append($"{targetSystem.Name} (`{targetSystem.Hid}`)"); + else + title.Append($"`{targetSystem.Hid}`"); + if (opts.Search != null) + title.Append($" matching **{opts.Search}**"); + + await ctx.RenderMemberList(ctx.LookupContextFor(target.System), _db, target.System, title.ToString(), opts); + } + public enum AddRemoveOperation { Add, diff --git a/PluralKit.Bot/Commands/Lists/MemberListOptions.cs b/PluralKit.Bot/Commands/Lists/MemberListOptions.cs index 9e00b226..5708c6a5 100644 --- a/PluralKit.Bot/Commands/Lists/MemberListOptions.cs +++ b/PluralKit.Bot/Commands/Lists/MemberListOptions.cs @@ -16,6 +16,7 @@ namespace PluralKit.Bot public bool Reverse { get; set; } public PrivacyLevel? PrivacyFilter { get; set; } = PrivacyLevel.Public; + public GroupId? GroupFilter { get; set; } public string? Search { get; set; } public bool SearchDescription { get; set; } @@ -55,7 +56,7 @@ namespace PluralKit.Bot PrivacyLevel.Public => "", // (default, no extra line needed) _ => new ArgumentOutOfRangeException($"Couldn't find readable string for privacy filter {PrivacyFilter}") }); - + return str.ToString(); } @@ -63,6 +64,7 @@ namespace PluralKit.Bot new DatabaseViewsExt.MemberListQueryOptions { PrivacyFilter = PrivacyFilter, + GroupFilter = GroupFilter, Search = Search, SearchDescription = SearchDescription }; diff --git a/PluralKit.Core/Database/Views/DatabaseViewsExt.cs b/PluralKit.Core/Database/Views/DatabaseViewsExt.cs index 5a0ef688..c399a28d 100644 --- a/PluralKit.Core/Database/Views/DatabaseViewsExt.cs +++ b/PluralKit.Core/Database/Views/DatabaseViewsExt.cs @@ -14,7 +14,11 @@ namespace PluralKit.Core public static Task> QueryMemberList(this IPKConnection conn, SystemId system, MemberListQueryOptions opts) { - StringBuilder query = new StringBuilder("select * from member_list where system = @system"); + StringBuilder query; + if (opts.GroupFilter == null) + query = new StringBuilder("select * from member_list where system = @system"); + else + query = new StringBuilder("select member_list.* from group_members inner join member_list on member_list.id = group_members.member_id where group_id = @groupFilter"); if (opts.PrivacyFilter != null) query.Append($" and member_visibility = {(int) opts.PrivacyFilter}"); @@ -35,7 +39,7 @@ namespace PluralKit.Core query.Append(")"); } - return conn.QueryAsync(query.ToString(), new {system, filter = opts.Search}); + return conn.QueryAsync(query.ToString(), new {system, filter = opts.Search, groupFilter = opts.GroupFilter}); } public struct MemberListQueryOptions @@ -44,6 +48,7 @@ namespace PluralKit.Core public string? Search; public bool SearchDescription; public LookupContext Context; + public GroupId? GroupFilter; } } } \ No newline at end of file