This repository has been archived on 2022-10-02. You can view files and clone it, but cannot push or open issues or pull requests.
dcbot-old/main.go

159 lines
4.0 KiB
Go

package main
import (
"log"
"os"
"os/signal"
"path"
"strings"
"syscall"
"github.com/sirupsen/logrus"
"github.com/tribalwarshelp/dcbot/message"
"github.com/tribalwarshelp/golang-sdk/sdk"
_cron "github.com/tribalwarshelp/dcbot/cron"
"github.com/tribalwarshelp/dcbot/discord"
group_repository "github.com/tribalwarshelp/dcbot/group/repository"
observation_repository "github.com/tribalwarshelp/dcbot/observation/repository"
server_repository "github.com/tribalwarshelp/dcbot/server/repository"
"github.com/tribalwarshelp/shared/mode"
gopglogrusquerylogger "github.com/Kichiyaki/go-pg-logrus-query-logger/v10"
"github.com/go-pg/pg/v10"
"github.com/joho/godotenv"
"github.com/robfig/cron/v3"
)
const (
commandPrefix = "tw!"
)
var status = "tribalwarshelp.com | " + discord.HelpCommand.WithPrefix(commandPrefix).String()
func init() {
os.Setenv("TZ", "UTC")
if mode.Get() == mode.DevelopmentMode {
godotenv.Load(".env.local")
}
setupLogger()
}
func main() {
dir, err := os.Getwd()
if err != nil {
logrus.Fatal(err)
}
dirWithMessages := path.Join(dir, "message", "translations")
if err := message.LoadMessageFiles(dirWithMessages); err != nil {
logrus.Fatal(err)
}
logrus.WithField("dir", dirWithMessages).
WithField("languages", message.LanguageTags()).
Info("Loaded messages")
dbOptions := &pg.Options{
User: os.Getenv("DB_USER"),
Password: os.Getenv("DB_PASSWORD"),
Database: os.Getenv("DB_NAME"),
Addr: os.Getenv("DB_HOST") + ":" + os.Getenv("DB_PORT"),
}
dbFields := logrus.Fields{
"user": dbOptions.User,
"database": dbOptions.Database,
"addr": dbOptions.Addr,
}
db := pg.Connect(dbOptions)
defer func() {
if err := db.Close(); err != nil {
logrus.WithFields(dbFields).Fatalln(err)
}
}()
if strings.ToUpper(os.Getenv("LOG_DB_QUERIES")) == "TRUE" {
db.AddQueryHook(gopglogrusquerylogger.QueryLogger{
Entry: logrus.NewEntry(logrus.StandardLogger()),
})
}
serverRepo, err := server_repository.NewPgRepo(db)
if err != nil {
logrus.Fatal(err)
}
groupRepo, err := group_repository.NewPgRepo(db)
if err != nil {
logrus.Fatal(err)
}
observationRepo, err := observation_repository.NewPgRepo(db)
if err != nil {
logrus.Fatal(err)
}
logrus.WithFields(dbFields).Info("Connection with the database has been established")
api := sdk.New(os.Getenv("API_URL"))
sess, err := discord.New(discord.SessionConfig{
Token: os.Getenv("BOT_TOKEN"),
CommandPrefix: commandPrefix,
Status: status,
ObservationRepository: observationRepo,
ServerRepository: serverRepo,
GroupRepository: groupRepo,
API: api,
})
if err != nil {
logrus.Fatal(err)
}
defer sess.Close()
logrus.WithFields(logrus.Fields{
"api": os.Getenv("API_URL"),
"commandPrefix": commandPrefix,
"status": status,
}).Info("The Discord session has been initialized")
c := cron.New(cron.WithChain(
cron.SkipIfStillRunning(cron.VerbosePrintfLogger(log.New(os.Stdout, "cron: ", log.LstdFlags))),
))
_cron.Attach(c, _cron.Config{
ServerRepo: serverRepo,
ObservationRepo: observationRepo,
Discord: sess,
GroupRepo: groupRepo,
API: api,
Status: status,
})
c.Start()
defer c.Stop()
logrus.Info("Started the cron scheduler")
logrus.Info("Bot is running!")
channel := make(chan os.Signal, 1)
signal.Notify(channel, os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGINT)
<-channel
logrus.Info("shutting down")
}
func setupLogger() {
if mode.Get() == mode.DevelopmentMode {
logrus.SetLevel(logrus.DebugLevel)
}
timestampFormat := "2006-01-02 15:04:05"
if mode.Get() == mode.ProductionMode {
customFormatter := new(logrus.JSONFormatter)
customFormatter.TimestampFormat = timestampFormat
logrus.SetFormatter(customFormatter)
} else {
customFormatter := new(logrus.TextFormatter)
customFormatter.TimestampFormat = timestampFormat
customFormatter.FullTimestamp = true
logrus.SetFormatter(customFormatter)
}
}