diff --git a/commands/admin/newFactorio.go b/commands/admin/newFactorio.go index cd013b82..03fd4fb8 100755 --- a/commands/admin/newFactorio.go +++ b/commands/admin/newFactorio.go @@ -32,7 +32,8 @@ func NewMap(cmd *glob.CommandData, i *discordgo.InteractionCreate) { disc.EphemeralResponse(i, "Status:", "Generating new map.") fileName := fact.GenNewMap() - disc.FollowupResponse(i, &discordgo.WebhookParams{Content: "Map generator: " + fileName}) + + disc.EphemeralResponse(i, "Map generator", fileName) } /* Archive map */ @@ -162,15 +163,9 @@ func UpdateFactorio(cmd *glob.CommandData, i *discordgo.InteractionCreate) { _, msg, err, _ := factUpdater.DoQuickLatest(false) if err { - var elist []*discordgo.MessageEmbed - elist = append(elist, &discordgo.MessageEmbed{Title: "ERROR:", Description: "Factorio update failed: " + msg}) - f := discordgo.WebhookParams{Embeds: elist} - disc.FollowupResponse(i, &f) + disc.EphemeralResponse(i, "Error", "Factorio update failed: "+msg) } else { - var elist []*discordgo.MessageEmbed - elist = append(elist, &discordgo.MessageEmbed{Title: "Info:", Description: msg}) - f := discordgo.WebhookParams{Embeds: elist} - disc.FollowupResponse(i, &f) + disc.EphemeralResponse(i, "Info", msg) } } @@ -178,8 +173,5 @@ func InstallFactorio(cmd *glob.CommandData, i *discordgo.InteractionCreate) { disc.EphemeralResponse(i, "Info", "Installing Factorio...") _, msg, _, _ := factUpdater.DoQuickLatest(true) - var elist []*discordgo.MessageEmbed - elist = append(elist, &discordgo.MessageEmbed{Title: "Info:", Description: msg}) - f := discordgo.WebhookParams{Embeds: elist} - disc.FollowupResponse(i, &f) + disc.EphemeralResponse(i, "Info", msg) } diff --git a/commands/moderator/FTPLoad.go b/commands/moderator/FTPLoad.go index 9a1a7d78..4b89efb8 100755 --- a/commands/moderator/FTPLoad.go +++ b/commands/moderator/FTPLoad.go @@ -126,18 +126,18 @@ func LoadFTPFile(i *discordgo.InteractionCreate, file string, fType ftpTypeData) pass, _ := fact.CheckSave(pathPrefix, file+".zip", false) if pass { - disc.FollowupResponse(i, &discordgo.WebhookParams{Content: "Map appears to be valid!"}) + disc.EphemeralResponse(i, "Status", "Map appears to be valid!") } else { - disc.FollowupResponse(i, &discordgo.WebhookParams{Content: "Map appears to be invalid!"}) + disc.EphemeralResponse(i, "Status", "Map appears to be invalid!") } } else if fType.fType == TYPE_MODSETTINGS { //Mod settings here err := checkModSettings(pathPrefix + file) if err != nil { cwlog.DoLogCW("checkModSettings: Error: " + err.Error()) - disc.FollowupResponse(i, &discordgo.WebhookParams{Content: "The mod settings file appears to be invalid: " + err.Error()}) + disc.EphemeralResponse(i, "Error", "The mod settings file appears to be invalid: "+err.Error()) return } - disc.FollowupResponse(i, &discordgo.WebhookParams{Content: "Would load mod-settings here."}) + disc.EphemeralResponse(i, "Status", "Would load mod-settings here.") } else { //mod or modpack if fact.HasZipBomb(zipPath) { @@ -147,7 +147,7 @@ func LoadFTPFile(i *discordgo.InteractionCreate, file string, fType ftpTypeData) zip, err := zip.OpenReader(zipPath) if err != nil || zip == nil { - disc.FollowupResponse(i, &discordgo.WebhookParams{Content: "The zip file is invalid!"}) + disc.EphemeralResponse(i, "Error", "The zip file is invalid!") return } defer zip.Close() @@ -162,42 +162,42 @@ func LoadFTPFile(i *discordgo.InteractionCreate, file string, fType ftpTypeData) } } } - disc.FollowupResponse(i, &discordgo.WebhookParams{Content: "Would load modpack here."}) + disc.EphemeralResponse(i, "Error", "Would load modpack here.") } else if fType.fType == TYPE_MOD { for _, file := range zip.File { if path.Base(file.Name) == "info.json" { fc, err := file.Open() if err != nil { - disc.FollowupResponse(i, &discordgo.WebhookParams{Content: "The mod data could not be opened."}) + disc.EphemeralResponse(i, "Error", "The mod data could not be opened.") return } defer fc.Close() content, err := io.ReadAll(fc) if err != nil { - disc.FollowupResponse(i, &discordgo.WebhookParams{Content: "The mod data could not be read."}) + disc.EphemeralResponse(i, "Error", "The mod data could not be read.") return } jsonData := modInfoData{} err = json.Unmarshal(content, &jsonData) if err != nil { - disc.FollowupResponse(i, &discordgo.WebhookParams{Content: "The mod info could not be parsed."}) + disc.EphemeralResponse(i, "Error", "The mod info could not be parsed.") return } if len(jsonData.Author) < 2 || len(jsonData.Factorio_version) < 3 || len(jsonData.Name) < 3 || len(jsonData.Version) < 3 { - disc.FollowupResponse(i, &discordgo.WebhookParams{Content: "The mod data contains invalid data."}) + disc.EphemeralResponse(i, "Error", "The mod data contains invalid data.") return } - disc.FollowupResponse(i, &discordgo.WebhookParams{Content: "Would load the mod here."}) + disc.EphemeralResponse(i, "Error", "Would load the mod here.") return } } - disc.FollowupResponse(i, &discordgo.WebhookParams{Content: "The mod appears to be invalid or corrupted."}) + disc.EphemeralResponse(i, "Error", "The mod appears to be invalid or corrupted.") } } diff --git a/commands/user/NewModPack.go b/commands/user/NewModPack.go index dd8a39f2..f0e1ad3d 100755 --- a/commands/user/NewModPack.go +++ b/commands/user/NewModPack.go @@ -101,10 +101,7 @@ func makeModPack(i *discordgo.InteractionCreate, modsList []string) { err := makeZipFromFileList(modsList, cfg.Global.Paths.Folders.ModPack+packName) if err { buf := "Could not read/write the files, please inform moderators." - var elist []*discordgo.MessageEmbed - elist = append(elist, &discordgo.MessageEmbed{Title: "Error:", Description: buf}) - f := discordgo.WebhookParams{Embeds: elist} - disc.FollowupResponse(i, &f) + disc.EphemeralResponse(i, "Error", buf) return } else { @@ -123,10 +120,7 @@ func makeModPack(i *discordgo.InteractionCreate, modsList []string) { xTime.Unix(), ) - var elist []*discordgo.MessageEmbed - elist = append(elist, &discordgo.MessageEmbed{Title: "Success:", Description: buf}) - f := discordgo.WebhookParams{Embeds: elist} - disc.FollowupResponse(i, &f) + disc.EphemeralResponse(i, "Success", buf) } } diff --git a/disc/discUtils.go b/disc/discUtils.go index 1b8a9781..0780c37a 100755 --- a/disc/discUtils.go +++ b/disc/discUtils.go @@ -317,47 +317,34 @@ func InteractionResponse(i *discordgo.InteractionCreate, embed *discordgo.Messag } } -func FollowupResponse(i *discordgo.InteractionCreate, f *discordgo.WebhookParams) *discordgo.Message { +func EphemeralResponse(i *discordgo.InteractionCreate, title, message string) *discordgo.Message { + return EphemeralResponseColor(i, title, message, 0) +} + +func EphemeralResponseColor(i *discordgo.InteractionCreate, title, message string, color int) *discordgo.Message { if DS == nil { return nil } - if f.Embeds != nil { - cwlog.DoLogCW("FollowupResponse:\n" + i.Member.User.Username + "\n" + f.Embeds[0].Title + "\n" + f.Embeds[0].Description) + cwlog.DoLogCW("EphemeralResponse:\n" + i.Member.User.Username + "\n" + title + "\n" + message) - msg, err := DS.FollowupMessageCreate(i.Interaction, false, f) - if err != nil { - cwlog.DoLogCW(err.Error()) + embed := []*discordgo.MessageEmbed{{Title: title, Description: message, Color: 0x00ff00}} + if i.Interaction != nil { + resp := &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{Embeds: embed}, } - return msg - } else if f.Content != "" { - cwlog.DoLogCW("FollowupResponse:\n" + i.Member.User.Username + "\n" + f.Content) - - msg, err := DS.FollowupMessageCreate(i.Interaction, false, f) + err := DS.InteractionRespond(i.Interaction, resp) if err != nil { - cwlog.DoLogCW(err.Error()) + newResp := &discordgo.WebhookEdit{Embeds: &embed} + DS.InteractionResponseEdit(i.Interaction, newResp) } - return msg - } - - return nil -} - -func EphemeralResponse(i *discordgo.InteractionCreate, title, message string) { - if DS == nil { - return + return nil } - cwlog.DoLogCW("EphemeralResponse:\n" + i.Member.User.Username + "\n" + title + "\n" + message) - - var elist []*discordgo.MessageEmbed - elist = append(elist, &discordgo.MessageEmbed{Title: title, Description: message}) - - //1 << 6 is ephemeral/private - respData := &discordgo.InteractionResponseData{Embeds: elist, Flags: 1 << 6} - resp := &discordgo.InteractionResponse{Type: discordgo.InteractionResponseChannelMessageWithSource, Data: respData} - err := DS.InteractionRespond(i.Interaction, resp) + msg, err := DS.ChannelMessageSendComplex(i.ChannelID, &discordgo.MessageSend{Embeds: embed, Flags: discordgo.MessageFlagsEphemeral}) if err != nil { cwlog.DoLogCW(err.Error()) } + return msg } func SendMSG(channel string, embed *discordgo.MessageEmbed) *discordgo.Message { diff --git a/fact/factUtils.go b/fact/factUtils.go index 48ea432e..764e28ab 100755 --- a/fact/factUtils.go +++ b/fact/factUtils.go @@ -42,7 +42,6 @@ func CheckSave(path, name string, showError bool) (good bool, folder string) { fc, err := file.Open() if err != nil { - defer fc.Close() buf := fmt.Sprintf("Save '%v' is corrupted or invalid: '%v'.", name, err.Error()) if showError { @@ -51,6 +50,7 @@ func CheckSave(path, name string, showError bool) (good bool, folder string) { cwlog.DoLogCW(buf) break } else { + defer fc.Close() if strings.HasSuffix(file.Name, "level.dat0") { content, err := io.ReadAll(fc) if len(content) > (50*1024) && err == nil { @@ -808,8 +808,7 @@ func DoFTPLoad(i *discordgo.InteractionCreate, arg string) { } defer z.Close() - f := discordgo.WebhookParams{Content: buf, Flags: 1 << 6} - disc.FollowupResponse(i, &f) + disc.EphemeralResponse(i, "Status", buf) } func DoChangeMap(arg string) { diff --git a/fact/voteMap.go b/fact/voteMap.go index df7673ae..8e36d330 100755 --- a/fact/voteMap.go +++ b/fact/voteMap.go @@ -33,16 +33,14 @@ func CheckVote(i *discordgo.InteractionCreate, arg string) { if !FactorioBooted || !FactIsRunning { buf := "Factorio is not running." - f := discordgo.WebhookParams{Content: buf, Flags: 1 << 6} - disc.FollowupResponse(i, &f) + disc.EphemeralResponse(i, "Status", buf) return } /* Only if allowed */ if !disc.CheckRegular(i) && !disc.CheckModerator(i) && !disc.CheckAdmin(i) { buf := "You must have the `" + strings.ToUpper(cfg.Global.Discord.Roles.Regular) + "` Discord role to use this command. See /register and the read-this-first channel for more info." - f := discordgo.WebhookParams{Content: buf, Flags: 1 << 6} - disc.FollowupResponse(i, &f) + disc.EphemeralResponse(i, "Status", buf) return } @@ -67,16 +65,14 @@ func CheckVote(i *discordgo.InteractionCreate, arg string) { if notfound { buf := "That save doesn't exist." - f := discordgo.WebhookParams{Content: buf, Flags: 1 << 6} - disc.FollowupResponse(i, &f) + disc.EphemeralResponse(i, "Status", buf) return } good, _ := CheckSave(path, autoSaveStr, false) if !good { buf := fmt.Sprintf("The save game '%v' does not appear to be valid.", autoSaveStr) - f := discordgo.WebhookParams{Content: buf, Flags: 1 << 6} - disc.FollowupResponse(i, &f) + disc.EphemeralResponse(i, "Status", buf) return } } @@ -85,8 +81,7 @@ func CheckVote(i *discordgo.InteractionCreate, arg string) { if !glob.VoteBox.LastMapChange.IsZero() && time.Since(glob.VoteBox.LastMapChange) < constants.MapCooldownMins*time.Minute { left := (constants.MapCooldownMins * time.Minute).Round(time.Second) - time.Since(glob.VoteBox.LastMapChange) buf := fmt.Sprintf("The map can not be changed for another %v.", left.Round(time.Second).String()) - f := discordgo.WebhookParams{Content: buf, Flags: 1 << 6} - disc.FollowupResponse(i, &f) + disc.EphemeralResponse(i, "Status", buf) return } @@ -110,8 +105,7 @@ func CheckVote(i *discordgo.InteractionCreate, arg string) { var buf string buf = fmt.Sprintf("You have changed your vote to: %v", arg) - f := discordgo.WebhookParams{Content: buf, Flags: 1 << 6} - disc.FollowupResponse(i, &f) + disc.EphemeralResponse(i, "Status", buf) changedVote = true buf = fmt.Sprintf("%v has changed their vote to: %v", i.Member.User.Username, arg) @@ -120,16 +114,14 @@ func CheckVote(i *discordgo.InteractionCreate, arg string) { break } else if v.NumChanges >= constants.MaxVoteChanges { buf := "You can not change your vote anymore until it expires." - f := discordgo.WebhookParams{Content: buf, Flags: 1 << 6} - disc.FollowupResponse(i, &f) + disc.EphemeralResponse(i, "Status", buf) return } /* If they didn't change a already valid vote, then check cooldown */ if left > 0 && !changedVote { buf := "You can not vote again yet, you must wait " + left.Round(time.Second).String() + "." - f := discordgo.WebhookParams{Content: buf, Flags: 1 << 6} - disc.FollowupResponse(i, &f) + disc.EphemeralResponse(i, "Status", buf) return } @@ -152,8 +144,7 @@ func CheckVote(i *discordgo.InteractionCreate, arg string) { if foundVote && len(glob.VoteBox.Votes) >= vpos { /* sanity check */ if !disc.CheckModerator(i) && glob.VoteBox.Votes[vpos].TotalVotes >= constants.MaxVotesPerMap { buf := "You have used all of your allotted votes for this cycle." - f := discordgo.WebhookParams{Content: buf, Flags: 1 << 6} - disc.FollowupResponse(i, &f) + disc.EphemeralResponse(i, "Status", buf) return } else { glob.VoteBox.Votes[vpos] = newVote @@ -165,8 +156,7 @@ func CheckVote(i *discordgo.InteractionCreate, arg string) { var buf string buf = fmt.Sprintf("You have voted for: %v", arg) - f := discordgo.WebhookParams{Content: buf, Flags: 1 << 6} - disc.FollowupResponse(i, &f) + disc.EphemeralResponse(i, "Status", buf) buf = fmt.Sprintf("%v has voted for: %v", i.Member.User.Username, arg) LogGameCMS(true, cfg.Local.Channel.ChatChannel, buf)