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 package bots
import ( import (
"database/sql"
"fmt" "fmt"
"github.com/iyear/E5SubBot/task"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.uber.org/zap" "go.uber.org/zap"
@ -10,7 +10,6 @@ import (
tb "gopkg.in/tucnak/telebot.v2" tb "gopkg.in/tucnak/telebot.v2"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"time" "time"
) )
@ -18,12 +17,10 @@ var (
BotToken string BotToken string
Socks5 string Socks5 string
bot *tb.Bot bot *tb.Bot
//logger *log.Logger
) )
const ( const (
dbDriverName = "mysql" logo = `
logo = `
______ _____ _____ _ ____ _ ______ _____ _____ _ ____ _
| ____| ____/ ____| | | | _ \ | | | ____| ____/ ____| | | | _ \ | |
| |__ | |__| (___ _ _| |__ | |_) | ___ | |_ | |__ | |__| (___ _ _| |__ | |_) | ___ | |_
@ -33,8 +30,6 @@ const (
` `
) )
var dbPath string
func BotStart() { func BotStart() {
MakeHandle() MakeHandle()
TaskLaunch() TaskLaunch()
@ -57,14 +52,10 @@ func MakeHandle() {
bot.Handle("/log", bLog) bot.Handle("/log", bLog)
} }
func TaskLaunch() { func TaskLaunch() {
task := cron.New() c := cron.New()
//每三小时执行一次 c.AddFunc(viper.GetString("cron"), task.SignTask)
task.AddFunc(viper.GetString("cron"), SignTask)
//log分为每天
//task.AddFunc(" 0 0 * * *", InitLogger)
// */1 * * * * 1 */3 * * *
fmt.Println("Cron Task Start……") fmt.Println("Cron Task Start……")
task.Start() c.Start()
} }
func init() { func init() {
fmt.Println(logo) fmt.Println(logo)
@ -75,28 +66,12 @@ func init() {
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) 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") BotToken = viper.GetString("bot_token")
Socks5 = viper.GetString("socks5") Socks5 = viper.GetString("socks5")
Poller := &tb.LongPoller{Timeout: 15 * time.Second} 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 { if upd.Message == nil {
return true return true
} }
@ -105,26 +80,26 @@ func init() {
} }
return true return true
}) })
botsettings := tb.Settings{ botSetting := tb.Settings{
Token: BotToken, Token: BotToken,
Poller: spamProtected, Poller: spamPoller,
} }
//set socks5 //set socks5
if Socks5 != "" { if Socks5 != "" {
fmt.Println("Proxy:" + Socks5) fmt.Println("Proxy:" + Socks5)
dialer, err := proxy.SOCKS5("tcp", Socks5, nil, proxy.Direct) dialer, err := proxy.SOCKS5("tcp", Socks5, nil, proxy.Direct)
if err != nil { 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{} httpTransport := &http.Transport{}
httpClient := &http.Client{Transport: httpTransport} httpClient := &http.Client{Transport: httpTransport}
httpTransport.Dial = dialer.Dial httpTransport.Dial = dialer.Dial
botsettings.Client = httpClient botSetting.Client = httpClient
} }
//create bot //create bot
bot, err = tb.NewBot(botsettings) bot, err = tb.NewBot(botSetting)
if err != nil { 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) fmt.Println("Bot: " + strconv.Itoa(bot.Me.ID) + " " + bot.Me.Username)
} }