From 0d27e669c8947ddeb053570f3788e3d03ccdfe46 Mon Sep 17 00:00:00 2001 From: Iris System Date: Wed, 23 Nov 2022 23:53:21 +1300 Subject: [PATCH] feat(bot): add admin commands for hid rerolls --- PluralKit.Bot/CommandMeta/CommandTree.cs | 6 ++ PluralKit.Bot/Commands/Admin.cs | 71 ++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/PluralKit.Bot/CommandMeta/CommandTree.cs b/PluralKit.Bot/CommandMeta/CommandTree.cs index b129beb4..da051b20 100644 --- a/PluralKit.Bot/CommandMeta/CommandTree.cs +++ b/PluralKit.Bot/CommandMeta/CommandTree.cs @@ -117,6 +117,12 @@ public partial class CommandTree 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("rsid", "rerollsystemid")) + await ctx.Execute(Admin, a => a.RerollSystemId(ctx)); + else if (ctx.Match("rmid", "rerollmemberid")) + await ctx.Execute(Admin, a => a.RerollMemberId(ctx)); + else if (ctx.Match("rgid", "rerollgroupid")) + await ctx.Execute(Admin, a => a.RerollGroupId(ctx)); else if (ctx.Match("uml", "updatememberlimit")) await ctx.Execute(Admin, a => a.SystemMemberLimit(ctx)); else if (ctx.Match("ugl", "updategrouplimit")) diff --git a/PluralKit.Bot/Commands/Admin.cs b/PluralKit.Bot/Commands/Admin.cs index 3a0d3823..d5a2ed0f 100644 --- a/PluralKit.Bot/Commands/Admin.cs +++ b/PluralKit.Bot/Commands/Admin.cs @@ -1,5 +1,8 @@ using System.Text.RegularExpressions; +using Dapper; +using SqlKata; + using PluralKit.Core; namespace PluralKit.Bot; @@ -87,6 +90,74 @@ public class Admin await ctx.Reply($"{Emojis.Success} Group ID updated (`{target.Hid}` -> `{newHid}`)."); } + public async Task RerollSystemId(Context ctx) + { + ctx.AssertBotAdmin(); + + var target = await ctx.MatchSystem(); + if (target == null) + throw new PKError("Unknown system."); + + if (!await ctx.PromptYesNo($"Reroll system ID `{target.Hid}`?", "Reroll")) + throw new PKError("ID change cancelled."); + + var query = new Query("systems").AsUpdate(new + { + hid = new UnsafeLiteral("find_free_system_hid()"), + }) + .Where("id", target.Id); + + var newHid = await ctx.Database.QueryFirst(query, "returning hid"); + await ctx.Reply($"{Emojis.Success} System ID updated (`{target.Hid}` -> `{newHid}`)."); + } + + public async Task RerollMemberId(Context ctx) + { + ctx.AssertBotAdmin(); + + var target = await ctx.MatchMember(); + if (target == null) + throw new PKError("Unknown member."); + + if (!await ctx.PromptYesNo( + $"Reroll member ID for **{target.NameFor(LookupContext.ByNonOwner)}** (`{target.Hid}`)?", + "Reroll" + )) + throw new PKError("ID change cancelled."); + + var query = new Query("members").AsUpdate(new + { + hid = new UnsafeLiteral("find_free_member_hid()"), + }) + .Where("id", target.Id); + + var newHid = await ctx.Database.QueryFirst(query, "returning hid"); + await ctx.Reply($"{Emojis.Success} Member ID updated (`{target.Hid}` -> `{newHid}`)."); + } + + public async Task RerollGroupId(Context ctx) + { + ctx.AssertBotAdmin(); + + var target = await ctx.MatchGroup(); + if (target == null) + throw new PKError("Unknown group."); + + if (!await ctx.PromptYesNo($"Reroll group ID for **{target.Name}** (`{target.Hid}`)?", + "Change" + )) + throw new PKError("ID change cancelled."); + + var query = new Query("groups").AsUpdate(new + { + hid = new UnsafeLiteral("find_free_group_hid()"), + }) + .Where("id", target.Id); + + var newHid = await ctx.Database.QueryFirst(query, "returning hid"); + await ctx.Reply($"{Emojis.Success} Group ID updated (`{target.Hid}` -> `{newHid}`)."); + } + public async Task SystemMemberLimit(Context ctx) { ctx.AssertBotAdmin();