summaryrefslogtreecommitdiff
path: root/config/config.go
diff options
context:
space:
mode:
Diffstat (limited to 'config/config.go')
-rw-r--r--config/config.go90
1 files changed, 90 insertions, 0 deletions
diff --git a/config/config.go b/config/config.go
new file mode 100644
index 0000000..e9f08b5
--- /dev/null
+++ b/config/config.go
@@ -0,0 +1,90 @@
+package config
+
+import (
+ "ai/types"
+ "ai/utils/logger"
+ "os"
+ "strconv"
+ "strings"
+
+ "github.com/joho/godotenv"
+)
+
+var Config *types.BotConfig
+
+func init() {
+ logPrefix := "Config"
+ logOptions := types.LogOptions{
+ Timestamp: true,
+ Prefix: logPrefix,
+ Level: types.Error,
+ Fatal: true,
+ }
+
+ if err := godotenv.Load(); err != nil {
+ logger.Log("Failed to load environment variables", logOptions)
+ }
+
+ Config = &types.BotConfig{
+ DiscordToken: getEnv("DISCORD_TOKEN"),
+ SpotifyClientId: getEnv("SPOTIFY_CLIENT_ID"),
+ SpotifyClientSecret: getEnv("SPOTIFY_CLIENT_SECRET"),
+ YoutubeAPIKey: getEnv("YOUTUBE_API_KEY"),
+ Activity: types.ActivityType(getIntEnv("ACTIVITY")),
+ ActivityMessage: getEnv("ACTIVITY_MESSAGE"),
+ }
+
+ if Config.DiscordToken == "" {
+ logger.Log("Unable to read Discord token. environment variable DISCORD_TOKEN is required", logOptions)
+ }
+
+ if Config.SpotifyClientId == "" {
+ logger.Log("Unable to read Spotify client ID. environment variable SPOTIFY_CLIENT_ID is required", logOptions)
+ }
+
+ if Config.SpotifyClientSecret == "" {
+ logger.Log("Unable to read Spotify client secret. environment variable SPOTIFY_CLIENT_SECRET is required", logOptions)
+ }
+
+ if Config.YoutubeAPIKey == "" {
+ logger.Log("Unable to read YouTube API key. environment variable YOUTUBE_API_KEY is required", logOptions)
+ }
+
+ if Config.Activity == 0 {
+ logOptions.Level = types.Warn
+ logOptions.Fatal = false
+ logger.Log("Activity message is empty or not set. Defaulting to PLAYING", logOptions)
+ Config.Activity = types.PLAYING
+ }
+
+ if Config.ActivityMessage == "" {
+ logOptions.Level = types.Warn
+ logOptions.Fatal = false
+ logger.Log("Activity message is empty or not set. Defaulting to empty string", logOptions)
+ Config.ActivityMessage = ""
+ }
+
+ logOptions.Level = types.Success
+ logOptions.Fatal = false
+ logger.Log("Config loaded successfully", logOptions)
+}
+
+func getEnv(key string) string {
+ value, exists := os.LookupEnv(key)
+ if !exists {
+ return ""
+ }
+ return strings.TrimSpace(value)
+}
+
+func getIntEnv(key string) int {
+ value := getEnv(key)
+ if value == "" {
+ return 0
+ }
+ i, err := strconv.Atoi(value)
+ if err != nil {
+ return 0
+ }
+ return i
+}