2020-05-29 17:22:52 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2020-05-30 08:35:45 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2020-07-19 10:16:02 +00:00
|
|
|
"path"
|
2020-10-17 10:43:27 +00:00
|
|
|
"strings"
|
2020-05-30 08:35:45 +00:00
|
|
|
"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"
|
2020-06-18 12:51:31 +00:00
|
|
|
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"
|
2020-05-30 08:35:45 +00:00
|
|
|
|
2021-01-06 16:36:44 +00:00
|
|
|
"github.com/go-pg/pg/extra/pgdebug"
|
2020-05-30 08:35:45 +00:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2020-07-15 10:03:43 +00:00
|
|
|
const (
|
|
|
|
commandPrefix = "tw!"
|
|
|
|
)
|
|
|
|
|
2021-01-13 19:03:50 +00:00
|
|
|
var status = "tribalwarshelp.com | " + discord.HelpCommand.WithPrefix(commandPrefix).String()
|
2020-10-17 10:43:27 +00:00
|
|
|
|
2020-05-30 08:35:45 +00:00
|
|
|
func init() {
|
|
|
|
os.Setenv("TZ", "UTC")
|
|
|
|
|
|
|
|
if mode.Get() == mode.DevelopmentMode {
|
|
|
|
godotenv.Load(".env.development")
|
|
|
|
}
|
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-30 08:35:45 +00:00
|
|
|
}
|
|
|
|
|
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{
|
2020-05-30 08:35:45 +00:00
|
|
|
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)
|
2020-05-30 08:35:45 +00:00
|
|
|
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-05-30 08:35:45 +00:00
|
|
|
}()
|
2020-10-17 10:43:27 +00:00
|
|
|
if strings.ToUpper(os.Getenv("LOG_DB_QUERIES")) == "TRUE" {
|
2021-01-06 16:36:44 +00:00
|
|
|
db.AddQueryHook(pgdebug.DebugHook{
|
2020-10-17 10:43:27 +00:00
|
|
|
Verbose: true,
|
|
|
|
})
|
|
|
|
}
|
2020-06-27 16:48:33 +00:00
|
|
|
|
2020-05-30 08:35:45 +00:00
|
|
|
serverRepo, err := server_repository.NewPgRepo(db)
|
|
|
|
if err != nil {
|
2020-10-24 06:17:52 +00:00
|
|
|
logrus.Fatal(err)
|
2020-05-30 08:35:45 +00:00
|
|
|
}
|
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
|
|
|
}
|
2020-06-18 12:51:31 +00:00
|
|
|
observationRepo, err := observation_repository.NewPgRepo(db)
|
2020-05-30 08:35:45 +00:00
|
|
|
if err != nil {
|
2020-10-24 06:17:52 +00:00
|
|
|
logrus.Fatal(err)
|
2020-05-29 17:22:52 +00:00
|
|
|
}
|
2021-01-13 19:03:50 +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"))
|
|
|
|
|
2020-05-30 08:35:45 +00:00
|
|
|
sess, err := discord.New(discord.SessionConfig{
|
2020-06-18 12:51:31 +00:00
|
|
|
Token: os.Getenv("BOT_TOKEN"),
|
2020-07-15 10:03:43 +00:00
|
|
|
CommandPrefix: commandPrefix,
|
|
|
|
Status: status,
|
2020-06-18 12:51:31 +00:00
|
|
|
ObservationRepository: observationRepo,
|
|
|
|
ServerRepository: serverRepo,
|
2020-06-26 18:30:25 +00:00
|
|
|
GroupRepository: groupRepo,
|
2020-06-18 12:51:31 +00:00
|
|
|
API: api,
|
2020-05-30 08:35:45 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2020-10-24 06:17:52 +00:00
|
|
|
logrus.Fatal(err)
|
2020-05-30 08:35:45 +00:00
|
|
|
}
|
|
|
|
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 08:35:45 +00:00
|
|
|
|
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{
|
2020-06-18 12:51:31 +00:00
|
|
|
ServerRepo: serverRepo,
|
|
|
|
ObservationRepo: observationRepo,
|
|
|
|
Discord: sess,
|
2020-06-26 18:30:25 +00:00
|
|
|
GroupRepo: groupRepo,
|
2020-06-18 12:51:31 +00:00
|
|
|
API: api,
|
2020-07-15 10:03:43 +00:00
|
|
|
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()
|
2021-01-08 19:15:11 +00:00
|
|
|
logrus.Info("Started the cron scheduler")
|
2020-05-30 08:35:45 +00:00
|
|
|
|
2021-01-13 19:03:50 +00:00
|
|
|
logrus.Info("Bot is running!")
|
2020-05-30 08:35:45 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|