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-25 08:54:26 +00:00
|
|
|
|
await context.Followup("You're not an admin.");
|
2023-10-24 13:25:45 +00:00
|
|
|
|
return;
|
2023-10-24 12:07:15 +00:00
|
|
|
|
}
|
2023-10-24 13:25:45 +00:00
|
|
|
|
|
2023-10-25 08:38:21 +00:00
|
|
|
|
if (!IsInAdminChannel(context.Command))
|
|
|
|
|
{
|
2023-10-25 08:54:26 +00:00
|
|
|
|
await context.Followup("Please use admin commands only in the admin channel.");
|
2023-10-25 08:38:21 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2023-10-25 08:54:26 +00:00
|
|
|
|
await context.AdminFollowup("Failed to get user ID");
|
2023-10-24 13:25:45 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Program.UserRepo.ClearUserAssociatedAddress(userId.Value);
|
2023-10-25 08:54:26 +00:00
|
|
|
|
await context.AdminFollowup("Done.");
|
2023-10-24 13:25:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2023-10-25 08:54:26 +00:00
|
|
|
|
await context.AdminFollowup("Failed to get user ID");
|
2023-10-24 13:25:45 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var report = Program.UserRepo.GetInteractionReport(userId.Value);
|
2023-10-25 08:54:26 +00:00
|
|
|
|
await context.AdminFollowup(string.Join(Environment.NewLine, report));
|
2023-10-24 13:25:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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())
|
|
|
|
|
{
|
2023-10-25 08:54:26 +00:00
|
|
|
|
await context.AdminFollowup("No deployments available.");
|
2023-10-24 13:25:45 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-25 08:54:26 +00:00
|
|
|
|
await context.AdminFollowup($"Deployments: {string.Join(", ", deployments.Select(FormatDeployment))}");
|
2023-10-24 13:25:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2023-10-25 08:54:26 +00:00
|
|
|
|
await context.AdminFollowup("Success!");
|
2023-10-24 13:43:07 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-10-25 08:54:26 +00:00
|
|
|
|
await context.AdminFollowup("That didn't work.");
|
2023-10-24 13:43:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class DeployRemoveCommand : SubCommandOption
|
|
|
|
|
{
|
|
|
|
|
private readonly DeploymentsFilesMonitor monitor;
|
|
|
|
|
private readonly StringOption stringOption = new StringOption(
|
2023-10-24 13:45:40 +00:00
|
|
|
|
name: "name",
|
2023-10-24 13:43:07 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2023-10-25 08:54:26 +00:00
|
|
|
|
await context.AdminFollowup("Success!");
|
2023-10-24 13:43:07 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-10-25 08:54:26 +00:00
|
|
|
|
await context.AdminFollowup("That didn't work.");
|
2023-10-24 13:43:07 +00:00
|
|
|
|
}
|
2023-10-24 13:25:45 +00:00
|
|
|
|
}
|
2023-10-24 12:07:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|