using System.Diagnostics; namespace Aeshnidae.Invasions; /// /// The warning and the outcome, to Discord through the VPS's notify-discord.sh (the script /// the ops tools use), on the channel key in Settings.Discord.Channel. No channel: logged /// only - the default, so a staging server never posts where players read. Fire-and-forget /// on the thread pool; the world thread never waits on Discord. /// public static class Discord { public static string LastResult { get; private set; } = "never posted"; public static void Post(string text) { var d = Mod.Settings.Discord; if (string.IsNullOrWhiteSpace(d.Channel)) { LastResult = "no channel set - logged only"; ModManager.Log($"[{Mod.Name}] Discord (no channel): {text.Replace('\n', ' ')}"); return; } var script = (d.NotifyScript ?? "").Replace("~", Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)); if (string.IsNullOrWhiteSpace(script) || !File.Exists(script)) { LastResult = $"notify script {script} not found - logged only"; ModManager.Log($"[{Mod.Name}] Discord ({LastResult}): {text.Replace('\n', ' ')}"); return; } _ = Task.Run(() => { try { var psi = new ProcessStartInfo { FileName = "/bin/bash", UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, }; psi.ArgumentList.Add(script); psi.ArgumentList.Add(d.Channel); psi.ArgumentList.Add(text); using var proc = Process.Start(psi); if (proc is null) { LastResult = "notify script did not start"; } else { proc.WaitForExit(20000); LastResult = proc.ExitCode == 0 ? $"posted {DateTime.UtcNow:yyyy-MM-dd HH:mm} UTC ({d.Channel})" : $"notify-discord.sh exited {proc.ExitCode}: {proc.StandardError.ReadToEnd().Trim()}"; } } catch (Exception ex) { LastResult = $"notify script failed: {ex.Message}"; } ModManager.Log($"[{Mod.Name}] Discord: {LastResult}"); }); } }