From 1475795dca3f58a474e4d2c95783097aed1d17e9 Mon Sep 17 00:00:00 2001 From: Distortions81 Date: Fri, 1 Nov 2024 20:27:58 -0600 Subject: [PATCH] Updated factorio boot/close message, embed edits --- disc/discUtils.go | 28 ++++++++++++++++++++++++++++ fact/factUtils.go | 2 ++ glob/glob.go | 3 +++ support/factLauncher.go | 29 ++++++++++++++++++++++++----- support/pipeHandle.go | 10 +++++++--- 5 files changed, 64 insertions(+), 8 deletions(-) diff --git a/disc/discUtils.go b/disc/discUtils.go index 54c2a838..c5671db1 100755 --- a/disc/discUtils.go +++ b/disc/discUtils.go @@ -156,6 +156,34 @@ func SmartWriteDiscordEmbed(ch string, embed *discordgo.MessageEmbed) *discordgo return nil } +/* Send embedded message */ +func SmartEditDiscordEmbed(ch string, msg *discordgo.Message, title, description string, color int) *discordgo.Message { + + if ch == "" { + return nil + } + + if DS != nil { + if msg != nil && len(msg.Embeds) > 0 { + embed := msg.Embeds[0] + embed.Title = title + embed.Description = embed.Description + "\n" + description + embed.Color = color + msg, _ := DS.ChannelMessageEditEmbed(msg.ChannelID, msg.ID, embed) + return msg + } else { + msg, err := DS.ChannelMessageSendEmbed(ch, &discordgo.MessageEmbed{Title: title, Description: description, Color: color}) + + if err != nil { + cwlog.DoLogCW("SmartWriteDiscordEmbed: ERROR: %v", err) + } + return msg + } + } + + return nil +} + /*Send normal message to a channel*/ func SmartWriteDiscord(ch string, text string) *discordgo.Message { diff --git a/fact/factUtils.go b/fact/factUtils.go index 2eeabed7..9ac7d2d2 100755 --- a/fact/factUtils.go +++ b/fact/factUtils.go @@ -259,6 +259,7 @@ func WriteWhitelist() int { /* Quit Factorio */ func QuitFactorio(message string) { + glob.QuitMessage = disc.SmartWriteDiscordEmbed(cfg.Local.Channel.ChatChannel, &discordgo.MessageEmbed{Title: "Notice", Description: "Quitting Factorio: " + message, Color: 0xffff00}) if message == "" { message = "Server quitting." @@ -287,6 +288,7 @@ func QuitFactorio(message string) { cwlog.DoLogCW("Forcing factorio to close!!!") glob.FactorioCancel() } + } /* Send a string to Factorio, via stdin */ diff --git a/glob/glob.go b/glob/glob.go index e8a2a583..9ddeea25 100755 --- a/glob/glob.go +++ b/glob/glob.go @@ -127,6 +127,9 @@ type ChoiceData struct { } var ( + BootMessage *discordgo.Message + QuitMessage *discordgo.Message + FactorioContext context.Context FactorioCancel context.CancelFunc diff --git a/support/factLauncher.go b/support/factLauncher.go index db2b4e77..0cefdefd 100755 --- a/support/factLauncher.go +++ b/support/factLauncher.go @@ -21,6 +21,8 @@ import ( "ChatWire/disc" "ChatWire/fact" "ChatWire/glob" + + "github.com/bwmarrin/discordgo" ) var dlcNames = []string{"elevated-rails", "quality", "space-age"} @@ -326,6 +328,9 @@ func injectSoftMod(fileName, folderName string) { /* Create config files, launch factorio */ func launchFactorio() { + glob.BootMessage = nil + glob.QuitMessage = nil + if fact.FactIsRunning { cwlog.DoLogCW("launchFactorio: Factorio is already running.") return @@ -334,6 +339,8 @@ func launchFactorio() { glob.FactorioLock.Lock() defer glob.FactorioLock.Unlock() + msg := disc.SmartWriteDiscordEmbed(cfg.Local.Channel.ChatChannel, &discordgo.MessageEmbed{Title: "Notice", Description: "Launch Factorio...", Color: 0x00FF00}) + /* Allow crash reports right away */ glob.LastCrashReport = time.Time{} @@ -351,7 +358,8 @@ func launchFactorio() { cfg.Global.Paths.Folders.FactorioDir if _, err := os.Stat(checkFactPath); os.IsNotExist(err) { - fact.CMS(cfg.Local.Channel.ChatChannel, "Factorio does not appear to be installed. Use /factorio install-factorio to install it.") + disc.SmartEditDiscordEmbed(cfg.Local.Channel.ChatChannel, msg, "ERROR", "Factorio is not installed. Use `/factorio install-factorio` to install it.", 0xff0000) + cwlog.DoLogCW("Factorio does not appear to be installed at the configured path: " + checkFactPath) fact.FactAutoStart = false return @@ -360,7 +368,8 @@ func launchFactorio() { /* Find, test and load newest save game available */ found, fileName, folderName := GetSaveGame(true) if !found { - cwlog.DoLogCW("Unable to load any saves.") + disc.SmartEditDiscordEmbed(cfg.Local.Channel.ChatChannel, msg, "ERROR", "Unable to access save-games.", 0xff0000) + fact.FactAutoStart = false return } @@ -377,7 +386,9 @@ func launchFactorio() { /* Generate config file for Factorio server, if it fails stop everything.*/ if !fact.GenerateFactorioConfig() { fact.FactAutoStart = false - fact.LogCMS(cfg.Local.Channel.ChatChannel, "Unable to generate config file for Factorio server.") + + disc.SmartEditDiscordEmbed(cfg.Local.Channel.ChatChannel, msg, "ERROR", "Unable to write a config file for Fatorio.", 0xff0000) + return } @@ -388,7 +399,9 @@ func launchFactorio() { delay := throt * throt * 10 if delay > 0 { - fact.LogCMS(cfg.Local.Channel.ChatChannel, fmt.Sprintf("Rate limiting: Automatically rebooting Factorio in %d seconds.", delay)) + buf := fmt.Sprintf("Rate limiting: Waiting for %v seconds before launching Factorio.", delay) + disc.SmartEditDiscordEmbed(cfg.Local.Channel.ChatChannel, msg, "Warning", buf, 0xffff00) + for i := 0; i < delay*11 && throt > 0 && glob.ServerRunning && glob.RelaunchThrottle > 0; i++ { time.Sleep(100 * time.Millisecond) } @@ -459,7 +472,7 @@ func launchFactorio() { modFiles := GetModFiles() if len(modFiles) > 0 { - fact.LogGameCMS(false, cfg.Local.Channel.ChatChannel, "Loading mods...") + disc.SmartEditDiscordEmbed(cfg.Local.Channel.ChatChannel, msg, "Status", "Loading mods...", 0x00ff00) } if glob.FactorioCancel != nil { @@ -503,6 +516,8 @@ func launchFactorio() { /* Factorio is not happy. */ if errp != nil { fact.LogCMS(cfg.Local.Channel.ChatChannel, fmt.Sprintf("An error occurred when attempting to execute cmd.StdinPipe() Details: %s", errp)) + disc.SmartEditDiscordEmbed(cfg.Local.Channel.ChatChannel, msg, "ERROR", "Launching Factorio failed!", 0xff0000) + /* close lock */ fact.DoExit(true) return @@ -519,9 +534,13 @@ func launchFactorio() { err = cmd.Start() if err != nil { fact.LogCMS(cfg.Local.Channel.ChatChannel, fmt.Sprintf("An error occurred when attempting to start the game. Details: %s", err)) + disc.SmartEditDiscordEmbed(cfg.Local.Channel.ChatChannel, msg, "ERROR", "Launching Factorio failed!", 0xff0000) fact.DoExit(true) return } + + //msg = disc.SmartEditDiscordEmbed(cfg.Local.Channel.ChatChannel, msg, "Status", "Factorio loading.", 0x00ff00) + glob.BootMessage = msg } func ConfigSoftMod() { diff --git a/support/pipeHandle.go b/support/pipeHandle.go index c4aa2544..ec07ebe1 100755 --- a/support/pipeHandle.go +++ b/support/pipeHandle.go @@ -656,12 +656,15 @@ func handleFactGoodbye(input *handleData) bool { glob.FactorioCancel() glob.FactorioCancel = nil - cwlog.DoLogCW("Factorio has closed.") - fact.LogGameCMS(false, cfg.Local.Channel.ChatChannel, "Factorio is now offline.") fact.FactorioBooted = false fact.FactorioBootedAt = time.Time{} fact.SetFactRunning(false) + cwlog.DoLogCW("Factorio has closed.") + disc.SmartEditDiscordEmbed(cfg.Local.Channel.ChatChannel, glob.QuitMessage, "Notice", "Factorio is now offline.", 0xFF0000) + + glob.BootMessage = nil + glob.QuitMessage = nil return true } return false @@ -676,7 +679,8 @@ func handleFactReady(input *handleData) bool { fact.FactorioBooted = true fact.FactorioBootedAt = time.Now() fact.SetFactRunning(true) - fact.LogGameCMS(false, cfg.Local.Channel.ChatChannel, "Factorio "+fact.FactorioVersion+" is now online.") + cwlog.DoLogGame("Factorio " + fact.FactorioVersion + " is now online.") + glob.BootMessage = disc.SmartEditDiscordEmbed(cfg.Local.Channel.ChatChannel, glob.BootMessage, "Complete", "Factorio "+fact.FactorioVersion+" is now online.", 0x00ff00) fact.WriteFact("/sversion") fact.WriteFact(glob.OnlineCommand) }