independent config
This commit is contained in:
parent
a1e68cdc5c
commit
15e9d93abd
95
bots/bots.go
95
bots/bots.go
@ -2,6 +2,9 @@ package bots
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/iyear/E5SubBot/config"
|
||||||
|
"github.com/iyear/E5SubBot/logger"
|
||||||
|
"github.com/iyear/E5SubBot/model"
|
||||||
"github.com/iyear/E5SubBot/task"
|
"github.com/iyear/E5SubBot/task"
|
||||||
"github.com/robfig/cron/v3"
|
"github.com/robfig/cron/v3"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
@ -14,9 +17,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
BotToken string
|
bot *tb.Bot
|
||||||
Socks5 string
|
|
||||||
bot *tb.Bot
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -31,6 +32,48 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func BotStart() {
|
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()
|
MakeHandle()
|
||||||
TaskLaunch()
|
TaskLaunch()
|
||||||
fmt.Println("Bot Start")
|
fmt.Println("Bot Start")
|
||||||
@ -57,49 +100,3 @@ func TaskLaunch() {
|
|||||||
fmt.Println("Cron Task Start……")
|
fmt.Println("Cron Task Start……")
|
||||||
c.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)
|
|
||||||
}
|
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package config
|
|||||||
import (
|
import (
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
"go.uber.org/zap"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -22,7 +23,8 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrorTimes map[string]int //错误次数
|
BotToken string
|
||||||
|
Socks5 string
|
||||||
BindMaxNum int
|
BindMaxNum int
|
||||||
ErrMaxTimes int
|
ErrMaxTimes int
|
||||||
Notice string
|
Notice string
|
||||||
@ -34,8 +36,11 @@ func InitConfig() {
|
|||||||
viper.AddConfigPath(".")
|
viper.AddConfigPath(".")
|
||||||
err := viper.ReadInConfig()
|
err := viper.ReadInConfig()
|
||||||
if err != nil {
|
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("errlimit", 5)
|
||||||
viper.SetDefault("bindmax", 5)
|
viper.SetDefault("bindmax", 5)
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user