This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
205 lines (173 loc) · 4.76 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"context"
"database/sql"
"errors"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"strconv"
"time"
"github.com/bwmarrin/discordgo"
_ "github.com/lib/pq"
"github.com/lmittmann/tint"
"github.com/mattn/go-isatty"
"golang.org/x/exp/slog"
"golang.org/x/time/rate"
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
if isatty.IsTerminal(os.Stdout.Fd()) {
slog.SetDefault(slog.New(tint.NewHandler(os.Stdout, &tint.Options{
TimeFormat: time.Kitchen,
})))
} else {
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil)))
}
if err := run(ctx); err != nil {
slog.Error("Exiting", "err", err)
}
}
func run(ctx context.Context) error {
var (
discordToken string
postgresDSN string
crawlerDelayIntervalSecs int
announceDelayIntervalSecs int
)
flag.StringVar(&discordToken, "discord-token", "", "Discord Bot token")
flag.StringVar(&postgresDSN, "postgres-dsn", "", "PostgreSQL DSN")
flag.IntVar(&crawlerDelayIntervalSecs, "crawler-interval-secs", 3600, "How long to wait (in seconds) before checking RSS feeds")
flag.IntVar(&announceDelayIntervalSecs, "announce-interval-secs", 300, "How long to wait (in seconds) before checking for new items to announce")
flag.Parse()
discordToken = func(defaultValue string) string {
if value, ok := os.LookupEnv("GOOSE_DISCORD_TOKEN"); ok {
return value
}
return defaultValue
}(discordToken)
postgresDSN = func(defaultValue string) string {
if value, ok := os.LookupEnv("GOOSE_POSTGRES_DSN"); ok {
return value
}
return defaultValue
}(postgresDSN)
crawlerDelayIntervalSecs = func(defaultValue int) int {
if strvalue, ok := os.LookupEnv("GOOSE_CRAWLER_INTERVAL_SECS"); ok {
if value, err := strconv.ParseInt(strvalue, 10, 64); err == nil {
return int(value)
}
}
return defaultValue
}(crawlerDelayIntervalSecs)
announceDelayIntervalSecs = func(defaultValue int) int {
if strvalue, ok := os.LookupEnv("GOOSE_ANNOUNCE_INTERVAL_SECS"); ok {
if value, err := strconv.ParseInt(strvalue, 10, 64); err == nil {
return int(value)
}
}
return defaultValue
}(announceDelayIntervalSecs)
if discordToken == "" {
return errors.New("missing required Discord token")
}
if postgresDSN == "" {
return errors.New("missing required PostgreSQL DSN")
}
db, err := sql.Open("postgres", postgresDSN)
if err != nil {
return err
}
defer db.Close()
connStartDB := time.Now()
err = func() error {
slog.Info("Connecting to database")
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
return db.PingContext(ctx)
}()
if err != nil {
return fmt.Errorf("ping database: %w", err)
}
slog.With(slog.Duration("duration", time.Since(connStartDB))).Info("Connected to database")
articles := &Articles{
db: db,
}
feeds := &Feeds{
DB: db,
}
subscriptions := &Subscriptions{
db: db,
}
session, err := discordgo.New("Bot " + discordToken)
if err != nil {
return err
}
defer session.Close()
rateLimiter := rate.NewLimiter(rate.Every(time.Second), 1)
bot := &Bot{
articles: articles,
feeds: feeds,
subscriptions: subscriptions,
autocompletions: &AutoCompletions{subscriptions: subscriptions},
rateLimiter: rateLimiter,
session: session,
httpClient: &http.Client{
Timeout: 3 * time.Second,
},
}
session.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
data := i.ApplicationCommandData()
if i.Type == discordgo.InteractionApplicationCommandAutocomplete {
for _, option := range data.Options {
if !option.Focused || option.Name != optionCollectionName {
continue
}
switch data.Name {
case commandUnsubscribe, commandTest:
bot.AutocompleteCollectionName(s, i.Interaction, option)
return
default:
return
}
}
}
switch data.Name {
case commandSubscribe:
bot.Subscribe(s, i.Interaction)
case commandUnsubscribe:
bot.Unsubscribe(s, i.Interaction)
case commandTest:
bot.Test(s, i.Interaction)
}
})
connStartDisc := time.Now()
err = session.Open()
if err != nil {
return err
}
for _, cmd := range commands {
_, err := session.ApplicationCommandCreate(session.State.User.ID, "", cmd)
if err != nil {
return fmt.Errorf("register command: %w", err)
}
}
slog.With("duration", time.Since(connStartDisc)).Info("Connected to Discord")
updateTicker := time.NewTicker(time.Duration(announceDelayIntervalSecs) * time.Second)
defer updateTicker.Stop()
refreshTicker := time.NewTicker(time.Duration(crawlerDelayIntervalSecs) * time.Second)
defer refreshTicker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-updateTicker.C:
_ = bot.Update(ctx)
case <-refreshTicker.C:
_ = bot.RefreshFeeds(ctx)
}
}
}