cs-codex-dist-tests/Tools/BiblioTech/Commands/AdminCommand.cs

200 lines
7.0 KiB
C#
Raw Normal View History

2023-10-24 13:25:45 +00:00
using BiblioTech.Options;
using CodexPlugin;
2023-10-24 12:07:15 +00:00
namespace BiblioTech.Commands
{
2023-10-24 13:25:45 +00:00
public class AdminCommand : BaseCommand
2023-10-24 12:07:15 +00:00
{
2023-10-24 13:25:45 +00:00
private readonly ClearUserAssociationCommand clearCommand;
private readonly ReportCommand reportCommand;
private readonly DeployListCommand deployListCommand;
private readonly DeployUploadCommand deployUploadCommand;
2023-10-24 13:43:07 +00:00
private readonly DeployRemoveCommand deployRemoveCommand;
2023-10-24 13:25:45 +00:00
public override string Name => "admin";
public override string StartingMessage => "...";
public override string Description => "Admins only.";
2023-10-24 12:07:15 +00:00
2023-10-24 13:25:45 +00:00
public AdminCommand(DeploymentsFilesMonitor monitor)
2023-10-24 12:07:15 +00:00
{
2023-10-24 13:25:45 +00:00
clearCommand = new ClearUserAssociationCommand();
reportCommand = new ReportCommand();
deployListCommand = new DeployListCommand(monitor);
deployUploadCommand = new DeployUploadCommand(monitor);
2023-10-24 13:43:07 +00:00
deployRemoveCommand = new DeployRemoveCommand(monitor);
2023-10-24 12:07:15 +00:00
}
2023-10-24 13:25:45 +00:00
public override CommandOption[] Options => new CommandOption[]
2023-10-24 12:07:15 +00:00
{
2023-10-24 13:25:45 +00:00
clearCommand,
reportCommand,
deployListCommand,
deployUploadCommand,
2023-10-24 13:43:07 +00:00
deployRemoveCommand
2023-10-24 13:25:45 +00:00
};
protected override async Task Invoke(CommandContext context)
{
if (!IsSenderAdmin(context.Command))
2023-10-24 12:07:15 +00:00
{
2023-10-24 13:25:45 +00:00
await context.Command.FollowupAsync("You're not an admin.");
return;
2023-10-24 12:07:15 +00:00
}
2023-10-24 13:25:45 +00:00
await clearCommand.CommandHandler(context);
await reportCommand.CommandHandler(context);
await deployListCommand.CommandHandler(context);
await deployUploadCommand.CommandHandler(context);
2023-10-24 13:43:07 +00:00
await deployRemoveCommand.CommandHandler(context);
2023-10-24 12:07:15 +00:00
}
2023-10-24 13:25:45 +00:00
public class ClearUserAssociationCommand : SubCommandOption
{
private readonly UserOption user = new UserOption("User to clear Eth address for.", true);
public ClearUserAssociationCommand()
: base("clear", "Admin only. Clears current Eth address for a user, allowing them to set a new one.")
{
}
2023-10-24 12:07:15 +00:00
2023-10-24 13:25:45 +00:00
public override CommandOption[] Options => new[] { user };
2023-10-24 12:07:15 +00:00
2023-10-24 13:25:45 +00:00
protected override async Task onSubCommand(CommandContext context)
{
var userId = user.GetOptionUserId(context);
if (userId == null)
{
await context.Command.FollowupAsync("Failed to get user ID");
return;
}
Program.UserRepo.ClearUserAssociatedAddress(userId.Value);
await context.Command.FollowupAsync("Done.");
}
}
public class ReportCommand : SubCommandOption
2023-10-24 12:07:15 +00:00
{
2023-10-24 13:25:45 +00:00
private readonly UserOption user = new UserOption(
description: "User to report history for.",
isRequired: true);
public ReportCommand()
: base("report", "Admin only. Reports bot-interaction history for a user.")
{
}
2023-10-24 12:07:15 +00:00
2023-10-24 13:25:45 +00:00
public override CommandOption[] Options => new[] { user };
protected override async Task onSubCommand(CommandContext context)
{
var userId = user.GetOptionUserId(context);
if (userId == null)
{
await context.Command.FollowupAsync("Failed to get user ID");
return;
}
var report = Program.UserRepo.GetInteractionReport(userId.Value);
await context.Command.FollowupAsync(string.Join(Environment.NewLine, report));
}
}
public class DeployListCommand : SubCommandOption
2023-10-24 12:07:15 +00:00
{
2023-10-24 13:25:45 +00:00
private readonly DeploymentsFilesMonitor monitor;
public DeployListCommand(DeploymentsFilesMonitor monitor)
: base("list", "Lists current deployments.")
{
this.monitor = monitor;
}
protected override async Task onSubCommand(CommandContext context)
{
var deployments = monitor.GetDeployments();
if (!deployments.Any())
{
await context.Command.FollowupAsync("No deployments available.");
return;
}
await context.Command.FollowupAsync($"Deployments: {string.Join(", ", deployments.Select(FormatDeployment))}");
}
private string FormatDeployment(CodexDeployment deployment)
{
var m = deployment.Metadata;
2023-10-24 13:43:07 +00:00
return $"'{m.Name}' ({m.StartUtc.ToString("o")})";
2023-10-24 13:25:45 +00:00
}
}
public class DeployUploadCommand : SubCommandOption
{
private readonly DeploymentsFilesMonitor monitor;
private readonly FileAttachementOption fileOption = new FileAttachementOption(
name: "json",
description: "Codex-deployment json to add.",
isRequired: true);
public DeployUploadCommand(DeploymentsFilesMonitor monitor)
: base("add", "Upload a new deployment JSON file.")
{
this.monitor = monitor;
}
public override CommandOption[] Options => new[] { fileOption };
protected override async Task onSubCommand(CommandContext context)
{
var file = await fileOption.Parse(context);
if (file == null) return;
2023-10-24 13:43:07 +00:00
var result = await monitor.DownloadDeployment(file);
if (result)
{
await context.Command.FollowupAsync("Success!");
}
else
{
await context.Command.FollowupAsync("That didn't work.");
}
}
}
public class DeployRemoveCommand : SubCommandOption
{
private readonly DeploymentsFilesMonitor monitor;
private readonly StringOption stringOption = new StringOption(
name: "deployment name",
description: "Name of deployment to remove.",
isRequired: true);
public DeployRemoveCommand(DeploymentsFilesMonitor monitor)
: base("remove", "Removes a deployment file.")
{
this.monitor = monitor;
}
2023-10-24 13:25:45 +00:00
2023-10-24 13:43:07 +00:00
public override CommandOption[] Options => new[] { stringOption };
protected override async Task onSubCommand(CommandContext context)
{
var str = await stringOption.Parse(context);
if (string.IsNullOrEmpty(str)) return;
var result = monitor.DeleteDeployment(str);
if (result)
{
await context.Command.FollowupAsync("Success!");
}
else
{
await context.Command.FollowupAsync("That didn't work.");
}
2023-10-24 13:25:45 +00:00
}
2023-10-24 12:07:15 +00:00
}
}
}