PluralKit/PluralKit.Bot/Commands/APICommands.cs

67 lines
2.6 KiB
C#
Raw Normal View History

2019-06-20 19:15:57 +00:00
using System.Threading.Tasks;
using Discord;
2019-10-05 05:41:00 +00:00
using PluralKit.Bot.CommandSystem;
2019-06-20 19:15:57 +00:00
namespace PluralKit.Bot.Commands
{
2019-10-05 05:41:00 +00:00
public class APICommands
2019-06-20 19:15:57 +00:00
{
2019-10-05 05:41:00 +00:00
private SystemStore _systems;
public APICommands(SystemStore systems)
{
_systems = systems;
}
public async Task GetToken(Context ctx)
2019-06-20 19:15:57 +00:00
{
2019-10-05 05:41:00 +00:00
ctx.CheckSystem();
2019-06-20 19:15:57 +00:00
// Get or make a token
2019-10-05 05:41:00 +00:00
var token = ctx.System.Token ?? await MakeAndSetNewToken(ctx.System);
2019-06-20 19:15:57 +00:00
// If we're not already in a DM, reply with a reminder to check
2019-10-05 05:41:00 +00:00
if (!(ctx.Channel is IDMChannel))
2019-06-20 19:15:57 +00:00
{
2019-10-05 05:41:00 +00:00
await ctx.Reply($"{Emojis.Success} Check your DMs!");
2019-06-20 19:15:57 +00:00
}
// DM the user a security disclaimer, and then the token in a separate message (for easy copying on mobile)
2019-10-05 05:41:00 +00:00
await ctx.Author.SendMessageAsync($"{Emojis.Warn} Please note that this grants access to modify (and delete!) all your system data, so keep it safe and secure. If it leaks or you need a new one, you can invalidate this one with `pk;token refresh`.\n\nYour token is below:");
await ctx.Author.SendMessageAsync(token);
2019-06-20 19:15:57 +00:00
}
2019-10-05 05:41:00 +00:00
private async Task<string> MakeAndSetNewToken(PKSystem system)
2019-06-20 19:15:57 +00:00
{
2019-10-05 05:41:00 +00:00
system.Token = PluralKit.Utils.GenerateToken();
await _systems.Save(system);
return system.Token;
2019-06-20 19:15:57 +00:00
}
2019-10-05 05:41:00 +00:00
public async Task RefreshToken(Context ctx)
2019-06-20 19:15:57 +00:00
{
2019-10-05 05:41:00 +00:00
ctx.CheckSystem();
if (ctx.System.Token == null)
2019-06-20 19:15:57 +00:00
{
// If we don't have a token, call the other method instead
// This does pretty much the same thing, except words the messages more appropriately for that :)
2019-10-05 05:41:00 +00:00
await GetToken(ctx);
2019-06-20 19:15:57 +00:00
return;
}
// Make a new token from scratch
2019-10-05 05:41:00 +00:00
var token = await MakeAndSetNewToken(ctx.System);
2019-06-20 19:15:57 +00:00
// If we're not already in a DM, reply with a reminder to check
2019-10-05 05:41:00 +00:00
if (!(ctx.Channel is IDMChannel))
2019-06-20 19:15:57 +00:00
{
2019-10-05 05:41:00 +00:00
await ctx.Reply($"{Emojis.Success} Check your DMs!");
2019-06-20 19:15:57 +00:00
}
// DM the user an invalidation disclaimer, and then the token in a separate message (for easy copying on mobile)
2019-10-05 05:41:00 +00:00
await ctx.Author.SendMessageAsync($"{Emojis.Warn} Your previous API token has been invalidated. You will need to change it anywhere it's currently used.\n\nYour token is below:");
await ctx.Author.SendMessageAsync(token);
2019-06-20 19:15:57 +00:00
}
}
}