Allow Discord bot to start before RCON is available
This commit is contained in:
+9
-8
@@ -22,9 +22,6 @@ int interval = int.Parse(Env("AUTO_RESTART_INTERVAL_HOURS", "6"));
|
||||
int[] warnings = Env("AUTO_RESTART_WARNING_SECONDS", "600,300,60").Split(',').Select(int.Parse).OrderDescending().ToArray();
|
||||
var cooldown = TimeSpan.FromSeconds(int.Parse(Env("MANUAL_RESTART_COOLDOWN_SECONDS", "600")));
|
||||
var readyTimeout = TimeSpan.FromSeconds(int.Parse(Env("RESTART_READY_TIMEOUT_SECONDS", "900")));
|
||||
var address = (await Dns.GetHostAddressesAsync(Env("RCON_HOST", "zomboid"))).First(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
|
||||
using var rcon = new RCON(address, ushort.Parse(Env("RCON_PORT", "27015")), rconPassword,
|
||||
strictCommandPacketIdMatching: false);
|
||||
var gate = new SemaphoreSlim(1, 1);
|
||||
DateTimeOffset? lastManual = null;
|
||||
|
||||
@@ -35,7 +32,14 @@ var host = builder.Build();
|
||||
|
||||
bool Allowed(ApplicationCommandContext c) => c.Interaction.GuildId == guildId && c.User is GuildInteractionUser u &&
|
||||
(roles.Count == 0 || u.RoleIds.Any(roles.Contains));
|
||||
async Task<string> Command(string command) => await rcon.SendCommandAsync(command);
|
||||
async Task<string> Command(string command, bool allowDisconnect = false)
|
||||
{
|
||||
var address = (await Dns.GetHostAddressesAsync(Env("RCON_HOST", "zomboid"))).First(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
|
||||
using var rcon = new RCON(address, ushort.Parse(Env("RCON_PORT", "27015")), rconPassword, strictCommandPacketIdMatching: false);
|
||||
await rcon.ConnectAsync();
|
||||
try { return await rcon.SendCommandAsync(command); }
|
||||
catch when (allowDisconnect && !rcon.Connected) { return ""; }
|
||||
}
|
||||
async Task<(int Count, string[] Names)> Players()
|
||||
{
|
||||
string output = await Command("players");
|
||||
@@ -47,10 +51,7 @@ async Task<(int Count, string[] Names)> Players()
|
||||
return (count, names);
|
||||
}
|
||||
Task Broadcast(string message) => Command($"servermsg \"{message.Replace('"', '\'')}\"");
|
||||
async Task Quit()
|
||||
{
|
||||
try { await Command("quit"); } catch { if (rcon.Connected) throw; }
|
||||
}
|
||||
Task Quit() => Command("quit", true);
|
||||
async Task Ready(string kind)
|
||||
{
|
||||
bool stopped = false;
|
||||
|
||||
Reference in New Issue
Block a user