-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
155 lines (130 loc) · 3.56 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
package main
import (
"context"
_ "embed"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/disgoorg/disgo"
"github.com/disgoorg/disgo/bot"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/topi314/reddit-discord-bot/v2/redditbot"
)
var (
Version = "unknown"
Commit = "unknown"
)
var (
//go:embed sql/schema.sql
schema string
//go:embed reddit.png
redditIcon []byte
)
func main() {
slog.Info("starting reddit-discord-bot...", slog.String("version", Version), slog.String("commit", Commit))
cfg, err := redditbot.ReadConfig()
if err != nil {
slog.Error("error reading config", slog.Any("err", err))
return
}
if err = setupLogger(cfg.Log); err != nil {
slog.Error("error setting up logger", slog.Any("err", err))
return
}
slog.Info("loaded config", slog.String("config", cfg.String()))
if err = cfg.Validate(); err != nil {
slog.Error("error validating config", slog.Any("err", err))
return
}
client, err := disgo.New(cfg.Discord.Token,
bot.WithDefaultGateway(),
)
if err != nil {
slog.Error("error creating client", slog.Any("err", err))
return
}
reddit, err := redditbot.NewReddit(cfg.Reddit, Version)
if err != nil {
slog.Error("error creating reddit client", slog.Any("err", err))
return
}
db, err := redditbot.NewDB(cfg.Database, schema)
if err != nil {
slog.Error("error creating database client", slog.Any("err", err))
}
b := redditbot.New(cfg, redditIcon, client, reddit, db)
defer b.Close()
if cfg.Metrics.Enabled {
mux := http.NewServeMux()
mux.Handle(cfg.Metrics.Endpoint, promhttp.Handler())
b.MetricsServer = &http.Server{
Addr: cfg.Metrics.ListenAddr,
Handler: mux,
}
}
if cfg.Server.Enabled {
mux := http.NewServeMux()
mux.HandleFunc(cfg.Server.Endpoint, b.OnDiscordCallback)
b.Server = &http.Server{
Addr: cfg.Server.ListenAddr,
Handler: mux,
}
}
b.Client.AddEventListeners(bot.NewListenerFunc(b.OnApplicationCommand))
if cfg.Discord.SyncCommands {
if _, err = client.Rest().SetGlobalCommands(client.ApplicationID(), redditbot.Commands); err != nil {
slog.Error("error setting global commands", slog.Any("err", err))
}
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err = client.OpenGateway(ctx); err != nil {
slog.Error("error opening discord gateway", slog.Any("err", err))
}
go b.ListenSubreddits()
if cfg.Server.Enabled {
go b.ListenAndServe()
defer func() {
sCtx, sCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer sCancel()
if sErr := b.Server.Shutdown(sCtx); sErr != nil {
slog.Error("error shutting down server", slog.Any("err", sErr))
}
}()
}
if cfg.Metrics.Enabled {
go b.ListenAndServeMetrics()
defer func() {
mCtx, mCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer mCancel()
if mErr := b.MetricsServer.Shutdown(mCtx); mErr != nil {
slog.Error("error shutting down metrics server", slog.Any("err", mErr))
}
}()
}
defer slog.Info("stopping reddit-discord-bot...")
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM)
<-s
}
func setupLogger(cfg redditbot.LogConfig) error {
options := &slog.HandlerOptions{
AddSource: cfg.AddSource,
Level: cfg.Level,
}
var sHandler slog.Handler
switch cfg.Format {
case "json":
sHandler = slog.NewJSONHandler(os.Stdout, options)
case "text":
sHandler = slog.NewTextHandler(os.Stdout, options)
default:
return fmt.Errorf("unknown log format: %s", cfg.Format)
}
slog.SetDefault(slog.New(sHandler))
return nil
}