Deals with timeout for operations that may take a while.

This commit is contained in:
benbierens 2023-10-20 11:20:38 +02:00
parent 8910c7ff27
commit 869aeb9253
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
7 changed files with 53 additions and 7 deletions

View File

@ -6,6 +6,7 @@ namespace BiblioTech
public abstract class BaseCommand
{
public abstract string Name { get; }
public abstract string StartingMessage { get; }
public abstract string Description { get; }
public virtual CommandOption[] Options
{
@ -18,7 +19,17 @@ namespace BiblioTech
public async Task SlashCommandHandler(SocketSlashCommand command)
{
if (command.CommandName != Name) return;
await Invoke(command);
try
{
await command.RespondAsync(StartingMessage);
await Invoke(command);
}
catch (Exception ex)
{
await command.FollowupAsync("Something failed while trying to do that...");
Console.WriteLine(ex);
}
}
protected abstract Task Invoke(SocketSlashCommand command);

View File

@ -21,12 +21,12 @@ namespace BiblioTech
var deployments = monitor.GetDeployments();
if (deployments.Length == 0)
{
await command.RespondAsync("No deployments are currently available.");
await command.FollowupAsync("No deployments are currently available.");
return;
}
if (deployments.Length > 1)
{
await command.RespondAsync("Multiple deployments are online. I don't know which one to pick!");
await command.FollowupAsync("Multiple deployments are online. I don't know which one to pick!");
return;
}

View File

@ -0,0 +1,31 @@
using Discord.WebSocket;
namespace BiblioTech
{
public class DeploymentsCommand : BaseCommand
{
private readonly DeploymentsFilesMonitor monitor;
public DeploymentsCommand(DeploymentsFilesMonitor monitor)
{
this.monitor = monitor;
}
public override string Name => "deployments";
public override string StartingMessage => "Fetching deployments information...";
public override string Description => "Lists known deployments";
protected override async Task Invoke(SocketSlashCommand command)
{
var deployments = monitor.GetDeployments();
if (!deployments.Any())
{
await command.FollowupAsync("No deployments available.");
return;
}
await command.FollowupAsync($"Deployments: {string.Join(", ", deployments.Select(d => d.Metadata.StartUtc.ToString("o")))}");
}
}
}

View File

@ -44,7 +44,9 @@ namespace BiblioTech
var handler = new CommandHandler(client,
new GetBalanceCommand(monitor, ci),
new MintCommand(monitor, ci));
new MintCommand(monitor, ci),
new DeploymentsCommand(monitor)
);
await client.LoginAsync(TokenType.Bot, Config.ApplicationToken);
await client.StartAsync();

View File

@ -6,7 +6,7 @@ namespace BiblioTech.TokenCommands
public class EthAddressOption : CommandOption
{
public EthAddressOption()
: base(name: "EthAddress",
: base(name: "ethaddress",
description: "Ethereum address starting with '0x'.",
type: Discord.ApplicationCommandOptionType.String)
{

View File

@ -16,6 +16,7 @@ namespace BiblioTech.TokenCommands
}
public override string Name => "balance";
public override string StartingMessage => "Fetching balance...";
public override string Description => "Shows Eth and TestToken balance of an eth address.";
public override CommandOption[] Options => new[] { ethOption };

View File

@ -18,6 +18,7 @@ namespace BiblioTech.TokenCommands
}
public override string Name => "mint";
public override string StartingMessage => "Minting some tokens...";
public override string Description => "Mint some TestTokens and send some Eth to the address if its balance is low.";
public override CommandOption[] Options => new[] { ethOption };
@ -26,11 +27,11 @@ namespace BiblioTech.TokenCommands
var addr = await ethOption.Parse(command);
if (addr == null) return;
var report =
var report =
ProcessEth(gethNode, addr) +
ProcessTestTokens(gethNode, contracts, addr);
await command.RespondAsync(report);
await command.FollowupAsync(report);
}
private string ProcessTestTokens(IGethNode gethNode, ICodexContracts contracts, EthAddress addr)