use context utils in Admin.cs

This commit is contained in:
spiral 2021-07-14 19:59:22 -04:00
parent 5d8c16e480
commit 8992cf8a3f
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31

View File

@ -2,7 +2,6 @@
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using PluralKit.Bot.Interactive;
using PluralKit.Core; using PluralKit.Core;
namespace PluralKit.Bot namespace PluralKit.Bot
@ -36,12 +35,8 @@ namespace PluralKit.Bot
if (existingSystem != null) if (existingSystem != null)
throw new PKError($"Another system already exists with ID `{newHid}`."); throw new PKError($"Another system already exists with ID `{newHid}`.");
var prompt = new YesNoPrompt(ctx) if (!await ctx.PromptYesNo($"Change system ID of `{target.Hid}` to `{newHid}`?", "Change"))
{ throw new PKError("ID change cancelled.");
Message = $"Change system ID of `{target.Hid}` to `{newHid}`?",
AcceptLabel = "Change"
};
await prompt.Run();
await _db.Execute(c => _repo.UpdateSystem(c, target.Id, new SystemPatch {Hid = newHid})); await _db.Execute(c => _repo.UpdateSystem(c, target.Id, new SystemPatch {Hid = newHid}));
await ctx.Reply($"{Emojis.Success} System ID updated (`{target.Hid}` -> `{newHid}`)."); await ctx.Reply($"{Emojis.Success} System ID updated (`{target.Hid}` -> `{newHid}`).");
@ -63,14 +58,7 @@ namespace PluralKit.Bot
if (existingMember != null) if (existingMember != null)
throw new PKError($"Another member already exists with ID `{newHid}`."); throw new PKError($"Another member already exists with ID `{newHid}`.");
var prompt = new YesNoPrompt(ctx) if (!await ctx.PromptYesNo($"Change member ID of **{target.NameFor(LookupContext.ByNonOwner)}** (`{target.Hid}`) to `{newHid}`?", "Change"))
{
Message = $"Change member ID of **{target.NameFor(LookupContext.ByNonOwner)}** (`{target.Hid}`) to `{newHid}`?",
AcceptLabel = "Change"
};
await prompt.Run();
if (prompt.Result != true)
throw new PKError("ID change cancelled."); throw new PKError("ID change cancelled.");
await _db.Execute(c => _repo.UpdateMember(c, target.Id, new MemberPatch {Hid = newHid})); await _db.Execute(c => _repo.UpdateMember(c, target.Id, new MemberPatch {Hid = newHid}));
@ -93,14 +81,7 @@ namespace PluralKit.Bot
if (existingGroup != null) if (existingGroup != null)
throw new PKError($"Another group already exists with ID `{newHid}`."); throw new PKError($"Another group already exists with ID `{newHid}`.");
var prompt = new YesNoPrompt(ctx) if (!await ctx.PromptYesNo($"Change group ID of **{target.Name}** (`{target.Hid}`) to `{newHid}`?", "Change"))
{
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."); throw new PKError("ID change cancelled.");
await _db.Execute(c => _repo.UpdateGroup(c, target.Id, new GroupPatch {Hid = newHid})); await _db.Execute(c => _repo.UpdateGroup(c, target.Id, new GroupPatch {Hid = newHid}));
@ -125,15 +106,8 @@ namespace PluralKit.Bot
var newLimitStr = ctx.PopArgument(); var newLimitStr = ctx.PopArgument();
if (!int.TryParse(newLimitStr, out var newLimit)) if (!int.TryParse(newLimitStr, out var newLimit))
throw new PKError($"Couldn't parse `{newLimitStr}` as number."); throw new PKError($"Couldn't parse `{newLimitStr}` as number.");
var prompt = new YesNoPrompt(ctx)
{
Message = $"Update member limit from **{currentLimit}** to **{newLimit}**?",
AcceptLabel = "Update"
};
await prompt.Run();
if (prompt.Result != true) if (!await ctx.PromptYesNo($"Update member limit from **{currentLimit}** to **{newLimit}**?", "Update"))
throw new PKError("Member limit change cancelled."); throw new PKError("Member limit change cancelled.");
await using var conn = await _db.Obtain(); await using var conn = await _db.Obtain();
@ -163,14 +137,7 @@ namespace PluralKit.Bot
if (!int.TryParse(newLimitStr, out var newLimit)) if (!int.TryParse(newLimitStr, out var newLimit))
throw new PKError($"Couldn't parse `{newLimitStr}` as number."); throw new PKError($"Couldn't parse `{newLimitStr}` as number.");
var prompt = new YesNoPrompt(ctx) if (!await ctx.PromptYesNo($"Update group limit from **{currentLimit}** to **{newLimit}**?", "Update"))
{
Message = $"Update group limit from **{currentLimit}** to **{newLimit}**?",
AcceptLabel = "Update"
};
await prompt.Run();
if (prompt.Result != true)
throw new PKError("Group limit change cancelled."); throw new PKError("Group limit change cancelled.");
await using var conn = await _db.Obtain(); await using var conn = await _db.Obtain();