2021-06-14 23:17:11 +08:00
|
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"github.com/fsnotify/fsnotify"
|
|
|
|
|
|
"github.com/spf13/viper"
|
2021-06-15 10:00:51 +08:00
|
|
|
|
"go.uber.org/zap"
|
2021-06-14 23:17:11 +08:00
|
|
|
|
"strconv"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
LogBasePath string = "./log/"
|
|
|
|
|
|
WelcomeContent string = "欢迎使用E5SubBot!"
|
|
|
|
|
|
HelpContent string = `
|
|
|
|
|
|
命令:
|
|
|
|
|
|
/my 查看已绑定账户信息
|
|
|
|
|
|
/bind 绑定新账户
|
|
|
|
|
|
/unbind 解绑账户
|
|
|
|
|
|
/export 导出账户信息(JSON)
|
|
|
|
|
|
/help 帮助
|
|
|
|
|
|
源码及使用方法:https://github.com/iyear/E5SubBot
|
|
|
|
|
|
`
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
2021-06-15 18:27:28 +08:00
|
|
|
|
BotToken string
|
|
|
|
|
|
Socks5 string
|
|
|
|
|
|
BindMaxNum int
|
|
|
|
|
|
MaxGoroutines int
|
|
|
|
|
|
MaxErrTimes int
|
|
|
|
|
|
Cron string
|
|
|
|
|
|
Notice string
|
|
|
|
|
|
Admins []int64
|
2021-06-14 23:17:11 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func InitConfig() {
|
|
|
|
|
|
viper.SetConfigName("config")
|
|
|
|
|
|
viper.AddConfigPath(".")
|
|
|
|
|
|
err := viper.ReadInConfig()
|
|
|
|
|
|
if err != nil {
|
2021-06-15 10:00:51 +08:00
|
|
|
|
zap.S().Errorw("failed to read config", "error", err)
|
2021-06-14 23:17:11 +08:00
|
|
|
|
}
|
2021-06-15 10:00:51 +08:00
|
|
|
|
BotToken = viper.GetString("bot_token")
|
2021-06-19 13:03:38 +08:00
|
|
|
|
Cron = viper.GetString("cron")
|
2021-06-15 10:00:51 +08:00
|
|
|
|
Socks5 = viper.GetString("socks5")
|
2021-06-19 13:03:38 +08:00
|
|
|
|
//if Socks5[:5] != "socks5" {
|
|
|
|
|
|
// Socks5 = "socks5://" + Socks5
|
|
|
|
|
|
//}
|
2021-06-15 10:00:51 +08:00
|
|
|
|
|
2021-06-14 23:17:11 +08:00
|
|
|
|
viper.SetDefault("errlimit", 5)
|
|
|
|
|
|
viper.SetDefault("bindmax", 5)
|
|
|
|
|
|
|
|
|
|
|
|
BindMaxNum = viper.GetInt("bindmax")
|
2021-06-15 18:27:28 +08:00
|
|
|
|
MaxErrTimes = viper.GetInt("errlimit")
|
2021-06-14 23:17:11 +08:00
|
|
|
|
Notice = viper.GetString("notice")
|
2021-06-19 13:03:38 +08:00
|
|
|
|
|
2021-06-15 18:27:28 +08:00
|
|
|
|
MaxGoroutines = viper.GetInt("goroutine")
|
2021-06-14 23:17:11 +08:00
|
|
|
|
Admins = getAdmins()
|
|
|
|
|
|
|
|
|
|
|
|
viper.WatchConfig()
|
|
|
|
|
|
viper.OnConfigChange(func(e fsnotify.Event) {
|
2021-06-15 18:27:28 +08:00
|
|
|
|
MaxGoroutines = viper.GetInt("goroutine")
|
2021-06-14 23:17:11 +08:00
|
|
|
|
BindMaxNum = viper.GetInt("bindmax")
|
2021-06-15 18:27:28 +08:00
|
|
|
|
MaxErrTimes = viper.GetInt("errlimit")
|
2021-06-14 23:17:11 +08:00
|
|
|
|
Notice = viper.GetString("notice")
|
|
|
|
|
|
Admins = getAdmins()
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
func getAdmins() []int64 {
|
|
|
|
|
|
var result []int64
|
|
|
|
|
|
admins := strings.Split(viper.GetString("admin"), ",")
|
|
|
|
|
|
for _, v := range admins {
|
|
|
|
|
|
id, _ := strconv.ParseInt(v, 10, 64)
|
|
|
|
|
|
result = append(result, id)
|
|
|
|
|
|
}
|
|
|
|
|
|
return result
|
|
|
|
|
|
}
|