From ccac0a92033be289c9fd470f774651afd9f4fc19 Mon Sep 17 00:00:00 2001 From: Jake Fulmine Date: Sat, 27 Aug 2022 13:32:48 +0200 Subject: [PATCH] feat: add pk;random for other systems --- PluralKit.Bot/CommandMeta/CommandHelp.cs | 4 +-- PluralKit.Bot/CommandMeta/CommandTree.cs | 9 +++-- PluralKit.Bot/Commands/Random.cs | 45 +++++++++++++++++------- docs/content/command-list.md | 2 +- 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/PluralKit.Bot/CommandMeta/CommandHelp.cs b/PluralKit.Bot/CommandMeta/CommandHelp.cs index 235885f2..7cbb4082 100644 --- a/PluralKit.Bot/CommandMeta/CommandHelp.cs +++ b/PluralKit.Bot/CommandMeta/CommandHelp.cs @@ -51,7 +51,7 @@ public partial class CommandTree public static Command MemberServerName = new Command("member servername", "member servername [server name]", "Changes a member's display name in the current server"); public static Command MemberAutoproxy = new Command("member autoproxy", "member autoproxy [on|off]", "Sets whether a member will be autoproxied when autoproxy is set to latch or front mode."); public static Command MemberKeepProxy = new Command("member keepproxy", "member keepproxy [on|off]", "Sets whether to include a member's proxy tags when proxying"); - public static Command MemberRandom = new Command("random", "random", "Shows the info card of a randomly selected member in your system."); + public static Command MemberRandom = new Command("system random", "system [system] random", "Shows the info card of a randomly selected member in a system."); public static Command MemberPrivacy = new Command("member privacy", "member privacy ", "Changes a members's privacy settings"); 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"); @@ -69,7 +69,7 @@ public partial class CommandTree public static Command GroupDelete = new Command("group delete", "group delete", "Deletes a group"); public static Command GroupFrontPercent = new Command("group frontpercent", "group frontpercent [timespan]", "Shows a group's front breakdown."); public static Command GroupMemberRandom = new Command("group random", "group random", "Shows the info card of a randomly selected member in a group."); - public static Command GroupRandom = new Command("random", "random group", "Shows the info card of a randomly selected group in your system."); + public static Command GroupRandom = new Command("system random", "system [system] random group", "Shows the info card of a randomly selected group in a system."); public static Command Switch = new Command("switch", "switch [member 2] [member 3...]", "Registers a switch"); public static Command SwitchOut = new Command("switch out", "switch out", "Registers a switch with no members"); public static Command SwitchMove = new Command("switch move", "switch move ", "Moves the latest switch in time"); diff --git a/PluralKit.Bot/CommandMeta/CommandTree.cs b/PluralKit.Bot/CommandMeta/CommandTree.cs index b5b65dc9..233ec147 100644 --- a/PluralKit.Bot/CommandMeta/CommandTree.cs +++ b/PluralKit.Bot/CommandMeta/CommandTree.cs @@ -100,9 +100,9 @@ public partial class CommandTree return HandleAdminCommand(ctx); if (ctx.Match("random", "r")) if (ctx.Match("group", "g") || ctx.MatchFlag("group", "g")) - return ctx.Execute(GroupRandom, r => r.Group(ctx)); + return ctx.Execute(GroupRandom, r => r.Group(ctx, ctx.System)); else - return ctx.Execute(MemberRandom, m => m.Member(ctx)); + return ctx.Execute(MemberRandom, m => m.Member(ctx, ctx.System)); // remove compiler warning return ctx.Reply( @@ -250,6 +250,11 @@ public partial class CommandTree await ctx.CheckSystem(target).Execute(SystemPrivacy, m => m.SystemPrivacy(ctx, target)); else if (ctx.Match("delete", "remove", "destroy", "erase", "yeet")) await ctx.CheckSystem(target).Execute(SystemDelete, m => m.Delete(ctx, target)); + else if (ctx.Match("random", "r")) + if (ctx.Match("group", "g") || ctx.MatchFlag("group", "g")) + await ctx.CheckSystem(target).Execute(GroupRandom, r => r.Group(ctx, target)); + else + await ctx.CheckSystem(target).Execute(MemberRandom, m => m.Member(ctx, target)); } private async Task HandleMemberCommand(Context ctx) diff --git a/PluralKit.Bot/Commands/Random.cs b/PluralKit.Bot/Commands/Random.cs index b7ea811d..dd898680 100644 --- a/PluralKit.Bot/Commands/Random.cs +++ b/PluralKit.Bot/Commands/Random.cs @@ -15,48 +15,58 @@ public class Random // todo: get postgresql to return one random member/group instead of querying all members/groups - public async Task Member(Context ctx) + public async Task Member(Context ctx, PKSystem target) { - ctx.CheckSystem(); + if (target == null) + throw Errors.NoSystemError; - var members = await ctx.Repository.GetSystemMembers(ctx.System.Id).ToListAsync(); + ctx.CheckSystemPrivacy(target.Id, target.MemberListPrivacy); + + var members = await ctx.Repository.GetSystemMembers(target.Id).ToListAsync(); if (!ctx.MatchFlag("all", "a")) members = members.Where(m => m.MemberVisibility == PrivacyLevel.Public).ToList(); + else + ctx.CheckOwnSystem(target); if (members == null || !members.Any()) throw new PKError( "Your system has no members! Please create at least one member before using this command."); var randInt = randGen.Next(members.Count); - await ctx.Reply(embed: await _embeds.CreateMemberEmbed(ctx.System, members[randInt], ctx.Guild, - ctx.LookupContextFor(ctx.System.Id), ctx.Zone)); + await ctx.Reply(embed: await _embeds.CreateMemberEmbed(target, members[randInt], ctx.Guild, + ctx.LookupContextFor(target.Id), ctx.Zone)); } - public async Task Group(Context ctx) + public async Task Group(Context ctx, PKSystem target) { - ctx.CheckSystem(); + if (target == null) + throw Errors.NoSystemError; + + ctx.CheckSystemPrivacy(target.Id, target.GroupListPrivacy); - var groups = await ctx.Repository.GetSystemGroups(ctx.System.Id).ToListAsync(); + var groups = await ctx.Repository.GetSystemGroups(target.Id).ToListAsync(); if (!ctx.MatchFlag("all", "a")) groups = groups.Where(g => g.Visibility == PrivacyLevel.Public).ToList(); + else + ctx.CheckOwnSystem(target); if (groups == null || !groups.Any()) throw new PKError( "Your system has no groups! Please create at least one group before using this command."); var randInt = randGen.Next(groups.Count()); - await ctx.Reply(embed: await _embeds.CreateGroupEmbed(ctx, ctx.System, groups.ToArray()[randInt])); + await ctx.Reply(embed: await _embeds.CreateGroupEmbed(ctx, target, groups.ToArray()[randInt])); } public async Task GroupMember(Context ctx, PKGroup group) { - ctx.CheckOwnGroup(group); + ctx.CheckSystemPrivacy(group.System, group.ListPrivacy); var opts = ctx.ParseListOptions(ctx.DirectLookupContextFor(group.System)); opts.GroupFilter = group.Id; - var members = await ctx.Database.Execute(conn => conn.QueryMemberList(ctx.System.Id, opts.ToQueryOptions())); + var members = await ctx.Database.Execute(conn => conn.QueryMemberList(group.System, opts.ToQueryOptions())); if (members == null || !members.Any()) throw new PKError( @@ -64,11 +74,20 @@ public class Random if (!ctx.MatchFlag("all", "a")) members = members.Where(g => g.MemberVisibility == PrivacyLevel.Public); + else + ctx.CheckOwnGroup(group); + var ms = members.ToList(); + PKSystem system; + if (ctx.System?.Id == group.System) + system = ctx.System; + else + system = await ctx.Repository.GetSystem(group.System); + var randInt = randGen.Next(ms.Count); - await ctx.Reply(embed: await _embeds.CreateMemberEmbed(ctx.System, ms[randInt], ctx.Guild, - ctx.LookupContextFor(ctx.System.Id), ctx.Zone)); + await ctx.Reply(embed: await _embeds.CreateMemberEmbed(system, ms[randInt], ctx.Guild, + ctx.LookupContextFor(group.System), ctx.Zone)); } } \ No newline at end of file diff --git a/docs/content/command-list.md b/docs/content/command-list.md index 02b94279..2c47711e 100644 --- a/docs/content/command-list.md +++ b/docs/content/command-list.md @@ -53,6 +53,7 @@ Some arguments indicate the use of specific Discord features. These include: - `pk;system [system] list -full` - Shows a paginated list of a system's members, with increased detail. - `pk;find ` - Searches members by name. - `pk;system [system] find ` - (same as above, but for a specific system) +- `pk;system [system] random [-group]` - Shows the info card of a randomly selected member [or group] in a system. ## Member commands *Replace `` with a member's name, 5-character ID or display name. For most commands, adding `-clear` will clear/delete the field.* @@ -127,7 +128,6 @@ Some arguments indicate the use of specific Discord features. These include: - `pk;blacklist remove <#channel> [#channel...]` - Removes the given channel(s) from the proxy blacklist. ## Utility -- `pk;random [-group]` - Shows the info card of a randomly selected member [or group] in your system. - `pk;message ` - Looks up information about a proxied message by its message ID or link. - `pk;invite` - Sends the bot invite link for PluralKit. - `pk;import` - Imports a data file from PluralKit or Tupperbox.