remove mysql connection

This commit is contained in:
iyear 2021-06-14 23:02:42 +08:00
parent b8490fe150
commit e50ce27d24

View File

@ -1,8 +1,8 @@
package bots
import (
"database/sql"
"fmt"
"github.com/iyear/E5SubBot/task"
"github.com/robfig/cron/v3"
"github.com/spf13/viper"
"go.uber.org/zap"
@ -10,7 +10,6 @@ import (
tb "gopkg.in/tucnak/telebot.v2"
"net/http"
"strconv"
"strings"
"time"
)
@ -18,11 +17,9 @@ var (
BotToken string
Socks5 string
bot *tb.Bot
//logger *log.Logger
)
const (
dbDriverName = "mysql"
logo = `
______ _____ _____ _ ____ _
| ____| ____/ ____| | | | _ \ | |
@ -33,8 +30,6 @@ const (
`
)
var dbPath string
func BotStart() {
MakeHandle()
TaskLaunch()
@ -57,14 +52,10 @@ func MakeHandle() {
bot.Handle("/log", bLog)
}
func TaskLaunch() {
task := cron.New()
//每三小时执行一次
task.AddFunc(viper.GetString("cron"), SignTask)
//log分为每天
//task.AddFunc(" 0 0 * * *", InitLogger)
// */1 * * * * 1 */3 * * *
c := cron.New()
c.AddFunc(viper.GetString("cron"), task.SignTask)
fmt.Println("Cron Task Start……")
task.Start()
c.Start()
}
func init() {
fmt.Println(logo)
@ -75,28 +66,12 @@ func init() {
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
zap.S().Errorw("failed to read config","error",err)
zap.S().Errorw("failed to read config", "error", err)
}
host := viper.GetString("mysql.host")
user := viper.GetString("mysql.user")
port := viper.GetString("mysql.port")
pwd := viper.GetString("mysql.password")
database := viper.GetString("mysql.database")
dbPath = strings.Join([]string{user, ":", pwd, "@tcp(", host, ":", port, ")/", database, "?charset=utf8"}, "")
//fmt.Println(path)
db, err := sql.Open(dbDriverName, dbPath)
if err != nil {
zap.S().Errorw("failed to connect db","error",err,"path",dbPath)
}
fmt.Println("Connect MySQL Success!")
if ok, err := CreateTB(); !ok {
zap.S().Errorw("failed to create table","error",err,"path",dbPath)
}
defer db.Close()
BotToken = viper.GetString("bot_token")
Socks5 = viper.GetString("socks5")
Poller := &tb.LongPoller{Timeout: 15 * time.Second}
spamProtected := tb.NewMiddlewarePoller(Poller, func(upd *tb.Update) bool {
spamPoller := tb.NewMiddlewarePoller(Poller, func(upd *tb.Update) bool {
if upd.Message == nil {
return true
}
@ -105,26 +80,26 @@ func init() {
}
return true
})
botsettings := tb.Settings{
botSetting := tb.Settings{
Token: BotToken,
Poller: spamProtected,
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)
zap.S().Errorw("failed to make dialer", "error", err, "socks5", Socks5)
}
httpTransport := &http.Transport{}
httpClient := &http.Client{Transport: httpTransport}
httpTransport.Dial = dialer.Dial
botsettings.Client = httpClient
botSetting.Client = httpClient
}
//create bot
bot, err = tb.NewBot(botsettings)
bot, err = tb.NewBot(botSetting)
if err != nil {
zap.S().Errorw("failed to create bot","error",err)
zap.S().Errorw("failed to create bot", "error", err)
}
fmt.Println("Bot: " + strconv.Itoa(bot.Me.ID) + " " + bot.Me.Username)
}