2020-12-24 14:52:44 +01:00
|
|
|
using Myriad.Extensions;
|
2025-10-04 19:32:58 +00:00
|
|
|
using Myriad.Types;
|
2020-06-20 17:33:10 +02:00
|
|
|
|
2020-02-12 15:16:19 +01:00
|
|
|
using PluralKit.Core;
|
2019-05-21 23:40:26 +02:00
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
namespace PluralKit.Bot;
|
|
|
|
|
|
|
|
|
|
public class SystemLink
|
2019-05-21 23:40:26 +02:00
|
|
|
{
|
2025-10-04 19:32:58 +00:00
|
|
|
public async Task LinkSystem(Context ctx, User account)
|
2021-11-26 21:10:56 -05:00
|
|
|
{
|
|
|
|
|
ctx.CheckSystem();
|
|
|
|
|
|
2022-01-22 03:05:01 -05:00
|
|
|
var accountIds = await ctx.Repository.GetSystemAccounts(ctx.System.Id);
|
2021-11-26 21:10:56 -05:00
|
|
|
if (accountIds.Contains(account.Id))
|
|
|
|
|
throw Errors.AccountAlreadyLinked;
|
|
|
|
|
|
2022-01-22 03:05:01 -05:00
|
|
|
var existingAccount = await ctx.Repository.GetSystemByAccount(account.Id);
|
2021-11-26 21:10:56 -05:00
|
|
|
if (existingAccount != null)
|
2024-12-31 08:09:18 -07:00
|
|
|
throw Errors.AccountInOtherSystem(existingAccount, ctx.Config, ctx.DefaultPrefix);
|
2021-11-26 21:10:56 -05:00
|
|
|
|
|
|
|
|
var msg = $"{account.Mention()}, please confirm the link.";
|
|
|
|
|
if (!await ctx.PromptYesNo(msg, "Confirm", account, false)) throw Errors.MemberLinkCancelled;
|
2022-01-22 03:05:01 -05:00
|
|
|
await ctx.Repository.AddAccount(ctx.System.Id, account.Id);
|
2021-11-26 21:10:56 -05:00
|
|
|
await ctx.Reply($"{Emojis.Success} Account linked to system.");
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-08 03:26:40 +00:00
|
|
|
public async Task UnlinkAccount(Context ctx, string idRaw, bool confirmYes)
|
2021-11-26 21:10:56 -05:00
|
|
|
{
|
|
|
|
|
ctx.CheckSystem();
|
|
|
|
|
|
|
|
|
|
ulong id;
|
2025-09-28 15:03:28 +00:00
|
|
|
if (!idRaw.TryParseMention(out id))
|
2022-06-03 01:15:15 -04:00
|
|
|
throw new PKSyntaxError("You must pass an account to unlink from (either ID or @mention).");
|
2021-11-26 21:10:56 -05:00
|
|
|
|
2022-01-22 03:05:01 -05:00
|
|
|
var accountIds = (await ctx.Repository.GetSystemAccounts(ctx.System.Id)).ToList();
|
2021-11-26 21:10:56 -05:00
|
|
|
if (!accountIds.Contains(id)) throw Errors.AccountNotLinked;
|
2024-12-31 08:09:18 -07:00
|
|
|
if (accountIds.Count == 1) throw Errors.UnlinkingLastAccount(ctx.DefaultPrefix);
|
2021-11-26 21:10:56 -05:00
|
|
|
|
|
|
|
|
var msg = $"Are you sure you want to unlink <@{id}> from your system?";
|
2025-10-08 03:26:40 +00:00
|
|
|
if (!await ctx.PromptYesNo(msg, "Unlink", flagValue: confirmYes)) throw Errors.MemberUnlinkCancelled;
|
2021-11-26 21:10:56 -05:00
|
|
|
|
2022-01-22 03:05:01 -05:00
|
|
|
await ctx.Repository.RemoveAccount(ctx.System.Id, id);
|
2021-11-26 21:10:56 -05:00
|
|
|
await ctx.Reply($"{Emojis.Success} Account unlinked.");
|
2019-05-21 23:40:26 +02:00
|
|
|
}
|
|
|
|
|
}
|