Replaces debug/info fetching with list of known SPRs, settable via admin commands.

This commit is contained in:
benbierens 2023-11-21 09:38:58 +01:00
parent 54471d41d5
commit a84d6a3c22
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
4 changed files with 106 additions and 61 deletions

View File

@ -1,27 +0,0 @@
using BiblioTech.Options;
using CodexPlugin;
using Core;
namespace BiblioTech
{
public abstract class BaseCodexCommand : BaseDeploymentCommand
{
private readonly CoreInterface ci;
public BaseCodexCommand(CoreInterface ci)
{
this.ci = ci;
}
protected override async Task ExecuteDeploymentCommand(CommandContext context, CodexDeployment codexDeployment)
{
var codexContainers = codexDeployment.CodexInstances.Select(c => c.Containers).ToArray();
var group = ci.WrapCodexContainers(codexContainers);
await Execute(context, group);
}
protected abstract Task Execute(CommandContext context, ICodexNodeGroup codexGroup);
}
}

View File

@ -15,11 +15,17 @@ namespace BiblioTech.Commands
private readonly WhoIsCommand whoIsCommand = new WhoIsCommand(); private readonly WhoIsCommand whoIsCommand = new WhoIsCommand();
private readonly NetInfoCommand netInfoCommand; private readonly NetInfoCommand netInfoCommand;
private readonly DebugPeerCommand debugPeerCommand; private readonly DebugPeerCommand debugPeerCommand;
private readonly AddSprCommand addSprCommand;
private readonly ClearSprsCommand clearSprsCommand;
private readonly GetSprCommand getSprCommand;
public AdminCommand(CoreInterface ci) public AdminCommand(CoreInterface ci, SprCommand sprCommand)
{ {
netInfoCommand = new NetInfoCommand(ci); netInfoCommand = new NetInfoCommand(ci);
debugPeerCommand = new DebugPeerCommand(ci); debugPeerCommand = new DebugPeerCommand(ci);
addSprCommand = new AddSprCommand(sprCommand);
clearSprsCommand = new ClearSprsCommand(sprCommand);
getSprCommand = new GetSprCommand(sprCommand);
} }
public override string Name => "admin"; public override string Name => "admin";
@ -35,7 +41,10 @@ namespace BiblioTech.Commands
deployRemoveCommand, deployRemoveCommand,
whoIsCommand, whoIsCommand,
netInfoCommand, netInfoCommand,
debugPeerCommand debugPeerCommand,
addSprCommand,
clearSprsCommand,
getSprCommand
}; };
protected override async Task Invoke(CommandContext context) protected override async Task Invoke(CommandContext context)
@ -60,6 +69,9 @@ namespace BiblioTech.Commands
await whoIsCommand.CommandHandler(context); await whoIsCommand.CommandHandler(context);
await netInfoCommand.CommandHandler(context); await netInfoCommand.CommandHandler(context);
await debugPeerCommand.CommandHandler(context); await debugPeerCommand.CommandHandler(context);
await addSprCommand.CommandHandler(context);
await clearSprsCommand.CommandHandler(context);
await deployUploadCommand.CommandHandler(context);
} }
public class ClearUserAssociationCommand : SubCommandOption public class ClearUserAssociationCommand : SubCommandOption
@ -379,5 +391,77 @@ namespace BiblioTech.Commands
return content.ToArray(); return content.ToArray();
} }
} }
public class AddSprCommand : SubCommandOption
{
private readonly SprCommand sprCommand;
private readonly StringOption stringOption = new StringOption("spr", "Codex SPR", true);
public AddSprCommand(SprCommand sprCommand)
: base(name: "addspr",
description: "Adds a Codex SPR, to be given to users with '/boot'.")
{
this.sprCommand = sprCommand;
}
public override CommandOption[] Options => new[] { stringOption };
protected override async Task onSubCommand(CommandContext context)
{
var spr = await stringOption.Parse(context);
if (!string.IsNullOrEmpty(spr) )
{
sprCommand.Add(spr);
await context.Followup("A-OK!");
}
else
{
await context.Followup("SPR is null or empty.");
}
}
}
public class ClearSprsCommand : SubCommandOption
{
private readonly SprCommand sprCommand;
private readonly StringOption stringOption = new StringOption("areyousure", "set to 'true' if you are.", true);
public ClearSprsCommand(SprCommand sprCommand)
: base(name: "clearsprs",
description: "Clears all Codex SPRs in the bot. Users won't be able to use '/boot' till new ones are added.")
{
this.sprCommand = sprCommand;
}
public override CommandOption[] Options => new[] { stringOption };
protected override async Task onSubCommand(CommandContext context)
{
var areyousure = await stringOption.Parse(context);
if (areyousure != "true") return;
sprCommand.Clear();
await context.Followup("Cleared all SPRs.");
}
}
public class GetSprCommand: SubCommandOption
{
private readonly SprCommand sprCommand;
public GetSprCommand(SprCommand sprCommand)
: base(name: "getsprs",
description: "Shows all Codex SPRs in the bot.")
{
this.sprCommand = sprCommand;
}
protected override async Task onSubCommand(CommandContext context)
{
await context.Followup("SPRs: " + string.Join(", ", sprCommand.Get().Select(s => $"'{s}'")));
}
}
} }
} }

View File

@ -1,61 +1,48 @@
using BiblioTech.Options; using BiblioTech.Options;
using CodexPlugin;
using Core;
namespace BiblioTech.Commands namespace BiblioTech.Commands
{ {
public class SprCommand : BaseCodexCommand public class SprCommand : BaseCommand
{ {
private readonly Random random = new Random(); private readonly Random random = new Random();
private readonly List<string> sprCache = new List<string>(); private readonly List<string> knownSprs = new List<string>();
private DateTime lastUpdate = DateTime.MinValue;
public SprCommand(CoreInterface ci) : base(ci)
{
}
public override string Name => "boot"; public override string Name => "boot";
public override string StartingMessage => RandomBusyMessage.Get(); public override string StartingMessage => RandomBusyMessage.Get();
public override string Description => "Gets an SPR. (Signed peer record, used for bootstrapping.)"; public override string Description => "Gets an SPR. (Signed peer record, used for bootstrapping.)";
protected override async Task<bool> OnInvoke(CommandContext context) protected override async Task Invoke(CommandContext context)
{ {
if (ShouldUpdate()) await ReplyWithRandomSpr(context);
{
return true;
} }
await ReplyWithRandomSpr(context); public void Add(string spr)
return false; {
if (knownSprs.Contains(spr)) return;
knownSprs.Add(spr);
} }
protected override async Task Execute(CommandContext context, ICodexNodeGroup codexGroup) public void Clear()
{ {
lastUpdate = DateTime.UtcNow; knownSprs.Clear();
sprCache.Clear(); }
var infos = codexGroup.Select(c => c.GetDebugInfo()).ToArray(); public string[] Get()
sprCache.AddRange(infos.Select(i => i.spr)); {
return knownSprs.ToArray();
await ReplyWithRandomSpr(context);
} }
private async Task ReplyWithRandomSpr(CommandContext context) private async Task ReplyWithRandomSpr(CommandContext context)
{ {
if (!sprCache.Any()) if (!knownSprs.Any())
{ {
await context.Followup("I'm sorry, no SPRs are available... :c"); await context.Followup("I'm sorry, no SPRs are available... :c");
return; return;
} }
var i = random.Next(0, sprCache.Count); var i = random.Next(0, knownSprs.Count);
var spr = sprCache[i]; var spr = knownSprs[i];
await context.Followup($"Your SPR: `{spr}`"); await context.Followup($"Your SPR: `{spr}`");
} }
private bool ShouldUpdate()
{
return (DateTime.UtcNow - lastUpdate) > TimeSpan.FromMinutes(10);
}
} }
} }

View File

@ -49,12 +49,13 @@ namespace BiblioTech
var ci = entryPoint.CreateInterface(); var ci = entryPoint.CreateInterface();
var associateCommand = new UserAssociateCommand(); var associateCommand = new UserAssociateCommand();
var sprCommand = new SprCommand();
var handler = new CommandHandler(client, var handler = new CommandHandler(client,
new GetBalanceCommand(ci, associateCommand), new GetBalanceCommand(ci, associateCommand),
new MintCommand(ci, associateCommand), new MintCommand(ci, associateCommand),
new SprCommand(ci), sprCommand,
associateCommand, associateCommand,
new AdminCommand(ci) new AdminCommand(ci, sprCommand)
); );
await client.LoginAsync(TokenType.Bot, Config.ApplicationToken); await client.LoginAsync(TokenType.Bot, Config.ApplicationToken);