From 2f8d2135c9e3d549a5d53e24d1edb75a1a7438e8 Mon Sep 17 00:00:00 2001 From: Siwoo Jeon Date: Wed, 9 Oct 2024 21:08:17 +0900 Subject: [PATCH] fix: Add config prefix --- config.go | 31 ------------------------------- configs/config.go | 37 +++++++++++++++++++++++++++++++++++++ handler/messageCreate.go | 6 ++++-- main.go | 7 ++++--- 4 files changed, 45 insertions(+), 36 deletions(-) delete mode 100644 config.go create mode 100644 configs/config.go diff --git a/config.go b/config.go deleted file mode 100644 index 4e02186..0000000 --- a/config.go +++ /dev/null @@ -1,31 +0,0 @@ -package main - -import ( - "fmt" - "log" - "os" - - "github.com/joho/godotenv" -) - -type Config struct { - token string -} - -// LoadConfig a config -func LoadConfig() *Config { - err := godotenv.Load() - if err != nil { - fmt.Println("[goMuffin] 봇의 설절파일을 불러올 수가 없어요.") - log.Fatalln(err) - } - config := Config{} - setConfig(&config) - - return &config -} - -func setConfig(config *Config) { - token := os.Getenv("TOKEN") - config.token = token -} diff --git a/configs/config.go b/configs/config.go new file mode 100644 index 0000000..2bbcf7d --- /dev/null +++ b/configs/config.go @@ -0,0 +1,37 @@ +package configs + +import ( + "fmt" + "log" + "os" + + "github.com/joho/godotenv" +) + +var MUFFIN_VERSION = "0.0.0-gopher.e241009a" + +// MuffinConfig for Muffin bot +type MuffinConfig struct { + Token string + Prefix string +} + +func loadConfig() *MuffinConfig { + err := godotenv.Load() + if err != nil { + fmt.Println("[goMuffin] 봇의 설절파일을 불러올 수가 없어요.") + log.Fatalln(err) + } + config := MuffinConfig{} + setConfig(&config) + + return &config +} + +func setConfig(config *MuffinConfig) { + token := os.Getenv("TOKEN") + config.Prefix = os.Getenv("PREFIX") + config.Token = token +} + +var Config *MuffinConfig = loadConfig() diff --git a/handler/messageCreate.go b/handler/messageCreate.go index 962330e..daaa892 100644 --- a/handler/messageCreate.go +++ b/handler/messageCreate.go @@ -3,17 +3,19 @@ package handler import ( "strings" + "github.com/Muffin-laboratory/goMuffin/configs" "github.com/bwmarrin/discordgo" ) // MessageCreate is handlers of messageCreate event func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { + config := configs.Config if m.Author.ID == s.State.User.ID && m.Author.Bot { return } - if strings.HasPrefix(m.Content, "머핀아 ") { - content := strings.TrimPrefix(m.Content, "머핀아 ") + if strings.HasPrefix(m.Content, config.Prefix) { + content := strings.TrimPrefix(m.Content, config.Prefix) if content == "안녕" { s.ChannelMessageSend(m.ChannelID, "안녕") } diff --git a/main.go b/main.go index 12d60a5..893368a 100644 --- a/main.go +++ b/main.go @@ -7,14 +7,15 @@ import ( "os/signal" "syscall" + "github.com/Muffin-laboratory/goMuffin/configs" "github.com/Muffin-laboratory/goMuffin/handler" "github.com/bwmarrin/discordgo" ) func main() { - config := LoadConfig() + config := configs.Config - dg, err := discordgo.New("Bot " + config.token) + dg, err := discordgo.New("Bot " + config.Token) if err != nil { fmt.Println("[goMuffin] 봇의 세션을 만들수가 없어요.") log.Fatalln(err) @@ -24,7 +25,7 @@ func main() { dg.Open() - fmt.Println("[goMuffin] 봇이 실행되고 있어요.") + fmt.Println("[goMuffin] 봇이 실행되고 있어요. 버전:", configs.MUFFIN_VERSION) sc := make(chan os.Signal, 1) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) <-sc