Skip to content

Commit

Permalink
Updated factorio boot/close message, embed edits
Browse files Browse the repository at this point in the history
  • Loading branch information
Distortions81 committed Nov 2, 2024
1 parent a1e34a4 commit 1475795
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 8 deletions.
28 changes: 28 additions & 0 deletions disc/discUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
2 changes: 2 additions & 0 deletions fact/factUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -287,6 +288,7 @@ func QuitFactorio(message string) {
cwlog.DoLogCW("Forcing factorio to close!!!")
glob.FactorioCancel()
}

}

/* Send a string to Factorio, via stdin */
Expand Down
3 changes: 3 additions & 0 deletions glob/glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ type ChoiceData struct {
}

var (
BootMessage *discordgo.Message
QuitMessage *discordgo.Message

FactorioContext context.Context
FactorioCancel context.CancelFunc

Expand Down
29 changes: 24 additions & 5 deletions support/factLauncher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"ChatWire/disc"
"ChatWire/fact"
"ChatWire/glob"

"github.com/bwmarrin/discordgo"
)

var dlcNames = []string{"elevated-rails", "quality", "space-age"}
Expand Down Expand Up @@ -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
Expand All @@ -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{}

Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
}

Expand All @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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() {
Expand Down
10 changes: 7 additions & 3 deletions support/pipeHandle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down

0 comments on commit 1475795

Please sign in to comment.