PluralKit/PluralKit.Bot/Commands/SystemLink.cs

59 lines
2.2 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
using DSharpPlus.Entities;
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
{
private IDataStore _data;
2019-05-21 21:40:26 +00:00
2020-02-01 12:03:02 +00:00
public SystemLink(IDataStore data)
2019-10-05 05:41:00 +00:00
{
_data = data;
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();
var account = await ctx.MatchUser() ?? throw new PKSyntaxError("You must pass an account to link with (either ID or @mention).");
var accountIds = await _data.GetSystemAccounts(ctx.System);
2019-05-21 21:40:26 +00:00
if (accountIds.Contains(account.Id)) throw Errors.AccountAlreadyLinked;
var existingAccount = await _data.GetSystemByAccount(account.Id);
2019-05-21 21:40:26 +00:00
if (existingAccount != null) throw Errors.AccountInOtherSystem(existingAccount);
var msg = await ctx.Reply($"{account.Mention}, please confirm the link by clicking the {Emojis.Success} reaction on this message.",
mentions: new IMention[] { new UserMention(account) });
2019-10-05 05:41:00 +00:00
if (!await ctx.PromptYesNo(msg, user: account)) throw Errors.MemberLinkCancelled;
await _data.AddAccount(ctx.System, 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-01-25 17:08:35 +00:00
ulong id;
2019-10-05 05:41:00 +00:00
if (!ctx.HasNext())
2020-01-25 17:08:35 +00:00
id = ctx.Author.Id;
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
var accountIds = (await _data.GetSystemAccounts(ctx.System)).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;
2019-10-05 05:41:00 +00:00
var msg = await ctx.Reply(
2020-01-25 17:08:35 +00:00
$"Are you sure you want to unlink <@{id}> from your system?");
2019-10-05 05:41:00 +00:00
if (!await ctx.PromptYesNo(msg)) throw Errors.MemberUnlinkCancelled;
2019-05-21 21:40:26 +00:00
2020-01-25 17:08:35 +00:00
await _data.RemoveAccount(ctx.System, id);
2019-10-05 05:41:00 +00:00
await ctx.Reply($"{Emojis.Success} Account unlinked.");
2019-05-21 21:40:26 +00:00
}
}
}