Add group member list command

This commit is contained in:
Ske 2020-07-07 19:34:23 +02:00
parent 0f4c40b344
commit 8a28d836c7
4 changed files with 35 additions and 3 deletions

View File

@ -48,6 +48,7 @@ namespace PluralKit.Bot
public static Command GroupInfo = new Command("group", "group <name>", "Looks up information about a group"); public static Command GroupInfo = new Command("group", "group <name>", "Looks up information about a group");
public static Command GroupNew = new Command("group new", "group new <name>", "Creates a new group"); public static Command GroupNew = new Command("group new", "group new <name>", "Creates a new group");
public static Command GroupList = new Command("group list", "group list", "Lists all groups in this system"); 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 <group> list", "Lists all members in a group");
public static Command GroupRename = new Command("group rename", "group <group> name <new name>", "Renames a group"); public static Command GroupRename = new Command("group rename", "group <group> name <new name>", "Renames a group");
public static Command GroupDesc = new Command("group description", "group <group> description [description]", "Changes a group's description"); public static Command GroupDesc = new Command("group description", "group <group> description [description]", "Changes a group's description");
public static Command GroupAdd = new Command("group add", "group <group> add <member> [member 2] [member 3...]", "Adds one or more members to a group"); public static Command GroupAdd = new Command("group add", "group <group> add <member> [member 2] [member 3...]", "Adds one or more members to a group");
@ -339,6 +340,8 @@ namespace PluralKit.Bot
await ctx.Execute<Groups>(GroupAdd,g => g.AddRemoveMembers(ctx, target, Groups.AddRemoveOperation.Add)); await ctx.Execute<Groups>(GroupAdd,g => g.AddRemoveMembers(ctx, target, Groups.AddRemoveOperation.Add));
else if (ctx.Match("remove", "rem", "r")) else if (ctx.Match("remove", "rem", "r"))
await ctx.Execute<Groups>(GroupRemove, g => g.AddRemoveMembers(ctx, target, Groups.AddRemoveOperation.Remove)); await ctx.Execute<Groups>(GroupRemove, g => g.AddRemoveMembers(ctx, target, Groups.AddRemoveOperation.Remove));
else if (ctx.Match("members", "list", "ms", "l"))
await ctx.Execute<Groups>(GroupMemberList, g => g.ListGroupMembers(ctx, target));
else if (!ctx.HasNext()) else if (!ctx.HasNext())
await ctx.Execute<Groups>(GroupInfo, g => g.ShowGroupCard(ctx, target)); await ctx.Execute<Groups>(GroupInfo, g => g.ShowGroupCard(ctx, target));
else else

View File

@ -5,6 +5,8 @@ using System.Threading.Tasks;
using DSharpPlus.Entities; using DSharpPlus.Entities;
using NodaTime;
using PluralKit.Core; using PluralKit.Core;
namespace PluralKit.Bot 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 public enum AddRemoveOperation
{ {
Add, Add,

View File

@ -16,6 +16,7 @@ namespace PluralKit.Bot
public bool Reverse { get; set; } public bool Reverse { get; set; }
public PrivacyLevel? PrivacyFilter { get; set; } = PrivacyLevel.Public; public PrivacyLevel? PrivacyFilter { get; set; } = PrivacyLevel.Public;
public GroupId? GroupFilter { get; set; }
public string? Search { get; set; } public string? Search { get; set; }
public bool SearchDescription { get; set; } public bool SearchDescription { get; set; }
@ -63,6 +64,7 @@ namespace PluralKit.Bot
new DatabaseViewsExt.MemberListQueryOptions new DatabaseViewsExt.MemberListQueryOptions
{ {
PrivacyFilter = PrivacyFilter, PrivacyFilter = PrivacyFilter,
GroupFilter = GroupFilter,
Search = Search, Search = Search,
SearchDescription = SearchDescription SearchDescription = SearchDescription
}; };

View File

@ -14,7 +14,11 @@ namespace PluralKit.Core
public static Task<IEnumerable<ListedMember>> QueryMemberList(this IPKConnection conn, SystemId system, MemberListQueryOptions opts) public static Task<IEnumerable<ListedMember>> 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) if (opts.PrivacyFilter != null)
query.Append($" and member_visibility = {(int) opts.PrivacyFilter}"); query.Append($" and member_visibility = {(int) opts.PrivacyFilter}");
@ -35,7 +39,7 @@ namespace PluralKit.Core
query.Append(")"); query.Append(")");
} }
return conn.QueryAsync<ListedMember>(query.ToString(), new {system, filter = opts.Search}); return conn.QueryAsync<ListedMember>(query.ToString(), new {system, filter = opts.Search, groupFilter = opts.GroupFilter});
} }
public struct MemberListQueryOptions public struct MemberListQueryOptions
@ -44,6 +48,7 @@ namespace PluralKit.Core
public string? Search; public string? Search;
public bool SearchDescription; public bool SearchDescription;
public LookupContext Context; public LookupContext Context;
public GroupId? GroupFilter;
} }
} }
} }