PluralKit/PluralKit.Bot/Commands/SystemLink.cs

66 lines
2.4 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
}
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
await using var conn = await _db.Obtain();
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).");
2020-08-29 11:46:27 +00:00
var accountIds = await _repo.GetSystemAccounts(conn, ctx.System.Id);
if (accountIds.Contains(account.Id))
throw Errors.AccountAlreadyLinked;
2019-05-21 21:40:26 +00:00
2020-08-29 11:46:27 +00:00
var existingAccount = await _repo.GetSystemByAccount(conn, account.Id);
if (existingAccount != null)
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;
2020-08-29 11:46:27 +00:00
await _repo.AddAccount(conn, 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();
2019-05-21 21:40:26 +00:00
2020-08-29 11:46:27 +00:00
await using var conn = await _db.Obtain();
2020-01-25 17:08:35 +00:00
ulong id;
2019-10-05 05:41:00 +00:00
if (!ctx.HasNext())
2021-01-31 15:16:52 +00:00
id = ctx.Author.Id;
2020-01-25 17:08:35 +00:00
else if (!ctx.MatchUserRaw(out id))
throw new PKSyntaxError("You must pass an account to link with (either ID or @mention).");
2019-10-05 05:41:00 +00:00
2020-08-29 11:46:27 +00:00
var accountIds = (await _repo.GetSystemAccounts(conn, 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;
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
2020-08-29 11:46:27 +00:00
await _repo.RemoveAccount(conn, 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
}
}
}