Skip to content

Commit

Permalink
Auto edit responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Distortions81 committed Nov 1, 2024
1 parent b6ea0b8 commit 7eac8bb
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 86 deletions.
18 changes: 5 additions & 13 deletions commands/admin/newFactorio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -162,24 +163,15 @@ 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)
}
}

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)
}
24 changes: 12 additions & 12 deletions commands/moderator/FTPLoad.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
Expand All @@ -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.")
}
}

Expand Down
10 changes: 2 additions & 8 deletions commands/user/NewModPack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

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

}
Expand Down
47 changes: 17 additions & 30 deletions disc/discUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions fact/factUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
30 changes: 10 additions & 20 deletions fact/voteMap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

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

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

Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 7eac8bb

Please sign in to comment.