CookieMod/config.go

73 lines
1.9 KiB
Go

package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"time"
)
type CookieConfig struct {
Token string `json:"token"`
GuildID string `json:"guild_id"`
// config for modules
OpenWeatherMapAPIKey string `json:"openweathermap_api_key"`
}
type PermsConfig struct {
Roles []struct {
RoleID string `json:"role_id"`
Permissions []string `json:"permissions"`
} `json:"roles"`
}
var (
config CookieConfig
twitchConfig Twitch_Config
started time.Time = time.Now()
)
func ConfigCheck() CookieConfig {
hasMainConfig, configFolder := FileExists("data/config.json")
if hasMainConfig {
// load config
data, err := os.ReadFile("data/config.json")
if err != nil {
log.Fatal(fmt.Sprintf("Couldn't read data/config.json. (File). Reason: %s", err.Error()))
}
config := CookieConfig{}
err = json.Unmarshal(data, &config)
if err != nil {
log.Fatal(fmt.Sprintf("Couldn't read data/config.json. (JSON Unmarshal). Reason: %s", err.Error()))
}
return config
} else {
if !configFolder {
// create new
config := &CookieConfig{}
log.Info("Creating new config.")
token := SecretPrompt("Please enter your token > ")
config.Token = token
fmt.Println(strings.Repeat("*", len(token)))
guildID := SecretPrompt("Please enter your guild id > ")
fmt.Println(strings.Repeat("*", len(guildID)))
config.GuildID = guildID
data, err := json.Marshal(config)
if err != nil {
log.Fatal(fmt.Sprintf("Couldn't save the config! (JSON Marshal). Reason: %s", err.Error()))
}
err = os.WriteFile("data/config.json", data, 0777)
if err != nil {
log.Fatal(fmt.Sprintf("Couldn't save the config! (File). Reason: %s", err.Error()))
}
log.Info("First time configuration done! Please restart!")
os.Exit(0)
} else {
log.Fatal("Please remove the folder at data/config.json")
}
}
log.Fatal("No config could be returned!")
return CookieConfig{}
}