-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
317 lines (284 loc) · 10.6 KB
/
config.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package main
import (
"encoding/json"
"io/ioutil"
"os"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/fatih/color"
"github.com/muhammadmuzzammil1998/jsonc"
)
var (
config = defaultConfiguration()
)
//#region Credentials
var (
placeholderToken string = "REPLACE_WITH_YOUR_TOKEN_OR_DELETE_LINE"
placeholderEmail string = "REPLACE_WITH_YOUR_EMAIL_OR_DELETE_LINE"
placeholderPassword string = "REPLACE_WITH_YOUR_PASSWORD_OR_DELETE_LINE"
)
type configurationCredentials struct {
// Login
Token string `json:"token,omitempty"` // required for bot token (this or login)
Email string `json:"email,omitempty"` // required for login (this or token)
Password string `json:"password,omitempty"` // required for login (this or token)
UserBot bool `json:"userBot,omitempty"` // required
// APIs
SpotifyClientID string `json:"spotifyClientID,omitempty"` // optional
SpotifyClientSecret string `json:"spotifyClientSecret,omitempty"` // optional
//twitter?
//youtube?
//flickr?
}
//#endregion
//#region Configuration
var (
defConfig_CommandPrefix string = "dub "
defConfig_GithubUpdateChecking bool = true
defConfig_Presence_Enabled bool = true
defConfig_Presence_Details string = "<< STATUS {{presenceCount}} @ {{presenceDuration}} >>"
)
func defaultConfiguration() configuration {
return configuration{
// Credentials
Credentials: configurationCredentials{
Token: placeholderToken,
Email: placeholderEmail,
Password: placeholderPassword,
},
// Setup
CommandPrefix: defConfig_CommandPrefix,
LogLevel: logLevelInfo,
ExitOnBadConnection: false,
GithubUpdateChecking: defConfig_GithubUpdateChecking,
// Appearance
Presence: []configurationPresence{
{
Enabled: &defConfig_Presence_Enabled,
Type: string(discordgo.StatusOnline),
Label: discordgo.GameTypeGame,
Status: "DUB {{dubVersion}}",
StatusDetails: defConfig_Presence_Details,
Duration: 15,
},
{
Enabled: &defConfig_Presence_Enabled,
Type: string(discordgo.StatusOnline),
Label: discordgo.GameTypeGame,
Status: "{{xsysMemAvailable}} RAM free",
StatusDetails: defConfig_Presence_Details,
Duration: 15,
},
{
Enabled: &defConfig_Presence_Enabled,
Type: string(discordgo.StatusOnline),
Label: discordgo.GameTypeListening,
Status: "{{numServers}} servers",
StatusDetails: defConfig_Presence_Details,
Duration: 15,
},
{
Enabled: &defConfig_Presence_Enabled,
Type: string(discordgo.StatusIdle),
Label: discordgo.GameTypeWatching,
Status: "for {{uptime}}",
StatusDetails: defConfig_Presence_Details,
Duration: 30,
},
{
Enabled: &defConfig_Presence_Enabled,
Type: string(discordgo.StatusIdle),
Label: discordgo.GameTypeWatching,
Status: "server for {{xsysUptime}}",
StatusDetails: defConfig_Presence_Details,
Duration: 30,
},
},
// Discord
DiscordAdmins: []string{},
DiscordLogLevel: discordgo.LogError,
DiscordTimeout: 180,
DiscordCheckPerms: true,
MessageOutput: true,
}
}
type configurationPresence struct {
Enabled *bool `json:"enabled"`
Type string `json:"type"` // Online, Idle, DND, Invisible
Label discordgo.GameType `json:"label"` // Playing[0], Streaming[1], Listening[2], Watching[3], Custom[4,DOESNT WORK]
Status string `json:"status"` // text
StatusDetails string `json:"statusDetails"` // text
Duration int `json:"duration"` // seconds
}
type configuration struct {
Constants map[string]string `json:"_constants,omitempty"`
// Required
Credentials configurationCredentials `json:"credentials"` // required
// System
RebootScheduleSys *int `json:"rebootScheduleSys,omitempty"` // optional, hours
RebootScheduleBot *int `json:"rebootScheduleBot,omitempty"` // optional, hours
// Setup
CommandPrefix string `json:"commandPrefix"` // optional, defaults
LogLevel int `json:"logLevel"` // optional, defaults
ExitOnBadConnection bool `json:"exitOnBadConnection"` // optional, defaults
GithubUpdateChecking bool `json:"githubUpdateChecking"` // optional, defaults
// Appearance
Presence []configurationPresence `json:"presence"`
//EmbedColor *string `json:"embedColor"` // optional, defaults to role if undefined, then defaults random if no role color
// Discord
DiscordAdmins []string `json:"discordAdmins"` // optional
DiscordLogLevel int `json:"discordLogLevel"` // optional, defaults
DiscordTimeout int `json:"discordTimeout"` // optional, defaults
DiscordCheckPerms bool `json:"discordCheckPerms"` // optional, defaults
MessageOutput bool `json:"messageOutput"` // optional, defaults
//All *configurationTarget `json:"all,omitempty"` // optional, defaults
//AllBlacklistChannels *[]string `json:"allBlacklistChannels,omitempty"` // optional
//AllBlacklistServers *[]string `json:"allBlacklistServers,omitempty"` // optional
PermittedServers []configurationTarget `json:"permittedServers"` // required
OutputChannels []configurationOutput `json:"outputChannels"` // required
}
type configurationTarget struct {
Server string `json:"server,omitempty"` // used for config.PermittedServers
Servers *[]string `json:"servers,omitempty"` // ---> alternative to Server
UnlockCommands bool `json:"unlockCommands"` // optional, defaults
}
type configurationOutput struct {
Channel string `json:"channel,omitempty"` // used for config.OutputChannels
Channels *[]string `json:"channels,omitempty"` // ---> alternative to Channel
OutputProgram bool `json:"outputProgram,omitempty"` // optional, defaults
}
func isServerPermitted(serverID string) bool {
for _, server := range config.PermittedServers {
if serverID == server.Server {
return true
}
if server.Servers != nil {
for _, nestedServer := range *server.Servers {
if serverID == nestedServer {
return true
}
}
}
}
return false
}
func getPermittedServerConfig(serverID string) configurationTarget {
for _, server := range config.PermittedServers {
if serverID == server.Server {
return server
}
if server.Servers != nil {
for _, nestedServer := range *server.Servers {
if serverID == nestedServer {
return server
}
}
}
}
return configurationTarget{}
}
//#endregion
func initConfig() {
if _, err := os.Stat(configFileBase + ".jsonc"); err == nil {
configFile = configFileBase + ".jsonc"
configFileC = true
} else {
configFile = configFileBase + ".json"
configFileC = false
}
}
func loadConfig() {
// Determine json type
if _, err := os.Stat(configFileBase + ".jsonc"); err == nil {
configFile = configFileBase + ".jsonc"
configFileC = true
} else {
configFile = configFileBase + ".json"
configFileC = false
}
// .
dubLog("Settings", logLevelInfo, color.YellowString, "Loading from \"%s\"...", configFile)
// Load settings
configContent, err := ioutil.ReadFile(configFile)
if err != nil {
dubLog("Settings", logLevelFatal, color.HiRedString, "Failed to open file...\t%s", err)
//createConfig()
properExit()
} else {
fixed := string(configContent)
// Fix backslashes
fixed = strings.ReplaceAll(fixed, "\\", "\\\\")
for strings.Contains(fixed, "\\\\\\") {
fixed = strings.ReplaceAll(fixed, "\\\\\\", "\\\\")
}
//TODO: Not even sure if this is realistic to do but would be nice to have line comma & trailing comma fixing
// Parse
newConfig := defaultConfiguration()
if configFileC {
err = jsonc.Unmarshal([]byte(fixed), &newConfig)
} else {
err = json.Unmarshal([]byte(fixed), &newConfig)
}
if err != nil {
dubLog("Settings", logLevelFatal, color.HiRedString, "Failed to parse settings file...\t%s", err)
dubLog("Settings", logLevelWarning, color.MagentaString, "Please ensure you're following proper JSON format syntax.")
properExit()
}
// Constants
if newConfig.Constants != nil {
for key, value := range newConfig.Constants {
if strings.Contains(fixed, key) {
fixed = strings.ReplaceAll(fixed, key, value)
}
}
// Re-parse
newConfig = defaultConfiguration()
if configFileC {
err = jsonc.Unmarshal([]byte(fixed), &newConfig)
} else {
err = json.Unmarshal([]byte(fixed), &newConfig)
}
if err != nil {
dubLog("Settings", logLevelFatal, color.HiRedString, "Failed to re-parse settings file after replacing constants...\t%s", err)
dubLog("Settings", logLevelWarning, color.MagentaString, "Please ensure you're following proper JSON format syntax.")
properExit()
}
newConfig.Constants = nil
}
config = newConfig
// Debug Output
if logLevelDebug <= config.LogLevel {
dupeConfig := config
if dupeConfig.Credentials.Token != "" && dupeConfig.Credentials.Token != placeholderToken {
dupeConfig.Credentials.Token = "STRIPPED_FOR_OUTPUT"
}
if dupeConfig.Credentials.Email != "" && dupeConfig.Credentials.Email != placeholderEmail {
dupeConfig.Credentials.Email = "STRIPPED_FOR_OUTPUT"
}
if dupeConfig.Credentials.Password != "" && dupeConfig.Credentials.Password != placeholderPassword {
dupeConfig.Credentials.Password = "STRIPPED_FOR_OUTPUT"
}
if dupeConfig.Credentials.SpotifyClientID != "" {
dupeConfig.Credentials.SpotifyClientID = "STRIPPED_FOR_OUTPUT"
}
if dupeConfig.Credentials.SpotifyClientSecret != "" {
dupeConfig.Credentials.SpotifyClientSecret = "STRIPPED_FOR_OUTPUT"
}
s, err := json.MarshalIndent(dupeConfig, "", "\t")
if err != nil {
dubLog("Debug", logLevelDebug, color.HiRedString, "Failed to output...\t%s", err)
} else {
dubLog("Debug", logLevelDebug, color.HiYellowString, "Loaded Settings:\n%s", string(s))
}
}
// Credentials Check
if (config.Credentials.Token == "" || config.Credentials.Token == placeholderToken) &&
(config.Credentials.Email == "" || config.Credentials.Email == placeholderEmail) &&
(config.Credentials.Password == "" || config.Credentials.Password == placeholderPassword) {
dubLog("Discord", logLevelFatal, color.HiRedString, "No valid discord login found. Login is invalid...")
dubLog("Discord", logLevelWarning, color.HiYellowString, "Please save your credentials & info into \"%s\" then restart...", configFile)
dubLog("Discord", logLevelWarning, color.MagentaString, "If your credentials are already properly saved, please ensure you're following proper JSON format syntax.")
properExit()
}
}
}