Updates response message instead of deleting it.

This commit is contained in:
benbierens 2023-11-02 15:04:53 +01:00
parent fe11a97458
commit 438c4271e1
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
4 changed files with 70 additions and 33 deletions

View File

@ -23,9 +23,9 @@ namespace BiblioTech
try try
{ {
await command.RespondAsync(StartingMessage, ephemeral: true); var context = new CommandContext(command, command.Data.Options);
await Invoke(new CommandContext(command, command.Data.Options)); await command.RespondAsync(StartingMessage, ephemeral: IsEphemeral(context));
await command.DeleteOriginalResponseAsync(); await Invoke(context);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -34,6 +34,12 @@ namespace BiblioTech
} }
} }
private bool IsEphemeral(CommandContext context)
{
if (IsSenderAdmin(context.Command) && IsInAdminChannel(context.Command)) return false;
return true;
}
protected abstract Task Invoke(CommandContext context); protected abstract Task Invoke(CommandContext context);
protected bool IsSenderAdmin(SocketSlashCommand command) protected bool IsSenderAdmin(SocketSlashCommand command)

View File

@ -78,12 +78,12 @@ namespace BiblioTech.Commands
var user = userOption.GetUser(context); var user = userOption.GetUser(context);
if (user == null) if (user == null)
{ {
await context.AdminFollowup("Failed to get user ID"); await context.Followup("Failed to get user ID");
return; return;
} }
Program.UserRepo.ClearUserAssociatedAddress(user); Program.UserRepo.ClearUserAssociatedAddress(user);
await context.AdminFollowup("Done."); await context.Followup("Done.");
} }
} }
@ -105,12 +105,20 @@ namespace BiblioTech.Commands
var user = userOption.GetUser(context); var user = userOption.GetUser(context);
if (user == null) if (user == null)
{ {
await context.AdminFollowup("Failed to get user ID"); await context.Followup("Failed to get user ID");
return; return;
} }
var report = Program.UserRepo.GetInteractionReport(user); var report = string.Join(Environment.NewLine, Program.UserRepo.GetInteractionReport(user));
await context.AdminFollowup(string.Join(Environment.NewLine, report)); if (report.Length > 1900)
{
var filename = $"user-{user.Username}.log";
await context.FollowupWithAttachement(filename, report);
}
else
{
await context.Followup(report);
}
} }
} }
@ -127,11 +135,12 @@ namespace BiblioTech.Commands
if (!deployments.Any()) if (!deployments.Any())
{ {
await context.AdminFollowup("No deployments available."); await context.Followup("No deployments available.");
return; return;
} }
await context.AdminFollowup($"Deployments: {string.Join(", ", deployments.Select(FormatDeployment))}"); var nl = Environment.NewLine;
await context.Followup($"Deployments:{nl}{string.Join(nl, deployments.Select(FormatDeployment))}");
} }
private string FormatDeployment(CodexDeployment deployment) private string FormatDeployment(CodexDeployment deployment)
@ -163,11 +172,11 @@ namespace BiblioTech.Commands
var result = await Program.DeploymentFilesMonitor.DownloadDeployment(file); var result = await Program.DeploymentFilesMonitor.DownloadDeployment(file);
if (result) if (result)
{ {
await context.AdminFollowup("Success!"); await context.Followup("Success!");
} }
else else
{ {
await context.AdminFollowup("That didn't work."); await context.Followup("That didn't work.");
} }
} }
} }
@ -194,11 +203,11 @@ namespace BiblioTech.Commands
var result = Program.DeploymentFilesMonitor.DeleteDeployment(str); var result = Program.DeploymentFilesMonitor.DeleteDeployment(str);
if (result) if (result)
{ {
await context.AdminFollowup("Success!"); await context.Followup("Success!");
} }
else else
{ {
await context.AdminFollowup("That didn't work."); await context.Followup("That didn't work.");
} }
} }
} }
@ -227,11 +236,11 @@ namespace BiblioTech.Commands
if (user != null) if (user != null)
{ {
await context.AdminFollowup(Program.UserRepo.GetUserReport(user)); await context.Followup(Program.UserRepo.GetUserReport(user));
} }
if (ethAddr != null) if (ethAddr != null)
{ {
await context.AdminFollowup(Program.UserRepo.GetUserReport(ethAddr)); await context.Followup(Program.UserRepo.GetUserReport(ethAddr));
} }
} }
} }
@ -246,23 +255,23 @@ namespace BiblioTech.Commands
this.ci = ci; this.ci = ci;
} }
protected async Task OnDeployment(CommandContext context, Func<ICodexNodeGroup, Task> action) protected async Task OnDeployment(CommandContext context, Func<ICodexNodeGroup, string, Task> action)
{ {
var deployment = Program.DeploymentFilesMonitor.GetDeployments().SingleOrDefault(); var deployment = Program.DeploymentFilesMonitor.GetDeployments().SingleOrDefault();
if (deployment == null) if (deployment == null)
{ {
await context.AdminFollowup("No deployment found."); await context.Followup("No deployment found.");
return; return;
} }
try try
{ {
var group = ci.WrapCodexContainers(deployment.CodexInstances.Select(i => i.Container).ToArray()); var group = ci.WrapCodexContainers(deployment.CodexInstances.Select(i => i.Container).ToArray());
await action(group); await action(group, deployment.Metadata.Name);
} }
catch (Exception ex) catch (Exception ex)
{ {
await context.AdminFollowup("Failed to wrap nodes with exception: " + ex); await context.Followup("Failed to wrap nodes with exception: " + ex);
} }
} }
} }
@ -277,24 +286,31 @@ namespace BiblioTech.Commands
protected override async Task onSubCommand(CommandContext context) protected override async Task onSubCommand(CommandContext context)
{ {
await OnDeployment(context, async group => await OnDeployment(context, async (group, name) =>
{ {
await context.AdminFollowup($"{group.Count()} Codex nodes."); var nl = Environment.NewLine;
var content = new List<string>
{
$"{DateTime.UtcNow.ToString("o")} - {group.Count()} Codex nodes."
};
foreach (var node in group) foreach (var node in group)
{ {
try try
{ {
var info = node.GetDebugInfo(); var info = node.GetDebugInfo();
var nl = Environment.NewLine;
var json = JsonConvert.SerializeObject(info, Formatting.Indented); var json = JsonConvert.SerializeObject(info, Formatting.Indented);
var jsonInsert = $"{nl}```{nl}{json}{nl}```{nl}"; var jsonInsert = $"{nl}```{nl}{json}{nl}```{nl}";
await context.AdminFollowup($"Node '{node.GetName()}' responded with {jsonInsert}"); content.Add($"Node '{node.GetName()}' responded with {jsonInsert}");
} }
catch (Exception ex) catch (Exception ex)
{ {
await context.AdminFollowup($"Node '{node.GetName()}' failed to respond with exception: " + ex); content.Add($"Node '{node.GetName()}' failed to respond with exception: " + ex);
} }
} }
var filename = $"netinfo-{NoWhitespaces(name)}.log";
await context.FollowupWithAttachement(filename, string.Join(nl, content.ToArray()));
}); });
} }
} }
@ -316,9 +332,9 @@ namespace BiblioTech.Commands
var peerId = await peerIdOption.Parse(context); var peerId = await peerIdOption.Parse(context);
if (string.IsNullOrEmpty(peerId)) return; if (string.IsNullOrEmpty(peerId)) return;
await OnDeployment(context, async group => await OnDeployment(context, async (group, name) =>
{ {
await context.AdminFollowup($"Calling debug/peer for '{peerId}' on {group.Count()} Codex nodes."); await context.Followup($"Calling debug/peer for '{peerId}' on {group.Count()} Codex nodes.");
foreach (var node in group) foreach (var node in group)
{ {
try try
@ -327,15 +343,20 @@ namespace BiblioTech.Commands
var nl = Environment.NewLine; var nl = Environment.NewLine;
var json = JsonConvert.SerializeObject(info, Formatting.Indented); var json = JsonConvert.SerializeObject(info, Formatting.Indented);
var jsonInsert = $"{nl}```{nl}{json}{nl}```{nl}"; var jsonInsert = $"{nl}```{nl}{json}{nl}```{nl}";
await context.AdminFollowup($"Node '{node.GetName()}' responded with {jsonInsert}"); await context.Followup($"Node '{node.GetName()}' responded with {jsonInsert}");
} }
catch (Exception ex) catch (Exception ex)
{ {
await context.AdminFollowup($"Node '{node.GetName()}' failed to respond with exception: " + ex); await context.Followup($"Node '{node.GetName()}' failed to respond with exception: " + ex);
} }
} }
}); });
} }
} }
private static string NoWhitespaces(string s)
{
return s.Replace(" ", "-");
}
} }
} }

View File

@ -15,12 +15,19 @@ namespace BiblioTech.Options
public async Task Followup(string message) public async Task Followup(string message)
{ {
await Command.FollowupAsync(message, ephemeral: true); await Command.ModifyOriginalResponseAsync(m =>
{
m.Content = message;
});
} }
public async Task AdminFollowup(string message) public async Task FollowupWithAttachement(string filename, string content)
{ {
await Command.FollowupAsync(message); using var fileStream = new MemoryStream();
using var streamWriter = new StreamWriter(fileStream);
await streamWriter.WriteAsync(content);
await Command.FollowupWithFileAsync(fileStream, filename);
} }
} }
} }

View File

@ -46,7 +46,10 @@ namespace BiblioTech
public string[] GetInteractionReport(IUser user) public string[] GetInteractionReport(IUser user)
{ {
var result = new List<string>(); var result = new List<string>
{
$"User report create on {DateTime.UtcNow.ToString("o")}"
};
lock (repoLock) lock (repoLock)
{ {