feat(bot): add admin command for account recovery

This commit is contained in:
Iris System 2022-11-23 23:54:21 +13:00
parent 0d27e669c8
commit e65c2de542
2 changed files with 56 additions and 1 deletions

View File

@ -127,6 +127,8 @@ public partial class CommandTree
await ctx.Execute<Admin>(Admin, a => a.SystemMemberLimit(ctx));
else if (ctx.Match("ugl", "updategrouplimit"))
await ctx.Execute<Admin>(Admin, a => a.SystemGroupLimit(ctx));
else if (ctx.Match("sr", "systemrecover"))
await ctx.Execute<Admin>(Admin, a => a.SystemRecover(ctx));
else
await ctx.Reply($"{Emojis.Error} Unknown command.");
}

View File

@ -3,6 +3,9 @@ using System.Text.RegularExpressions;
using Dapper;
using SqlKata;
using Myriad.Rest;
using Myriad.Types;
using PluralKit.Core;
namespace PluralKit.Bot;
@ -10,10 +13,12 @@ namespace PluralKit.Bot;
public class Admin
{
private readonly BotConfig _botConfig;
private readonly DiscordApiClient _rest;
public Admin(BotConfig botConfig)
public Admin(BotConfig botConfig, DiscordApiClient rest)
{
_botConfig = botConfig;
_rest = rest;
}
public async Task UpdateSystemId(Context ctx)
@ -213,4 +218,52 @@ public class Admin
await ctx.Repository.UpdateSystemConfig(target.Id, new SystemConfigPatch { GroupLimitOverride = newLimit });
await ctx.Reply($"{Emojis.Success} Group limit updated.");
}
public async Task SystemRecover(Context ctx)
{
ctx.AssertBotAdmin();
var rerollToken = ctx.MatchFlag("rt", "reroll-token");
var systemToken = ctx.PopArgument();
var systemId = await ctx.Database.Execute(conn => conn.QuerySingleOrDefaultAsync<SystemId?>(
"select id from systems where token = @token",
new { token = systemToken }
));
if (systemId == null)
throw new PKError("Could not retrieve a system with that token.");
var account = await ctx.MatchUser();
if (account == null)
throw new PKError("You must pass an account to associate the system with (either ID or @mention).");
var existingAccount = await ctx.Repository.GetSystemByAccount(account.Id);
if (existingAccount != null)
throw Errors.AccountInOtherSystem(existingAccount);
var system = await ctx.Repository.GetSystem(systemId.Value!);
if (!await ctx.PromptYesNo($"Associate account {account.NameAndMention()} with system `{system.Hid}`?", "Recover account"))
throw new PKError("System recovery cancelled.");
await ctx.Repository.AddAccount(system.Id, account.Id);
if (rerollToken)
await ctx.Repository.UpdateSystem(system.Id, new SystemPatch { Token = StringUtils.GenerateToken() });
if ((await ctx.BotPermissions).HasFlag(PermissionSet.ManageMessages))
await _rest.DeleteMessage(ctx.Message);
await ctx.Reply(null, new Embed
{
Title = "System recovered",
Description = $"{account.NameAndMention()} has been linked to system `{system.Hid}`.",
Fields = new Embed.Field[]
{
new Embed.Field("Token rerolled?", rerollToken ? "yes" : "no", true),
new Embed.Field("Actioned by", ctx.Author.NameAndMention(), true),
},
Color = DiscordUtils.Green,
});
}
}