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

@@ -5,24 +5,27 @@ using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.Net;
using Newtonsoft.Json;
using PluralKit.Bot.CommandSystem;
namespace PluralKit.Bot.Commands
{
public class ImportExportCommands : ModuleBase<PKCommandContext>
public class ImportExportCommands
{
public DataFileService DataFiles { get; set; }
[Command("import")]
[Remarks("import [fileurl]")]
public async Task Import([Remainder] string url = null)
private DataFileService _dataFiles;
public ImportExportCommands(DataFileService dataFiles)
{
if (url == null) url = Context.Message.Attachments.FirstOrDefault()?.Url;
_dataFiles = dataFiles;
}
public async Task Import(Context ctx)
{
var url = ctx.RemainderOrNull() ?? ctx.Message.Attachments.FirstOrDefault()?.Url;
if (url == null) throw Errors.NoImportFilePassed;
await Context.BusyIndicator(async () =>
await ctx.BusyIndicator(async () =>
{
using (var client = new HttpClient())
{
@@ -74,8 +77,8 @@ namespace PluralKit.Bot.Commands
issueStr +=
"\n- PluralKit does not support per-member system tags. Since you had multiple members with distinct tags, tags will not be imported. You can set your system tag using the `pk;system tag <tag>` command later.";
var msg = await Context.Channel.SendMessageAsync($"{issueStr}\n\nDo you want to proceed with the import?");
if (!await Context.PromptYesNo(msg)) throw Errors.ImportCancelled;
var msg = await ctx.Reply($"{issueStr}\n\nDo you want to proceed with the import?");
if (!await ctx.PromptYesNo(msg)) throw Errors.ImportCancelled;
}
data = res.System;
@@ -89,37 +92,36 @@ namespace PluralKit.Bot.Commands
if (!data.Valid) throw Errors.InvalidImportFile;
if (data.LinkedAccounts != null && !data.LinkedAccounts.Contains(Context.User.Id))
if (data.LinkedAccounts != null && !data.LinkedAccounts.Contains(ctx.Author.Id))
{
var msg = await Context.Channel.SendMessageAsync($"{Emojis.Warn} You seem to importing a system profile belonging to another account. Are you sure you want to proceed?");
if (!await Context.PromptYesNo(msg)) throw Errors.ImportCancelled;
var msg = await ctx.Reply($"{Emojis.Warn} You seem to importing a system profile belonging to another account. Are you sure you want to proceed?");
if (!await ctx.PromptYesNo(msg)) throw Errors.ImportCancelled;
}
// If passed system is null, it'll create a new one
// (and that's okay!)
var result = await DataFiles.ImportSystem(data, Context.SenderSystem, Context.User.Id);
var result = await _dataFiles.ImportSystem(data, ctx.System, ctx.Author.Id);
if (Context.SenderSystem == null)
if (ctx.System != null)
{
await Context.Channel.SendMessageAsync($"{Emojis.Success} PluralKit has created a system for you based on the given file. Your system ID is `{result.System.Hid}`. Type `pk;system` for more information.");
await ctx.Reply($"{Emojis.Success} PluralKit has created a system for you based on the given file. Your system ID is `{result.System.Hid}`. Type `pk;system` for more information.");
}
else
{
await Context.Channel.SendMessageAsync($"{Emojis.Success} Updated {result.ModifiedNames.Count} members, created {result.AddedNames.Count} members. Type `pk;system list` to check!");
await ctx.Reply($"{Emojis.Success} Updated {result.ModifiedNames.Count} members, created {result.AddedNames.Count} members. Type `pk;system list` to check!");
}
}
});
}
[Command("export")]
[Remarks("export")]
[MustHaveSystem]
public async Task Export()
public async Task Export(Context ctx)
{
var json = await Context.BusyIndicator(async () =>
ctx.CheckSystem();
var json = await ctx.BusyIndicator(async () =>
{
// Make the actual data file
var data = await DataFiles.ExportSystem(Context.SenderSystem);
var data = await _dataFiles.ExportSystem(ctx.System);
return JsonConvert.SerializeObject(data, Formatting.None);
});
@@ -129,16 +131,16 @@ namespace PluralKit.Bot.Commands
try
{
await Context.User.SendFileAsync(stream, "system.json", $"{Emojis.Success} Here you go!");
await ctx.Author.SendFileAsync(stream, "system.json", $"{Emojis.Success} Here you go!");
// If the original message wasn't posted in DMs, send a public reminder
if (!(Context.Channel is IDMChannel))
await Context.Channel.SendMessageAsync($"{Emojis.Success} Check your DMs!");
if (!(ctx.Channel is IDMChannel))
await ctx.Reply($"{Emojis.Success} Check your DMs!");
}
catch (HttpException)
{
// If user has DMs closed, tell 'em to open them
await Context.Channel.SendMessageAsync(
await ctx.Reply(
$"{Emojis.Error} Could not send the data file in your DMs. Do you have DMs closed?");
}
}