This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
188 lines (162 loc) · 4 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"context"
"os"
"os/signal"
"time"
"github.com/DarkOnion0/ADZTBotV2/commands"
"github.com/DarkOnion0/ADZTBotV2/config"
"github.com/DarkOnion0/ADZTBotV2/functions"
"github.com/rs/zerolog"
"github.com/bwmarrin/discordgo"
"github.com/rs/zerolog/log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
var s *discordgo.Session
func init() {
// enable or not the debug level (default is Info)
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if *config.Debug == "true" {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Logger = log.With().Caller().Logger()
// activate the pretty logger for dev purpose only if the debug mode is enabled
if *config.Debug == "true" {
log.Logger = log.With().Caller().Logger().Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
log.Info().
Str("type", "main").
Str("function", "init").
Msg("Logger is configured!")
log.Debug().
Str("type", "main").
Str("function", "init").
Msg("Debug mode is enabled!")
}
func init() {
var err error
s, err = discordgo.New("Bot " + *config.BotToken)
if err != nil {
log.Fatal().
Err(err).
Str("type", "main").
Str("function", "init").
Msg("An error occurred, invalid bot parameters")
}
}
func init() {
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := commands.CommandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
})
}
func main() {
/*
MongoBD initialisation
*/
var errMongo error
config.Client, errMongo = mongo.NewClient(options.Client().ApplyURI(*config.DBUrl))
if errMongo != nil {
log.Fatal().
Err(errMongo).
Str("type", "main").
Str("function", "main").
Msg("Something bad happen while creating a the MongoDB client")
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
errMongo = config.Client.Connect(ctx)
if errMongo != nil {
log.Fatal().
Err(errMongo).
Str("type", "main").
Str("function", "main").
Msg("Something bad happen while connecting to MongoDB")
}
errMongo = config.Client.Ping(ctx, readpref.Primary())
if errMongo != nil {
log.Fatal().
Err(errMongo).
Str("type", "main").
Str("function", "main").
Msg("Something bad happen while pinging the database, is it online ?!")
}
log.Info().
Str("type", "main").
Str("function", "main").
Msg("DB is connected !")
defer func(c *mongo.Client) {
err := c.Disconnect(ctx)
if err != nil {
log.Fatal().
Err(err).
Str("type", "main").
Str("function", "main").
Msg("An error occurred while closing the bot")
}
}(config.Client)
/*
Timer job(s)
*/
go func() {
functions.UpdateUserRankingCron()
for {
time.Sleep(time.Duration(*config.Timer))
functions.UpdateUserRankingCron()
}
}()
/*
Discordgo initialization
*/
s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Info().
Str("type", "main").
Str("function", "main").
Msg("Bot is up!")
})
err := s.Open()
if err != nil {
log.Fatal().
Err(err).
Str("type", "main").
Str("function", "main").
Msg("Cannot open the discord session")
}
for _, v := range commands.Commands {
_, err := s.ApplicationCommandCreate(s.State.User.ID, *config.GuildID, v)
if err != nil {
log.Panic().
Err(err).
Str("type", "main").
Str("function", "main").
Str("command", v.Name).
Msg("Cannot create command")
}
log.Debug().
Str("type", "main").
Str("function", "main").
Str("command", v.Name).
Msg("Command has been successfully created")
}
defer func(s *discordgo.Session) {
err := s.Close()
if err != nil {
log.Fatal().
Err(err).
Str("type", "main").
Str("function", "main").
Msg("An error occurred while closing the bot")
}
}(s)
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
<-stop
log.Info().
Str("type", "main").
Str("function", "main").
Msg("Gracefully shutting down the bot...")
}