PluralKit/PluralKit.Bot/Commands/SystemLink.cs

60 lines
2.1 KiB
C#
Raw Normal View History

2019-05-21 21:40:26 +00:00
using System.Linq;
using System.Threading.Tasks;
2019-10-05 05:41:00 +00:00
2020-12-24 13:52:44 +00:00
using Myriad.Extensions;
using Myriad.Rest.Types;
using PluralKit.Core;
2019-05-21 21:40:26 +00:00
namespace PluralKit.Bot
2019-05-21 21:40:26 +00:00
{
2020-02-01 12:03:02 +00:00
public class SystemLink
2019-05-21 21:40:26 +00:00
{
2020-08-29 11:46:27 +00:00
private readonly IDatabase _db;
private readonly ModelRepository _repo;
2019-05-21 21:40:26 +00:00
2020-08-29 11:46:27 +00:00
public SystemLink(IDatabase db, ModelRepository repo)
2019-10-05 05:41:00 +00:00
{
2020-08-29 11:46:27 +00:00
_db = db;
_repo = repo;
2019-10-05 05:41:00 +00:00
}
2021-08-27 15:03:47 +00:00
2019-10-05 05:41:00 +00:00
public async Task LinkSystem(Context ctx)
2019-05-21 21:40:26 +00:00
{
2019-10-05 05:41:00 +00:00
ctx.CheckSystem();
2020-08-29 11:46:27 +00:00
2019-10-05 05:41:00 +00:00
var account = await ctx.MatchUser() ?? throw new PKSyntaxError("You must pass an account to link with (either ID or @mention).");
var accountIds = await _repo.GetSystemAccounts(ctx.System.Id);
2020-08-29 11:46:27 +00:00
if (accountIds.Contains(account.Id))
throw Errors.AccountAlreadyLinked;
2019-05-21 21:40:26 +00:00
var existingAccount = await _repo.GetSystemByAccount(account.Id);
2020-08-29 11:46:27 +00:00
if (existingAccount != null)
2021-08-27 15:03:47 +00:00
throw Errors.AccountInOtherSystem(existingAccount);
2019-05-21 21:40:26 +00:00
2021-07-02 10:40:40 +00:00
var msg = $"{account.Mention()}, please confirm the link.";
if (!await ctx.PromptYesNo(msg, "Confirm", user: account, matchFlag: false)) throw Errors.MemberLinkCancelled;
await _repo.AddAccount(ctx.System.Id, account.Id);
2019-10-05 05:41:00 +00:00
await ctx.Reply($"{Emojis.Success} Account linked to system.");
2019-05-21 21:40:26 +00:00
}
2019-10-05 05:41:00 +00:00
public async Task UnlinkAccount(Context ctx)
2019-05-21 21:40:26 +00:00
{
2019-10-05 05:41:00 +00:00
ctx.CheckSystem();
2021-08-27 15:03:47 +00:00
2020-01-25 17:08:35 +00:00
ulong id;
if (!ctx.MatchUserRaw(out id))
2020-01-25 17:08:35 +00:00
throw new PKSyntaxError("You must pass an account to link with (either ID or @mention).");
2019-10-05 05:41:00 +00:00
var accountIds = (await _repo.GetSystemAccounts(ctx.System.Id)).ToList();
2020-01-25 17:08:35 +00:00
if (!accountIds.Contains(id)) throw Errors.AccountNotLinked;
2019-05-21 21:40:26 +00:00
if (accountIds.Count == 1) throw Errors.UnlinkingLastAccount;
2021-08-27 15:03:47 +00:00
2020-07-21 00:10:26 +00:00
var msg = $"Are you sure you want to unlink <@{id}> from your system?";
2021-07-02 10:40:40 +00:00
if (!await ctx.PromptYesNo(msg, "Unlink")) throw Errors.MemberUnlinkCancelled;
2019-05-21 21:40:26 +00:00
await _repo.RemoveAccount(ctx.System.Id, id);
2019-10-05 05:41:00 +00:00
await ctx.Reply($"{Emojis.Success} Account unlinked.");
2019-05-21 21:40:26 +00:00
}
}
}