50 lines
1.5 KiB
C#
50 lines
1.5 KiB
C#
using AnsonBot.Core;
|
|
using Discord.Interactions;
|
|
|
|
namespace AnsonBot.Bot;
|
|
|
|
public class ServerModule : InteractionModuleBase<SocketInteractionContext>
|
|
{
|
|
private readonly IRconService _rcon;
|
|
private readonly PlayerTrackerService _tracker;
|
|
|
|
public ServerModule(IRconService rcon, PlayerTrackerService tracker)
|
|
{
|
|
_rcon = rcon;
|
|
_tracker = tracker;
|
|
}
|
|
|
|
[SlashCommand("players", "Show who's online in Palworld")]
|
|
public async Task PlayersAsync()
|
|
{
|
|
await DeferAsync();
|
|
|
|
try
|
|
{
|
|
// Live call so the list is current rather than up to a poll stale.
|
|
var raw = await _rcon.SendAsync("ShowPlayers");
|
|
var online = ShowPlayersParser.Parse(raw);
|
|
|
|
if (online.Count == 0)
|
|
{
|
|
await FollowupAsync("Nobody's online.");
|
|
return;
|
|
}
|
|
|
|
var sessions = (await _tracker.GetOpenSessionsAsync())
|
|
.ToDictionary(s => s.PlayerId);
|
|
|
|
var lines = online.Select(p =>
|
|
sessions.TryGetValue(p.Id, out var s)
|
|
? $"• {p.Name} — {Humanize.Duration(DateTime.UtcNow - s.StartedUtc)}"
|
|
// Joined since the last poll, so no session row exists yet.
|
|
: $"• {p.Name} — just joined");
|
|
|
|
await FollowupAsync($"**{online.Count} online:**\n{string.Join("\n", lines)}");
|
|
}
|
|
catch
|
|
{
|
|
await FollowupAsync("Couldn't reach the Palworld server.", ephemeral: true);
|
|
}
|
|
}
|
|
}
|