Refactor command system

This commit is contained in:
Ske
2019-10-05 07:41:00 +02:00
parent 0ec522ca0a
commit 1988b29fbc
21 changed files with 1353 additions and 882 deletions

View File

@@ -1,66 +1,67 @@
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using PluralKit.Bot.CommandSystem;
namespace PluralKit.Bot.Commands
{
[Group("token")]
public class APICommands: ModuleBase<PKCommandContext>
public class APICommands
{
public SystemStore Systems { get; set; }
[Command]
[MustHaveSystem]
[Remarks("token")]
public async Task GetToken()
private SystemStore _systems;
public APICommands(SystemStore systems)
{
_systems = systems;
}
public async Task GetToken(Context ctx)
{
ctx.CheckSystem();
// Get or make a token
var token = Context.SenderSystem.Token ?? await MakeAndSetNewToken();
var token = ctx.System.Token ?? await MakeAndSetNewToken(ctx.System);
// If we're not already in a DM, reply with a reminder to check
if (!(Context.Channel is IDMChannel))
if (!(ctx.Channel is IDMChannel))
{
await Context.Channel.SendMessageAsync($"{Emojis.Success} Check your DMs!");
await ctx.Reply($"{Emojis.Success} Check your DMs!");
}
// DM the user a security disclaimer, and then the token in a separate message (for easy copying on mobile)
await Context.User.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 Context.User.SendMessageAsync(token);
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);
}
private async Task<string> MakeAndSetNewToken()
private async Task<string> MakeAndSetNewToken(PKSystem system)
{
Context.SenderSystem.Token = PluralKit.Utils.GenerateToken();
await Systems.Save(Context.SenderSystem);
return Context.SenderSystem.Token;
system.Token = PluralKit.Utils.GenerateToken();
await _systems.Save(system);
return system.Token;
}
[Command("refresh")]
[MustHaveSystem]
[Alias("expire", "invalidate", "update", "new")]
[Remarks("token refresh")]
public async Task RefreshToken()
public async Task RefreshToken(Context ctx)
{
if (Context.SenderSystem.Token == null)
ctx.CheckSystem();
if (ctx.System.Token == null)
{
// 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 :)
await GetToken();
await GetToken(ctx);
return;
}
// Make a new token from scratch
var token = await MakeAndSetNewToken();
var token = await MakeAndSetNewToken(ctx.System);
// If we're not already in a DM, reply with a reminder to check
if (!(Context.Channel is IDMChannel))
if (!(ctx.Channel is IDMChannel))
{
await Context.Channel.SendMessageAsync($"{Emojis.Success} Check your DMs!");
await ctx.Reply($"{Emojis.Success} Check your DMs!");
}
// DM the user an invalidation disclaimer, and then the token in a separate message (for easy copying on mobile)
await Context.User.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 Context.User.SendMessageAsync(token);
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);
}
}
}