From c2b6e0eeedb49b06eb730006eb4521aa25b591af Mon Sep 17 00:00:00 2001 From: spiral Date: Thu, 8 Jul 2021 10:04:05 -0400 Subject: [PATCH] Add groups to `pk;admin` --- PluralKit.Bot/Commands/Admin.cs | 67 +++++++++++++++++++++++ PluralKit.Bot/Commands/CommandTree.cs | 4 ++ PluralKit.Core/Models/Patch/GroupPatch.cs | 2 + 3 files changed, 73 insertions(+) diff --git a/PluralKit.Bot/Commands/Admin.cs b/PluralKit.Bot/Commands/Admin.cs index 0e492cc2..b0ded394 100644 --- a/PluralKit.Bot/Commands/Admin.cs +++ b/PluralKit.Bot/Commands/Admin.cs @@ -77,6 +77,36 @@ namespace PluralKit.Bot await ctx.Reply($"{Emojis.Success} Member ID updated (`{target.Hid}` -> `{newHid}`)."); } + public async Task UpdateGroupId(Context ctx) + { + AssertBotAdmin(ctx); + + var target = await ctx.MatchGroup(); + if (target == null) + throw new PKError("Unknown group."); + + var newHid = ctx.PopArgument(); + if (!Regex.IsMatch(newHid, "^[a-z]{5}$")) + throw new PKError($"Invalid new group ID `{newHid}`."); + + var existingGroup = await _db.Execute(c => _repo.GetGroupByHid(c, newHid)); + if (existingGroup != null) + throw new PKError($"Another group already exists with ID `{newHid}`."); + + var prompt = new YesNoPrompt(ctx) + { + Message = $"Change group ID of **{target.Name}** (`{target.Hid}`) to `{newHid}`?", + AcceptLabel = "Change" + }; + await prompt.Run(); + + if (prompt.Result != true) + throw new PKError("ID change cancelled."); + + await _db.Execute(c => _repo.UpdateGroup(c, target.Id, new GroupPatch {Hid = newHid})); + await ctx.Reply($"{Emojis.Success} Group ID updated (`{target.Hid}` -> `{newHid}`)."); + } + public async Task SystemMemberLimit(Context ctx) { AssertBotAdmin(ctx); @@ -114,6 +144,43 @@ namespace PluralKit.Bot await ctx.Reply($"{Emojis.Success} Member limit updated."); } + public async Task SystemGroupLimit(Context ctx) + { + AssertBotAdmin(ctx); + + var target = await ctx.MatchSystem(); + if (target == null) + throw new PKError("Unknown system."); + + var currentLimit = target.GroupLimitOverride ?? Limits.MaxGroupCount; + if (!ctx.HasNext()) + { + await ctx.Reply($"Current group limit is **{currentLimit}** groups."); + return; + } + + var newLimitStr = ctx.PopArgument(); + if (!int.TryParse(newLimitStr, out var newLimit)) + throw new PKError($"Couldn't parse `{newLimitStr}` as number."); + + var prompt = new YesNoPrompt(ctx) + { + Message = $"Update group limit from **{currentLimit}** to **{newLimit}**?", + AcceptLabel = "Update" + }; + await prompt.Run(); + + if (prompt.Result != true) + throw new PKError("Group limit change cancelled."); + + await using var conn = await _db.Obtain(); + await _repo.UpdateSystem(conn, target.Id, new SystemPatch + { + GroupLimitOverride = newLimit + }); + await ctx.Reply($"{Emojis.Success} Group limit updated."); + } + private void AssertBotAdmin(Context ctx) { if (!IsBotAdmin(ctx)) diff --git a/PluralKit.Bot/Commands/CommandTree.cs b/PluralKit.Bot/Commands/CommandTree.cs index 51250527..2509c118 100644 --- a/PluralKit.Bot/Commands/CommandTree.cs +++ b/PluralKit.Bot/Commands/CommandTree.cs @@ -217,8 +217,12 @@ namespace PluralKit.Bot await ctx.Execute(Admin, a => a.UpdateSystemId(ctx)); else if (ctx.Match("umid", "updatememberid")) await ctx.Execute(Admin, a => a.UpdateMemberId(ctx)); + else if (ctx.Match("ugid", "updategroupid")) + await ctx.Execute(Admin, a => a.UpdateGroupId(ctx)); else if (ctx.Match("uml", "updatememberlimit")) await ctx.Execute(Admin, a => a.SystemMemberLimit(ctx)); + else if (ctx.Match("ugl", "updategrouplimit")) + await ctx.Execute(Admin, a => a.SystemGroupLimit(ctx)); else await ctx.Reply($"{Emojis.Error} Unknown command."); } diff --git a/PluralKit.Core/Models/Patch/GroupPatch.cs b/PluralKit.Core/Models/Patch/GroupPatch.cs index 933a3376..b6d7dc35 100644 --- a/PluralKit.Core/Models/Patch/GroupPatch.cs +++ b/PluralKit.Core/Models/Patch/GroupPatch.cs @@ -6,6 +6,7 @@ namespace PluralKit.Core public class GroupPatch: PatchObject { public Partial Name { get; set; } + public Partial Hid { get; set; } public Partial DisplayName { get; set; } public Partial Description { get; set; } public Partial Icon { get; set; } @@ -18,6 +19,7 @@ namespace PluralKit.Core public override UpdateQueryBuilder Apply(UpdateQueryBuilder b) => b .With("name", Name) + .With("hid", Hid) .With("display_name", DisplayName) .With("description", Description) .With("icon", Icon)