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

281 lines
9.9 KiB
C#
Raw Normal View History

2023-10-24 13:25:45 +00:00
using BiblioTech.Options;
using CodexPlugin;
2023-10-29 08:25:50 +00:00
using Core;
using Newtonsoft.Json;
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-25 09:25:27 +00:00
private readonly ClearUserAssociationCommand clearCommand = new ClearUserAssociationCommand();
private readonly ReportCommand reportCommand = new ReportCommand();
private readonly DeployListCommand deployListCommand = new DeployListCommand();
private readonly DeployUploadCommand deployUploadCommand = new DeployUploadCommand();
private readonly DeployRemoveCommand deployRemoveCommand = new DeployRemoveCommand();
private readonly WhoIsCommand whoIsCommand = new WhoIsCommand();
2023-10-29 08:25:50 +00:00
private readonly NetInfoCommand netInfoCommand;
public AdminCommand(CoreInterface ci)
{
netInfoCommand = new NetInfoCommand(ci);
}
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 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-25 09:25:27 +00:00
deployRemoveCommand,
whoIsCommand,
2023-10-29 08:25:50 +00:00
netInfoCommand
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
if (!IsInAdminChannel(context.Command))
{
2023-10-25 08:54:26 +00:00
await context.Followup("Please use admin commands only in the admin channel.");
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-25 09:25:27 +00:00
await whoIsCommand.CommandHandler(context);
2023-10-29 08:47:34 +00:00
await netInfoCommand.CommandHandler(context);
2023-10-24 12:07:15 +00:00
}
2023-10-24 13:25:45 +00:00
public class ClearUserAssociationCommand : SubCommandOption
{
2023-10-25 09:25:27 +00:00
private readonly UserOption userOption = new UserOption("User to clear Eth address for.", true);
2023-10-24 13:25:45 +00:00
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-25 09:25:27 +00:00
public override CommandOption[] Options => new[] { userOption };
2023-10-24 12:07:15 +00:00
2023-10-24 13:25:45 +00:00
protected override async Task onSubCommand(CommandContext context)
{
2023-10-25 09:25:27 +00:00
var user = userOption.GetUser(context);
if (user == null)
2023-10-24 13:25:45 +00:00
{
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;
}
2023-10-25 09:25:27 +00:00
Program.UserRepo.ClearUserAssociatedAddress(user);
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-25 09:25:27 +00:00
private readonly UserOption userOption = new UserOption(
2023-10-24 13:25:45 +00:00
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-25 09:25:27 +00:00
public override CommandOption[] Options => new[] { userOption };
2023-10-24 13:25:45 +00:00
protected override async Task onSubCommand(CommandContext context)
{
2023-10-25 09:25:27 +00:00
var user = userOption.GetUser(context);
if (user == null)
2023-10-24 13:25:45 +00:00
{
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;
}
2023-10-25 09:25:27 +00:00
var report = Program.UserRepo.GetInteractionReport(user);
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-25 09:25:27 +00:00
public DeployListCommand()
2023-10-24 13:25:45 +00:00
: base("list", "Lists current deployments.")
{
}
protected override async Task onSubCommand(CommandContext context)
{
2023-10-25 09:25:27 +00:00
var deployments = Program.DeploymentFilesMonitor.GetDeployments();
2023-10-24 13:25:45 +00:00
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 FileAttachementOption fileOption = new FileAttachementOption(
name: "json",
description: "Codex-deployment json to add.",
isRequired: true);
2023-10-25 09:25:27 +00:00
public DeployUploadCommand()
2023-10-24 13:25:45 +00:00
: base("add", "Upload a new deployment JSON file.")
{
}
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-25 09:25:27 +00:00
var result = await Program.DeploymentFilesMonitor.DownloadDeployment(file);
2023-10-24 13:43:07 +00:00
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 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);
2023-10-25 09:25:27 +00:00
public DeployRemoveCommand()
2023-10-24 13:43:07 +00:00
: base("remove", "Removes a deployment file.")
{
}
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;
2023-10-25 09:25:27 +00:00
var result = Program.DeploymentFilesMonitor.DeleteDeployment(str);
2023-10-24 13:43:07 +00:00
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
}
2023-10-25 09:25:27 +00:00
public class WhoIsCommand : SubCommandOption
{
private readonly UserOption userOption = new UserOption("User", isRequired: false);
private readonly EthAddressOption ethAddressOption = new EthAddressOption(isRequired: false);
public WhoIsCommand()
: base(name: "whois",
description: "Fetches info about a user or ethAddress in the testnet.")
{
}
public override CommandOption[] Options => new CommandOption[]
{
userOption,
ethAddressOption
};
protected override async Task onSubCommand(CommandContext context)
{
var user = userOption.GetUser(context);
var ethAddr = await ethAddressOption.Parse(context);
if (user != null)
{
await context.AdminFollowup(Program.UserRepo.GetUserReport(user));
}
if (ethAddr != null)
{
await context.AdminFollowup(Program.UserRepo.GetUserReport(ethAddr));
}
}
}
2023-10-29 08:25:50 +00:00
public class NetInfoCommand : SubCommandOption
{
private readonly CoreInterface ci;
public NetInfoCommand(CoreInterface ci)
: base(name: "netinfo",
description: "Fetches info endpoints of codex nodes.")
{
this.ci = ci;
}
protected override async Task onSubCommand(CommandContext context)
{
var deployment = Program.DeploymentFilesMonitor.GetDeployments().SingleOrDefault();
if (deployment == null)
{
await context.AdminFollowup("No deployment found.");
return;
}
try
{
var group = ci.WrapCodexContainers(deployment.CodexInstances.Select(i => i.Container).ToArray());
2023-10-29 08:47:34 +00:00
await context.AdminFollowup($"{group.Count()} Codex nodes.");
2023-10-29 08:25:50 +00:00
foreach (var node in group)
{
try
{
var info = node.GetDebugInfo();
var json = JsonConvert.SerializeObject(info);
await context.AdminFollowup($"Node '{node.GetName()}' responded with '{json}'");
}
catch (Exception ex)
{
await context.AdminFollowup($"Node '{node.GetName()}' failed to respond with exception: " + ex);
}
}
}
catch (Exception ex)
{
await context.AdminFollowup("Failed to wrap nodes with exception: " + ex);
}
}
}
2023-10-24 12:07:15 +00:00
}
}