tgbot_misaka_5882f7/main.go

90 lines
1.9 KiB
Go
Raw Normal View History

2022-11-21 04:39:21 +09:00
package main
import (
"os"
"os/signal"
"syscall"
"go.uber.org/zap"
2023-03-08 05:46:00 +09:00
"go.uber.org/zap/zapcore"
2022-11-23 05:33:23 +09:00
tele "gopkg.in/telebot.v3"
2022-11-22 21:33:50 +09:00
"git.gensokyo.cafe/kkyy/tgbot_misaka_5882f7/utils"
2022-11-21 04:39:21 +09:00
)
2023-03-20 16:22:46 +09:00
var (
logger *zap.SugaredLogger
loglvl zap.AtomicLevel
)
2022-11-21 04:39:21 +09:00
func initLogger() {
2023-03-08 05:46:00 +09:00
logCfg := zap.NewProductionConfig()
loglvl = logCfg.Level
l, err := logCfg.Build()
2022-11-21 04:39:21 +09:00
if err != nil {
panic(err)
}
logger = l.Sugar()
}
func runBot() {
logger.Info("Bot initializing...")
bot, err := initBot()
if err != nil {
logger.Fatalw("Failed to initialize bot", "err", err)
}
2022-11-23 05:33:23 +09:00
// Announce commands
if config.TGAnnounceCommands {
logger.Info("Announcing commands...")
if err = bot.SetCommands([]tele.Command{
2023-03-08 03:28:26 +09:00
{Text: "tr", Description: "Translate text"},
{Text: "xr", Description: "Currency exchange rates"},
{Text: "year_progress", Description: "Time doesn't wait."},
2022-11-23 05:33:23 +09:00
{Text: "traffic", Description: "Show traffic usage."},
{Text: "dig", Description: "Diggy diggy dig."},
}); err != nil {
logger.Fatalw("Failed to announce commands", "err", err)
}
}
2022-11-22 21:33:50 +09:00
botFinCh := utils.WaitFor(bot.Start)
logger.Infow("Bot started", "username", bot.Me.Username)
2022-11-21 04:39:21 +09:00
go initExchangeRates()
2022-11-21 04:39:21 +09:00
// listen for shutdown signal
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
2022-11-22 21:33:50 +09:00
select {
case sig := <-sigCh:
logger.Infow("Received signal", "signal", sig)
logger.Info("Bot shutdown...")
bot.Stop()
case <-botFinCh:
logger.Warn("Bot terminated")
}
2022-11-21 04:39:21 +09:00
}
func main() {
initLogger()
if err := LoadCfg(); err != nil {
logger.Fatalw("Failed to load config", "err", err)
}
2023-03-08 05:46:00 +09:00
parsedLvl, err := zapcore.ParseLevel(config.LogLevel)
if err != nil {
logger.Warnf("Invalid log level %q", config.LogLevel)
} else {
loglvl.SetLevel(parsedLvl)
}
2023-03-20 19:12:41 +09:00
if err := initMsgCache(); err != nil {
logger.Fatalw("Failed to initialize message cache", "err", err)
}
2022-11-21 04:39:21 +09:00
runBot()
}