Skip to content

Commit

Permalink
fix: make status an adapter based button, allowing adhoc interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
cruikshj committed May 19, 2024
1 parent 9b572dc commit e6f179a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/ServerManagerDiscordBot/ServerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public async Task<ServerInfo> GetServerInfoAsync(string name, CancellationToken
return server;
}

public async Task<ServerStatus?> GetServerStatusAsync(string name, CancellationToken cancellationToken = default)
public async Task<ServerStatus> GetServerStatusAsync(string name, CancellationToken cancellationToken = default)
{
var server = await GetServerInfoAsync(name, cancellationToken);

Expand All @@ -58,7 +58,7 @@ public async Task<ServerInfo> GetServerInfoAsync(string name, CancellationToken
return await adapter.GetServerStatusAsync(cancellationToken);
}

return null;
return ServerStatus.Unknown;
}

public async Task StartServerAsync(string name, bool wait = false, CancellationToken cancellationToken = default)
Expand Down
54 changes: 28 additions & 26 deletions src/ServerManagerDiscordBot/ServersCommandModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class ServersCommandModule(
IOptions<AppSettings> appSettings,
ServerManager serverManager,
ILargeFileDownloadHandler largeFileDownloadHandler,
ILogger<ServersCommandModule> logger)
ILogger<ServersCommandModule> logger)
: InteractionModuleBase
{
public AppSettings AppSettings { get; } = appSettings.Value;
Expand All @@ -17,7 +17,7 @@ public class ServersCommandModule(
public ILogger Logger { get; } = logger;

[SlashCommand("servers", "Display server information.")]
public async Task Servers([Autocomplete(typeof(ServersAutocompleteHandler))]string? name = null)
public async Task Servers([Autocomplete(typeof(ServersAutocompleteHandler))] string? name = null)
{
await DeferAsync(ephemeral: true);

Expand Down Expand Up @@ -64,17 +64,6 @@ private async Task List()
private async Task Info(string name)
{
var info = await ServerManager.GetServerInfoAsync(name);
ServerStatus? status = null;
try
{
status = await ServerManager.GetServerStatusAsync(name);
}
catch (Exception ex)
{
status = ServerStatus.Unknown;
Logger.LogError(ex, "Error getting status for server '{Name}'.", name);
}

var embed = new EmbedBuilder();

embed.Author = new EmbedAuthorBuilder();
Expand All @@ -95,19 +84,13 @@ private async Task Info(string name)
embed.AddField(field.Key, field.Value);
}

if (status is not null)
{
embed.AddField("Status", status);
}

embed.WithCurrentTimestamp();

var component = new ComponentBuilder();

if (!string.IsNullOrWhiteSpace(info.HostAdapterName))
{
var hostActionsRow = new ActionRowBuilder();
hostActionsRow
.WithButton("Status", $"status|{name}", ButtonStyle.Secondary)
.WithButton("Start", $"start|{name}", ButtonStyle.Success)
.WithButton("Restart", $"restart|{name}", ButtonStyle.Primary)
.WithButton("Stop", $"stop|{name}", ButtonStyle.Danger)
Expand Down Expand Up @@ -162,7 +145,7 @@ public async Task Stop(string name)
await RespondAsync($"The `{name}` server is stopping...", ephemeral: true);
try
{
await ServerManager.StopServerAsync(name, wait: true);
await ServerManager.StopServerAsync(name, wait: true);

await FollowupAsync($"{Context.User.GlobalName} stopped the `{name}` server.");
}
Expand All @@ -177,7 +160,7 @@ public async Task Stop(string name)
public async Task Restart(string name)
{
await RespondAsync($"The `{name}` server is restarting...", ephemeral: true);

try
{
await ServerManager.StopServerAsync(name, wait: true);
Expand Down Expand Up @@ -216,7 +199,8 @@ public async Task Files(string name)

var selectMenu = new SelectMenuBuilder();
selectMenu.CustomId = $"file|{name}";
selectMenu.WithOptions(files.Select(f => new SelectMenuOptionBuilder() {
selectMenu.WithOptions(files.Select(f => new SelectMenuOptionBuilder()
{
Label = f.Name,
Value = f.Name
}).ToList());
Expand All @@ -243,7 +227,7 @@ public async Task DownloadFile(string name, string fileName)
await FollowupAsync("File downloads are disabled.", ephemeral: true);
return;
}

await DeferAsync(ephemeral: true);
try
{
Expand Down Expand Up @@ -310,7 +294,25 @@ public async Task Readme(string name)
await FollowupAsync($"Error: {ex.Message}", ephemeral: true);
}
}


[ComponentInteraction("status|*")]
public async Task Status(string name)
{
await DeferAsync(ephemeral: true);

try
{
var status = await ServerManager.GetServerStatusAsync(name);

await FollowupAsync($"The status of the `{name}` server is `{status}`.", ephemeral: true);
}
catch (Exception ex)
{
Logger.LogError(ex, "Error in status interaction for server '{Name}'.", name);
await FollowupAsync($"Error: {ex.Message}", ephemeral: true);
}
}

[ComponentInteraction("logs|*")]
public async Task Logs(string name)
{
Expand All @@ -322,7 +324,7 @@ public async Task Logs(string name)
if (!logs.Any())
{
await FollowupAsync($"No logs for the `{name}` server are available.", ephemeral: true);
return;
return;
}

var fileAttachments = logs.Select(log => new FileAttachment(log.Value, $"{log.Key}.log")).ToArray();
Expand Down

0 comments on commit e6f179a

Please sign in to comment.