PluralKit/PluralKit.Bot/Commands/LinkCommands.cs

57 lines
2.2 KiB
C#
Raw Normal View History

2019-05-21 21:40:26 +00:00
using System.Linq;
using System.Threading.Tasks;
using Discord;
2019-10-05 05:41:00 +00:00
using PluralKit.Bot.CommandSystem;
2019-05-21 21:40:26 +00:00
namespace PluralKit.Bot.Commands
{
2019-10-05 05:41:00 +00:00
public class LinkCommands
2019-05-21 21:40:26 +00:00
{
private IDataStore _data;
2019-05-21 21:40:26 +00:00
public LinkCommands(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);
2019-10-05 05:41:00 +00:00
var msg = await ctx.Reply($"{account.Mention}, please confirm the link by clicking the {Emojis.Success} reaction on this message.");
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
2019-10-05 05:41:00 +00:00
IUser account;
if (!ctx.HasNext())
account = ctx.Author;
else
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)).ToList();
2019-05-21 21:40:26 +00:00
if (!accountIds.Contains(account.Id)) throw Errors.AccountNotLinked;
if (accountIds.Count == 1) throw Errors.UnlinkingLastAccount;
2019-10-05 05:41:00 +00:00
var msg = await ctx.Reply(
2019-05-21 21:40:26 +00:00
$"Are you sure you want to unlink {account.Mention} 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
await _data.RemoveAccount(ctx.System, account.Id);
2019-10-05 05:41:00 +00:00
await ctx.Reply($"{Emojis.Success} Account unlinked.");
2019-05-21 21:40:26 +00:00
}
}
}