From 15e9d93abdc9586c002d2773c5715fdc8bc1b655 Mon Sep 17 00:00:00 2001 From: iyear Date: Tue, 15 Jun 2021 10:00:51 +0800 Subject: [PATCH] independent config --- bots/bots.go | 95 +++++++++++++++++++++++------------------------- config/config.go | 9 ++++- 2 files changed, 53 insertions(+), 51 deletions(-) diff --git a/bots/bots.go b/bots/bots.go index 29a3681..1d55473 100644 --- a/bots/bots.go +++ b/bots/bots.go @@ -2,6 +2,9 @@ package bots import ( "fmt" + "github.com/iyear/E5SubBot/config" + "github.com/iyear/E5SubBot/logger" + "github.com/iyear/E5SubBot/model" "github.com/iyear/E5SubBot/task" "github.com/robfig/cron/v3" "github.com/spf13/viper" @@ -14,9 +17,7 @@ import ( ) var ( - BotToken string - Socks5 string - bot *tb.Bot + bot *tb.Bot ) const ( @@ -31,6 +32,48 @@ const ( ) func BotStart() { + var err error + fmt.Println(logo) + //read config + config.InitConfig() + //Init Logger + logger.InitLogger() + //InitDB + model.InitDB() + Poller := &tb.LongPoller{Timeout: 15 * time.Second} + spamPoller := tb.NewMiddlewarePoller(Poller, func(upd *tb.Update) bool { + if upd.Message == nil { + return true + } + if !upd.Message.Private() { + return false + } + return true + }) + botSetting := tb.Settings{ + Token: config.BotToken, + Poller: spamPoller, + } + //set socks5 + if config.Socks5 != "" { + fmt.Println("Proxy:" + config.Socks5) + dialer, err := proxy.SOCKS5("tcp", config.Socks5, nil, proxy.Direct) + if err != nil { + zap.S().Errorw("failed to make dialer", "error", err, "socks5", config.Socks5) + } + httpTransport := &http.Transport{} + httpClient := &http.Client{Transport: httpTransport} + httpTransport.Dial = dialer.Dial + botSetting.Client = httpClient + } + //create bot + bot, err = tb.NewBot(botSetting) + if err != nil { + zap.S().Errorw("failed to create bot", "error", err) + return + } + fmt.Println("Bot: " + strconv.Itoa(bot.Me.ID) + " " + bot.Me.Username) + MakeHandle() TaskLaunch() fmt.Println("Bot Start") @@ -57,49 +100,3 @@ func TaskLaunch() { fmt.Println("Cron Task Start……") c.Start() } -func init() { - fmt.Println(logo) - - //read config - fmt.Println("Read Config……") - viper.SetConfigName("config") - viper.AddConfigPath(".") - err := viper.ReadInConfig() - if err != nil { - zap.S().Errorw("failed to read config", "error", err) - } - BotToken = viper.GetString("bot_token") - Socks5 = viper.GetString("socks5") - Poller := &tb.LongPoller{Timeout: 15 * time.Second} - spamPoller := tb.NewMiddlewarePoller(Poller, func(upd *tb.Update) bool { - if upd.Message == nil { - return true - } - if !upd.Message.Private() { - return false - } - return true - }) - botSetting := tb.Settings{ - Token: BotToken, - Poller: spamPoller, - } - //set socks5 - if Socks5 != "" { - fmt.Println("Proxy:" + Socks5) - dialer, err := proxy.SOCKS5("tcp", Socks5, nil, proxy.Direct) - if err != nil { - zap.S().Errorw("failed to make dialer", "error", err, "socks5", Socks5) - } - httpTransport := &http.Transport{} - httpClient := &http.Client{Transport: httpTransport} - httpTransport.Dial = dialer.Dial - botSetting.Client = httpClient - } - //create bot - bot, err = tb.NewBot(botSetting) - if err != nil { - zap.S().Errorw("failed to create bot", "error", err) - } - fmt.Println("Bot: " + strconv.Itoa(bot.Me.ID) + " " + bot.Me.Username) -} diff --git a/config/config.go b/config/config.go index 5e8ea2b..a934042 100644 --- a/config/config.go +++ b/config/config.go @@ -3,6 +3,7 @@ package config import ( "github.com/fsnotify/fsnotify" "github.com/spf13/viper" + "go.uber.org/zap" "strconv" "strings" ) @@ -22,7 +23,8 @@ const ( ) var ( - ErrorTimes map[string]int //错误次数 + BotToken string + Socks5 string BindMaxNum int ErrMaxTimes int Notice string @@ -34,8 +36,11 @@ func InitConfig() { viper.AddConfigPath(".") err := viper.ReadInConfig() if err != nil { - + zap.S().Errorw("failed to read config", "error", err) } + BotToken = viper.GetString("bot_token") + Socks5 = viper.GetString("socks5") + viper.SetDefault("errlimit", 5) viper.SetDefault("bindmax", 5)