/
mods.go
81 lines (70 loc) · 1.9 KB
/
mods.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package utils
import (
"encoding/json"
"fmt"
"io/ioutil"
"github.com/IIPoliII/FactorioServerManager/support"
"github.com/bwmarrin/discordgo"
)
// ModJson is struct containing a slice of Mod.
type ModJson struct {
Mods []Mod
}
// Mod is a struct containing info about a mod.
type Mod struct {
Name string
Enabled bool
}
func modListEmbed(ModList *ModJson) *discordgo.MessageEmbed {
fields := []*discordgo.MessageEmbedField{}
var enabled, disabled int
var S = "mod"
if len(ModList.Mods) > 1 {
S = "mods"
}
for _, mod := range ModList.Mods {
var value string
if mod.Enabled {
value = "Enabled"
enabled = enabled + 1
} else {
value = "Disabled"
disabled = disabled + 1
}
fields = append(fields, &discordgo.MessageEmbedField{
Name: mod.Name,
Value: value,
})
}
embed := &discordgo.MessageEmbed{
Type: "rich",
Color: 52,
Description: fmt.Sprintf("%d total %s (%d enabled, %d disabled)", len(ModList.Mods),
S, enabled, disabled),
Title: "Mods",
Fields: fields,
}
return embed
}
// ModsList returns the list of mods running on the server.
func ModsList(s *discordgo.Session, m *discordgo.MessageCreate) {
ModList := &ModJson{}
Json, err := ioutil.ReadFile(support.Config.ModListLocation)
// Don't exit on this error, just sent message to the channel!
if err != nil {
s.ChannelMessageSend(m.ChannelID,
fmt.Sprintf("Sorry, there was an error reading your mods list, did you specify it in the .env file? Error details: %s", err))
return
}
err = json.Unmarshal(Json, &ModList)
if err != nil {
s.ChannelMessageSend(m.ChannelID,
fmt.Sprintf("Sorry, there was an error reading your mods list. Error details: %s", err))
return
}
_, err = s.ChannelMessageSendEmbed(m.ChannelID, modListEmbed(ModList))
if err != nil {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Sorry, there was an error with the discord embed message Error details: %s", err))
}
return
}