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