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

153 lines
3.7 KiB
Go
Raw Normal View History

2020-05-29 17:22:52 +00:00
package main
import (
"github.com/Kichiyaki/appmode"
"github.com/Kichiyaki/goutil/envutil"
"os"
"os/signal"
2020-07-19 10:16:02 +00:00
"path"
2020-10-17 10:43:27 +00:00
"strings"
"syscall"
2020-06-02 07:00:40 +00:00
2020-10-24 06:17:52 +00:00
"github.com/sirupsen/logrus"
2020-07-19 10:16:02 +00:00
"github.com/tribalwarshelp/dcbot/message"
2020-06-05 15:49:51 +00:00
"github.com/tribalwarshelp/golang-sdk/sdk"
2020-06-02 07:00:40 +00:00
_cron "github.com/tribalwarshelp/dcbot/cron"
"github.com/tribalwarshelp/dcbot/discord"
grouprepository "github.com/tribalwarshelp/dcbot/group/repository"
observationrepository "github.com/tribalwarshelp/dcbot/observation/repository"
serverepository "github.com/tribalwarshelp/dcbot/server/repository"
2020-06-02 07:00:40 +00:00
"github.com/Kichiyaki/go-pg-logrus-query-logger/v10"
"github.com/go-pg/pg/v10"
"github.com/joho/godotenv"
2020-05-30 10:43:11 +00:00
"github.com/robfig/cron/v3"
2020-05-29 17:22:52 +00:00
)
const (
commandPrefix = "tw!"
)
var status = "tribalwarshelp.com | " + discord.HelpCommand.WithPrefix(commandPrefix).String()
2020-10-17 10:43:27 +00:00
func init() {
os.Setenv("TZ", "UTC")
if appmode.Equals(appmode.DevelopmentMode) {
godotenv.Load(".env.local")
}
2020-10-24 06:17:52 +00:00
setupLogger()
}
2020-05-29 17:22:52 +00:00
func main() {
2020-07-19 10:16:02 +00:00
dir, err := os.Getwd()
if err != nil {
2020-10-24 06:17:52 +00:00
logrus.Fatal(err)
2020-07-19 10:16:02 +00:00
}
2020-10-24 06:17:52 +00:00
dirWithMessages := path.Join(dir, "message", "translations")
if err := message.LoadMessages(dirWithMessages); err != nil {
logrus.WithField("dir", dirWithMessages).Fatal(err)
2020-07-19 10:16:02 +00:00
}
db := pg.Connect(&pg.Options{
User: envutil.GetenvString("DB_USER"),
Password: envutil.GetenvString("DB_PASSWORD"),
Database: envutil.GetenvString("DB_NAME"),
Addr: envutil.GetenvString("DB_HOST") + ":" + os.Getenv("DB_PORT"),
})
defer func() {
if err := db.Close(); err != nil {
logrus.Fatalln(err)
2020-05-29 17:22:52 +00:00
}
}()
2020-10-17 10:43:27 +00:00
if strings.ToUpper(os.Getenv("LOG_DB_QUERIES")) == "TRUE" {
db.AddQueryHook(gopglogrusquerylogger.QueryLogger{
Log: logrus.NewEntry(logrus.StandardLogger()),
MaxQueryLength: 5000,
2020-10-17 10:43:27 +00:00
})
}
2020-06-27 16:48:33 +00:00
serverRepo, err := serverepository.NewPgRepository(db)
if err != nil {
2020-10-24 06:17:52 +00:00
logrus.Fatal(err)
}
groupRepo, err := grouprepository.NewPgRepo(db)
2020-06-26 18:30:25 +00:00
if err != nil {
2020-10-24 06:17:52 +00:00
logrus.Fatal(err)
2020-06-26 18:30:25 +00:00
}
observationRepo, err := observationrepository.NewPgRepository(db)
if err != nil {
2020-10-24 06:17:52 +00:00
logrus.Fatal(err)
2020-05-29 17:22:52 +00:00
}
2020-06-27 16:48:33 +00:00
api := sdk.New(envutil.GetenvString("API_URL"))
2020-06-27 16:48:33 +00:00
sess, err := discord.New(discord.SessionConfig{
Token: envutil.GetenvString("BOT_TOKEN"),
CommandPrefix: commandPrefix,
Status: status,
ObservationRepository: observationRepo,
ServerRepository: serverRepo,
2020-06-26 18:30:25 +00:00
GroupRepository: groupRepo,
API: api,
})
if err != nil {
logrus.
WithFields(logrus.Fields{
"api": envutil.GetenvString("API_URL"),
"commandPrefix": commandPrefix,
"status": status,
}).
Fatal(err)
}
defer sess.Close()
c := cron.New(
cron.WithChain(
cron.SkipIfStillRunning(
cron.PrintfLogger(logrus.StandardLogger()),
),
),
)
2020-06-06 12:59:11 +00:00
_cron.Attach(c, _cron.Config{
ServerRepo: serverRepo,
ObservationRepo: observationRepo,
Discord: sess,
2020-06-26 18:30:25 +00:00
GroupRepo: groupRepo,
API: api,
Status: status,
2020-05-30 10:43:11 +00:00
})
2020-06-27 16:48:33 +00:00
c.Start()
2020-05-30 10:43:11 +00:00
defer c.Stop()
logrus.Info("The bot is up and running!")
channel := make(chan os.Signal, 1)
signal.Notify(channel, os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGINT)
<-channel
logrus.Info("shutting down...")
2020-05-29 17:22:52 +00:00
}
func setupLogger() {
if appmode.Equals(appmode.DevelopmentMode) {
logrus.SetLevel(logrus.DebugLevel)
}
timestampFormat := "2006-01-02 15:04:05"
if appmode.Equals(appmode.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)
}
}