2021-06-08 17:37:44 +00:00
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Bot;
|
|
|
|
|
|
|
|
public class Admin
|
2021-06-08 17:37:44 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly BotConfig _botConfig;
|
|
|
|
|
2022-01-22 08:05:01 +00:00
|
|
|
public Admin(BotConfig botConfig)
|
2021-06-08 17:37:44 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
_botConfig = botConfig;
|
|
|
|
}
|
2021-06-08 17:37:44 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async Task UpdateSystemId(Context ctx)
|
|
|
|
{
|
|
|
|
ctx.AssertBotAdmin();
|
2021-06-08 17:37:44 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var target = await ctx.MatchSystem();
|
|
|
|
if (target == null)
|
|
|
|
throw new PKError("Unknown system.");
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var newHid = ctx.PopArgument();
|
|
|
|
if (!Regex.IsMatch(newHid, "^[a-z]{5}$"))
|
|
|
|
throw new PKError($"Invalid new system ID `{newHid}`.");
|
2021-06-08 17:37:44 +00:00
|
|
|
|
2022-01-22 08:05:01 +00:00
|
|
|
var existingSystem = await ctx.Repository.GetSystemByHid(newHid);
|
2021-11-27 02:10:56 +00:00
|
|
|
if (existingSystem != null)
|
|
|
|
throw new PKError($"Another system already exists with ID `{newHid}`.");
|
2021-06-08 17:37:44 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (!await ctx.PromptYesNo($"Change system ID of `{target.Hid}` to `{newHid}`?", "Change"))
|
|
|
|
throw new PKError("ID change cancelled.");
|
2021-06-08 17:37:44 +00:00
|
|
|
|
2022-01-22 08:05:01 +00:00
|
|
|
await ctx.Repository.UpdateSystem(target.Id, new SystemPatch { Hid = newHid });
|
2021-11-27 02:10:56 +00:00
|
|
|
await ctx.Reply($"{Emojis.Success} System ID updated (`{target.Hid}` -> `{newHid}`).");
|
|
|
|
}
|
2021-06-08 17:37:44 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async Task UpdateMemberId(Context ctx)
|
|
|
|
{
|
|
|
|
ctx.AssertBotAdmin();
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var target = await ctx.MatchMember();
|
|
|
|
if (target == null)
|
|
|
|
throw new PKError("Unknown member.");
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var newHid = ctx.PopArgument();
|
|
|
|
if (!Regex.IsMatch(newHid, "^[a-z]{5}$"))
|
|
|
|
throw new PKError($"Invalid new member ID `{newHid}`.");
|
2021-06-08 17:37:44 +00:00
|
|
|
|
2022-01-22 08:05:01 +00:00
|
|
|
var existingMember = await ctx.Repository.GetMemberByHid(newHid);
|
2021-11-27 02:10:56 +00:00
|
|
|
if (existingMember != null)
|
|
|
|
throw new PKError($"Another member already exists with ID `{newHid}`.");
|
2021-06-08 17:37:44 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (!await ctx.PromptYesNo(
|
|
|
|
$"Change member ID of **{target.NameFor(LookupContext.ByNonOwner)}** (`{target.Hid}`) to `{newHid}`?",
|
|
|
|
"Change"
|
|
|
|
))
|
|
|
|
throw new PKError("ID change cancelled.");
|
2021-06-08 17:37:44 +00:00
|
|
|
|
2022-01-22 08:05:01 +00:00
|
|
|
await ctx.Repository.UpdateMember(target.Id, new MemberPatch { Hid = newHid });
|
2021-11-27 02:10:56 +00:00
|
|
|
await ctx.Reply($"{Emojis.Success} Member ID updated (`{target.Hid}` -> `{newHid}`).");
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async Task UpdateGroupId(Context ctx)
|
|
|
|
{
|
|
|
|
ctx.AssertBotAdmin();
|
2021-06-08 17:37:44 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var target = await ctx.MatchGroup();
|
|
|
|
if (target == null)
|
|
|
|
throw new PKError("Unknown group.");
|
2021-07-08 14:04:05 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var newHid = ctx.PopArgument();
|
|
|
|
if (!Regex.IsMatch(newHid, "^[a-z]{5}$"))
|
|
|
|
throw new PKError($"Invalid new group ID `{newHid}`.");
|
2021-07-08 14:04:05 +00:00
|
|
|
|
2022-01-22 08:05:01 +00:00
|
|
|
var existingGroup = await ctx.Repository.GetGroupByHid(newHid);
|
2021-11-27 02:10:56 +00:00
|
|
|
if (existingGroup != null)
|
|
|
|
throw new PKError($"Another group already exists with ID `{newHid}`.");
|
2021-07-08 14:04:05 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (!await ctx.PromptYesNo($"Change group ID of **{target.Name}** (`{target.Hid}`) to `{newHid}`?",
|
|
|
|
"Change"
|
|
|
|
))
|
|
|
|
throw new PKError("ID change cancelled.");
|
2021-07-08 14:04:05 +00:00
|
|
|
|
2022-01-22 08:05:01 +00:00
|
|
|
await ctx.Repository.UpdateGroup(target.Id, new GroupPatch { Hid = newHid });
|
2021-11-27 02:10:56 +00:00
|
|
|
await ctx.Reply($"{Emojis.Success} Group ID updated (`{target.Hid}` -> `{newHid}`).");
|
|
|
|
}
|
2021-07-08 14:04:05 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async Task SystemMemberLimit(Context ctx)
|
|
|
|
{
|
|
|
|
ctx.AssertBotAdmin();
|
2021-07-08 14:04:05 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var target = await ctx.MatchSystem();
|
|
|
|
if (target == null)
|
|
|
|
throw new PKError("Unknown system.");
|
|
|
|
|
2022-01-22 08:05:01 +00:00
|
|
|
var config = await ctx.Repository.GetSystemConfig(target.Id);
|
2021-11-30 02:35:21 +00:00
|
|
|
|
|
|
|
var currentLimit = config.MemberLimitOverride ?? Limits.MaxMemberCount;
|
2021-11-27 02:10:56 +00:00
|
|
|
if (!ctx.HasNext())
|
2021-06-08 17:37:44 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
await ctx.Reply($"Current member limit is **{currentLimit}** members.");
|
|
|
|
return;
|
|
|
|
}
|
2021-06-08 17:37:44 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var newLimitStr = ctx.PopArgument();
|
|
|
|
if (!int.TryParse(newLimitStr, out var newLimit))
|
|
|
|
throw new PKError($"Couldn't parse `{newLimitStr}` as number.");
|
2021-06-08 17:37:44 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (!await ctx.PromptYesNo($"Update member limit from **{currentLimit}** to **{newLimit}**?", "Update"))
|
|
|
|
throw new PKError("Member limit change cancelled.");
|
2021-06-08 17:37:44 +00:00
|
|
|
|
2022-01-22 08:05:01 +00:00
|
|
|
await ctx.Repository.UpdateSystemConfig(target.Id, new SystemConfigPatch { MemberLimitOverride = newLimit });
|
2021-11-27 02:10:56 +00:00
|
|
|
await ctx.Reply($"{Emojis.Success} Member limit updated.");
|
|
|
|
}
|
2021-06-08 17:37:44 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async Task SystemGroupLimit(Context ctx)
|
|
|
|
{
|
|
|
|
ctx.AssertBotAdmin();
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var target = await ctx.MatchSystem();
|
|
|
|
if (target == null)
|
|
|
|
throw new PKError("Unknown system.");
|
2021-06-08 17:37:44 +00:00
|
|
|
|
2022-01-22 08:05:01 +00:00
|
|
|
var config = await ctx.Repository.GetSystemConfig(target.Id);
|
2021-11-30 02:35:21 +00:00
|
|
|
|
|
|
|
var currentLimit = config.GroupLimitOverride ?? Limits.MaxGroupCount;
|
2021-11-27 02:10:56 +00:00
|
|
|
if (!ctx.HasNext())
|
2021-07-08 14:04:05 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
await ctx.Reply($"Current group limit is **{currentLimit}** groups.");
|
|
|
|
return;
|
|
|
|
}
|
2021-07-08 14:04:05 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var newLimitStr = ctx.PopArgument();
|
|
|
|
if (!int.TryParse(newLimitStr, out var newLimit))
|
|
|
|
throw new PKError($"Couldn't parse `{newLimitStr}` as number.");
|
2021-07-08 14:04:05 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (!await ctx.PromptYesNo($"Update group limit from **{currentLimit}** to **{newLimit}**?", "Update"))
|
|
|
|
throw new PKError("Group limit change cancelled.");
|
2021-07-08 14:04:05 +00:00
|
|
|
|
2022-01-22 08:05:01 +00:00
|
|
|
await ctx.Repository.UpdateSystemConfig(target.Id, new SystemConfigPatch { GroupLimitOverride = newLimit });
|
2021-11-27 02:10:56 +00:00
|
|
|
await ctx.Reply($"{Emojis.Success} Group limit updated.");
|
2021-06-08 17:37:44 +00:00
|
|
|
}
|
|
|
|
}
|