fix: Add config prefix

This commit is contained in:
Siwoo Jeon 2024-10-09 21:08:17 +09:00
parent 65820fc23a
commit 2f8d2135c9
Signed by: migan
GPG key ID: 036E9A8C5E8E48DA
4 changed files with 45 additions and 36 deletions

View file

@ -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
}

37
configs/config.go Normal file
View file

@ -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()

View file

@ -3,17 +3,19 @@ package handler
import ( import (
"strings" "strings"
"github.com/Muffin-laboratory/goMuffin/configs"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
// MessageCreate is handlers of messageCreate event // MessageCreate is handlers of messageCreate event
func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
config := configs.Config
if m.Author.ID == s.State.User.ID && m.Author.Bot { if m.Author.ID == s.State.User.ID && m.Author.Bot {
return return
} }
if strings.HasPrefix(m.Content, "머핀아 ") { if strings.HasPrefix(m.Content, config.Prefix) {
content := strings.TrimPrefix(m.Content, "머핀아 ") content := strings.TrimPrefix(m.Content, config.Prefix)
if content == "안녕" { if content == "안녕" {
s.ChannelMessageSend(m.ChannelID, "안녕") s.ChannelMessageSend(m.ChannelID, "안녕")
} }

View file

@ -7,14 +7,15 @@ import (
"os/signal" "os/signal"
"syscall" "syscall"
"github.com/Muffin-laboratory/goMuffin/configs"
"github.com/Muffin-laboratory/goMuffin/handler" "github.com/Muffin-laboratory/goMuffin/handler"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
func main() { func main() {
config := LoadConfig() config := configs.Config
dg, err := discordgo.New("Bot " + config.token) dg, err := discordgo.New("Bot " + config.Token)
if err != nil { if err != nil {
fmt.Println("[goMuffin] 봇의 세션을 만들수가 없어요.") fmt.Println("[goMuffin] 봇의 세션을 만들수가 없어요.")
log.Fatalln(err) log.Fatalln(err)
@ -24,7 +25,7 @@ func main() {
dg.Open() dg.Open()
fmt.Println("[goMuffin] 봇이 실행되고 있어요.") fmt.Println("[goMuffin] 봇이 실행되고 있어요. 버전:", configs.MUFFIN_VERSION)
sc := make(chan os.Signal, 1) sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc <-sc