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

144 lines
3.5 KiB
Go
Raw Normal View History

2020-05-29 17:22:52 +00:00
package main
import (
"log"
"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"
2020-06-26 18:30:25 +00:00
group_repository "github.com/tribalwarshelp/dcbot/group/repository"
observation_repository "github.com/tribalwarshelp/dcbot/observation/repository"
2020-06-02 07:00:40 +00:00
server_repository "github.com/tribalwarshelp/dcbot/server/repository"
"github.com/tribalwarshelp/shared/mode"
"github.com/go-pg/pg/extra/pgdebug"
"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 mode.Get() == mode.DevelopmentMode {
godotenv.Load(".env.development")
logrus.SetLevel(logrus.DebugLevel)
}
2020-10-24 06:17:52 +00:00
customFormatter := new(logrus.TextFormatter)
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
customFormatter.FullTimestamp = true
logrus.SetFormatter(customFormatter)
}
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.LoadMessageFiles(dirWithMessages); err != nil {
logrus.Fatal(err)
2020-07-19 10:16:02 +00:00
}
2020-10-24 06:17:52 +00:00
logrus.WithField("dir", dirWithMessages).
WithField("languages", message.LanguageTags()).
Info("Loaded messages")
2020-07-19 10:16:02 +00:00
2020-10-24 06:17:52 +00:00
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"),
2020-10-24 06:17:52 +00:00
}
dbFields := logrus.Fields{
"user": dbOptions.User,
"database": dbOptions.Database,
"addr": dbOptions.Addr,
}
db := pg.Connect(dbOptions)
defer func() {
if err := db.Close(); err != nil {
2020-10-24 06:17:52 +00:00
logrus.WithFields(dbFields).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(pgdebug.DebugHook{
2020-10-17 10:43:27 +00:00
Verbose: true,
})
}
2020-06-27 16:48:33 +00:00
serverRepo, err := server_repository.NewPgRepo(db)
if err != nil {
2020-10-24 06:17:52 +00:00
logrus.Fatal(err)
}
2020-06-26 18:30:25 +00:00
groupRepo, err := group_repository.NewPgRepo(db)
if err != nil {
2020-10-24 06:17:52 +00:00
logrus.Fatal(err)
2020-06-26 18:30:25 +00:00
}
observationRepo, err := observation_repository.NewPgRepo(db)
if err != nil {
2020-10-24 06:17:52 +00:00
logrus.Fatal(err)
2020-05-29 17:22:52 +00:00
}
logrus.WithFields(dbFields).Info("Connected to the database")
2020-06-27 16:48:33 +00:00
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,
2020-06-26 18:30:25 +00:00
GroupRepository: groupRepo,
API: api,
})
if err != nil {
2020-10-24 06:17:52 +00:00
logrus.Fatal(err)
}
defer sess.Close()
2020-10-24 06:17:52 +00:00
logrus.WithFields(logrus.Fields{
"api": os.Getenv("API_URL"),
"commandPrefix": commandPrefix,
"status": status,
}).Info("Initialized new Discord session")
2020-05-30 10:43:11 +00:00
c := cron.New(cron.WithChain(
cron.SkipIfStillRunning(cron.VerbosePrintfLogger(log.New(os.Stdout, "cron: ", log.LstdFlags))),
))
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("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
2020-10-24 06:17:52 +00:00
logrus.Info("shutting down")
2020-05-29 17:22:52 +00:00
}