-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
69 lines (59 loc) · 1.48 KB
/
main.go
File metadata and controls
69 lines (59 loc) · 1.48 KB
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
package main
import (
"context"
"log"
"log/slog"
"os"
"livid-bot/bot"
"livid-bot/db"
"livid-bot/internal/logging"
)
func main() {
_, logCloser, err := logging.Configure()
if err != nil {
log.Printf("failed to configure logging: %v", err)
os.Exit(1)
}
defer func() {
if err := logCloser.Close(); err != nil {
slog.Warn("failed to close logger", "error", err)
}
}()
token := requireEnv("DISCORD_BOT_TOKEN")
appID := requireEnv("DISCORD_APPLICATION_ID")
guildID := requireEnv("DISCORD_GUILD_ID")
databaseURL := requireEnv("DATABASE_URL")
ctx := context.Background()
pool, err := db.NewPool(ctx, databaseURL)
if err != nil {
slog.Error("failed to connect to database", "error", err)
os.Exit(1)
}
defer pool.Close()
if err := db.Migrate(ctx, pool); err != nil {
slog.Error("failed to run migrations", "error", err)
os.Exit(1)
}
cfg := bot.Config{
BotToken: token,
ApplicationID: appID,
GuildID: guildID,
StudyRepo: db.NewStudyRepository(pool),
MemberRepo: db.NewMemberRepository(pool),
RecruitRepo: db.NewRecruitRepository(pool),
AuditRepo: db.NewCommandAuditRepository(pool),
SuggestionRepo: db.NewSuggestionRepository(pool),
}
if err := bot.Run(cfg); err != nil {
slog.Error("bot exited with error", "error", err)
os.Exit(1)
}
}
func requireEnv(key string) string {
value := os.Getenv(key)
if value == "" {
slog.Error("required environment variable is not set", "key", key)
os.Exit(1)
}
return value
}