-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
62 lines (52 loc) · 1.42 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
package main
import (
"context"
"os"
"os/signal"
"syscall"
"time"
"go.uber.org/zap"
"openai-discord-bot/bot"
"openai-discord-bot/config"
)
func main() {
serviceCtx, cancel := context.WithCancel(context.Background())
config.Configure(serviceCtx)
log := config.GetLogger()
log.Info("Creating discord client")
discordSession, err := config.GetDiscordSession()
if err != nil {
log.Fatal("Failed to instantiate Discord client", zap.Error(err))
}
err = discordSession.Open()
if err != nil {
log.Fatal("Failed to connect to Discord", zap.Error(err))
}
defer func() {
err = discordSession.Close()
if err != nil {
log.Error("Error closing Discord connection", zap.Error(err))
}
}()
log.Info("connecting to OpenAI")
openapiClient, err := config.GetOpenAISession()
if err != nil {
log.Fatal("Failed to instantiate OpenAPI client", zap.Error(err))
}
botInstance := bot.NewAIBot(serviceCtx, openapiClient, discordSession, config.GetStorage(), config.GetImageStorage(), log)
log.Info("Starting bot")
err = botInstance.Go()
if err != nil {
log.Fatal("Failed to run the bot!", zap.Error(err))
}
stop := make(chan os.Signal)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
<-stop
log.Info("Gracefully shutting down")
botInstance.Shutdown()
time.Sleep(time.Second * 1)
cancel()
// Give anything flushing from the system context, a few seconds to finish up
time.Sleep(time.Second * 5)
_ = log.Sync()
}