Archived
1
This repository has been archived on 2022-11-04. You can view files and clone it, but cannot push or open issues or pull requests.
i2_bot/lib/connections/connections.go

38 lines
1.1 KiB
Go
Raw Normal View History

2017-10-04 17:56:18 +04:00
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package connections
import (
"bitbucket.org/pztrn/mogrus"
"git.wtfteam.pro/fat0troll/i2_bot/lib/config"
2017-10-18 07:03:34 +04:00
_ "github.com/go-sql-driver/mysql"
2017-10-26 18:17:58 +04:00
"github.com/go-telegram-bot-api/telegram-bot-api"
2017-11-14 03:44:21 +04:00
"github.com/jmoiron/sqlx"
2017-10-04 17:56:18 +04:00
)
// BotInit initializes connection to Telegram
2017-11-14 03:44:21 +04:00
func BotInit(cfg *config.Config, lg *mogrus.LoggerHandler) *tgbotapi.BotAPI {
2017-10-18 07:03:34 +04:00
bot, err := tgbotapi.NewBotAPI(cfg.Telegram.APIToken)
if err != nil {
2017-11-14 03:44:21 +04:00
lg.Fatal(err.Error())
2017-10-18 07:03:34 +04:00
}
2017-10-04 17:56:18 +04:00
2017-10-18 07:03:34 +04:00
bot.Debug = true
2017-10-04 17:56:18 +04:00
2017-11-14 03:44:21 +04:00
lg.Info("Bot version: " + config.VERSION)
lg.Info("Authorized on account @", bot.Self.UserName)
2017-10-04 17:56:18 +04:00
2017-10-18 07:03:34 +04:00
return bot
2017-10-04 17:56:18 +04:00
}
// DBInit initializes database connection
2017-11-14 03:44:21 +04:00
func DBInit(cfg *config.Config, lg *mogrus.LoggerHandler) *sqlx.DB {
2017-10-18 07:03:34 +04:00
database, err := sqlx.Connect("mysql", cfg.Database.User+":"+cfg.Database.Password+"@tcp("+cfg.Database.Host+":"+cfg.Database.Port+")/"+cfg.Database.Database+"?parseTime=true&charset=utf8mb4,utf8")
if err != nil {
2017-11-14 03:44:21 +04:00
lg.Fatal(err)
2017-10-18 07:03:34 +04:00
}
2017-11-14 03:44:21 +04:00
lg.Info("Database connection established!")
2017-10-18 07:03:34 +04:00
return database
2017-10-04 17:56:18 +04:00
}