This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
142 lines (121 loc) · 3.44 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"github.com/Kichiyaki/appmode"
"github.com/Kichiyaki/goutil/envutil"
"os"
"os/signal"
"path"
"syscall"
"github.com/sirupsen/logrus"
"github.com/tribalwarshelp/dcbot/message"
"github.com/tribalwarshelp/golang-sdk/sdk"
"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"
"github.com/Kichiyaki/go-pg-logrus-query-logger/v10"
"github.com/go-pg/pg/v10"
"github.com/joho/godotenv"
)
const (
commandPrefix = "tw!"
status = "tribalwarshelp.com | tw!help"
)
func init() {
os.Setenv("TZ", "UTC")
if appmode.Equals(appmode.DevelopmentMode) {
godotenv.Load(".env.local")
}
prepareLogger()
}
func main() {
dir, err := os.Getwd()
if err != nil {
logrus.Fatal(err)
}
dirWithMessages := path.Join(dir, "message", "translations")
if err := message.LoadMessages(dirWithMessages); err != nil {
logrus.WithField("dir", dirWithMessages).Fatal(err)
}
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)
}
}()
if envutil.GetenvBool("LOG_DB_QUERIES") {
db.AddQueryHook(querylogger.Logger{
Log: logrus.NewEntry(logrus.StandardLogger()),
MaxQueryLength: 5000,
})
}
serverRepo, err := serverepository.NewPgRepository(db)
if err != nil {
logrus.Fatal(err)
}
groupRepo, err := grouprepository.NewPgRepo(db)
if err != nil {
logrus.Fatal(err)
}
observationRepo, err := observationrepository.NewPgRepository(db)
if err != nil {
logrus.Fatal(err)
}
api := sdk.New(envutil.GetenvString("API_URL"))
sess, err := discord.New(discord.SessionConfig{
Token: envutil.GetenvString("BOT_TOKEN"),
CommandPrefix: commandPrefix,
Status: status,
ObservationRepository: observationRepo,
ServerRepository: serverRepo,
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.Config{
ServerRepo: serverRepo,
ObservationRepo: observationRepo,
Discord: sess,
GroupRepo: groupRepo,
API: api,
Status: status,
})
c.Start()
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...")
}
func prepareLogger() {
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)
}
}