feat: Add error if configs

This commit is contained in:
Siwoo Jeon 2025-04-03 18:21:17 +09:00
parent 0583945249
commit 5d79a22ebb
Signed by: migan
GPG key ID: 036E9A8C5E8E48DA
3 changed files with 24 additions and 14 deletions

View file

@ -8,7 +8,6 @@ import (
"github.com/joho/godotenv"
)
type botConfig struct {
Token string
Prefix string
@ -29,23 +28,35 @@ type MuffinConfig struct {
func loadConfig() *MuffinConfig {
err := godotenv.Load()
if err != nil {
fmt.Println("[goMuffin] 봇의 설절파일을 불러올 수가 없어요.")
log.Println("[goMuffin] 봇의 설절파일을 불러올 수가 없어요.")
log.Fatalln(err)
}
config := MuffinConfig{Bot: botConfig{}, Train: trainConfig{}}
setConfig(&config)
config := &MuffinConfig{Bot: botConfig{}, Train: trainConfig{}}
setConfig(config)
return &config
return config
}
func getRequiredValue(key string) string {
value := os.Getenv(key)
if value == "" {
log.Fatalln(fmt.Sprintf("[goMuffin] .env 파일에서 필요한 %s값이 없어요.", key))
}
return value
}
func getValue(key string) string {
return os.Getenv(key)
}
func setConfig(config *MuffinConfig) {
config.Bot.Prefix = os.Getenv("BOT_PREFIX")
config.Bot.Token = os.Getenv("BOT_TOKEN")
config.Bot.OwnerId = os.Getenv("BOT_OWNER_ID")
config.Bot.Prefix = getRequiredValue("BOT_PREFIX")
config.Bot.Token = getRequiredValue("BOT_TOKEN")
config.Bot.OwnerId = getRequiredValue("BOT_OWNER_ID")
config.Train.UserID = os.Getenv("TRAIN_USER_ID")
config.Train.UserID = getValue("TRAIN_USER_ID")
config.DatabaseURL = os.Getenv("DATABASE_URL")
config.DatabaseURL = getRequiredValue("DATABASE_URL")
}
var Config *MuffinConfig = loadConfig()

View file

@ -7,7 +7,7 @@ import (
"git.wh64.net/muffin/goMuffin/utils"
)
const MUFFIN_VERSION = "0.0.0-gopher_canary.250331b"
const MUFFIN_VERSION = "0.0.0-gopher_dev.250403a"
var updatedString string = utils.Decimals.FindAllStringSubmatch(MUFFIN_VERSION, -1)[3][0]

View file

@ -2,7 +2,6 @@ package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
@ -22,7 +21,7 @@ func main() {
dg, err := discordgo.New("Bot " + config.Bot.Token)
if err != nil {
fmt.Println("[goMuffin] 봇의 세션을 만들수가 없어요.")
log.Println("[goMuffin] 봇의 세션을 만들수가 없어요.")
log.Fatalln(err)
}
@ -46,7 +45,7 @@ func main() {
}
}()
fmt.Println("[goMuffin] 봇이 실행되고 있어요. 버전:", configs.MUFFIN_VERSION)
log.Println("[goMuffin] 봇이 실행되고 있어요. 버전:", configs.MUFFIN_VERSION)
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc