-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
191 lines (163 loc) · 4.71 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
package main
import (
"context"
"flag"
"net/http"
"os"
"os/signal"
"time"
"github.com/alexliesenfeld/health"
"github.com/bwmarrin/discordgo"
"github.com/go-redis/redis/v8"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// Variables used for command line parameters
var (
Token string // Discord bot token
// Global stuff for Redis and Ctrl-C
redisCtx context.Context
cancelCtx context.Context
cancel context.CancelFunc
rdb *redis.Client
redisAddr = flag.String("redis", "localhost:6379", "Address and port of redis host")
redisPass = flag.String("rpass", "", "Redis password")
redisUser = flag.String("ruser", "", "Redis username")
id = flag.String("id", "discord", "ID to use when publishing messages")
verbose = flag.Bool("verbose", false, "Enable verbose logging")
)
// init is run before main() and is used to initialize the logger
func init() {
// Setup logging
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
// Setup command line parameters
flag.StringVar(&Token, "t", "", "Bot Token") // Discord bot token
flag.Parse() // Parse the command line parameters
// Parse environment variables
parseEnv()
// Set the contexts before we start the program
redisCtx = context.Background()
cancelCtx, cancel = context.WithCancel(context.Background())
// Set the log level
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if *verbose {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
}
func main() {
// Hold the program open until CTRL-C is pressed
log.Info().Str("func", "main").Msg("Gateway is now running. Press CTRL-C to exit.")
defer cancel()
// Setup channel for capturing CTRL-C
cancelChan := make(chan os.Signal, 1)
signal.Notify(cancelChan, os.Interrupt)
defer func() {
signal.Stop(cancelChan)
cancel()
}()
// Log our input parameters
log.Info().
Str("func", "main").
Str("id", *id).
Str("redis", *redisAddr).
Msg("Starting Discord gateway")
// Create a new Discordgo session using the provided bot token.
dgo, err := discordgo.New("Bot " + Token)
if err != nil {
log.Fatal().Err(err).Msg("Unable to create Discord session")
return
}
// Test our connection to Discord
dgo.AddHandlerOnce(func(s *discordgo.Session, r *discordgo.Ready) {
log.Info().
Str("func", "main").
Str("id", r.User.ID).
Str("username", r.User.Username).
Str("discriminator", r.User.Discriminator).
Msg("Connected to Discord")
})
// Register the messageCreate func as a callback for MessageCreate events.
dgo.AddHandler(messageCreate)
// Connect to redis
rdb = redisConnect(*redisAddr, *redisUser, *redisPass, 0, redisCtx)
if rdb == nil {
log.Fatal().Msg("Unable to connect to Redis")
return
}
defer rdb.Close()
// Ping redis one more time to make sure we're connected
_, err = rdb.Ping(redisCtx).Result()
if err != nil {
log.Fatal().
Err(err).
Str("func", "main").
Msg("Error pinging Redis")
}
// Open a websocket connection to Discord and begin listening.
// Error out if we can't connect
err = dgo.Open()
if err != nil {
log.Fatal().
Err(err).
Str("func", "main").
Msg("Error opening connection to Discord")
}
defer dgo.Close()
// Handle outbound messages in a goroutine
go handleOutbound(redisCtx, rdb, dgo)
// Setup a health check endpoint
checker := health.NewChecker(
health.WithCacheDuration(1*time.Second),
health.WithTimeout(10*time.Second),
// Check the redis connection with a ping
health.WithPeriodicCheck(
15*time.Second,
3*time.Second,
health.Check{
Name: "redis",
Check: func(ctx context.Context) error {
_, err := rdb.Ping(ctx).Result()
return err
},
}),
// Test the redis pubsub connection by subscribing and unsubscribing
health.WithPeriodicCheck(
15*time.Second,
3*time.Second,
health.Check{
Name: "redis-pubsub",
Check: func(ctx context.Context) error {
pubsub := rdb.Subscribe(ctx, "test")
_, err := pubsub.Receive(ctx)
if err != nil {
return err
}
err = pubsub.Close()
return err
},
}),
// Test the Discord connection by sending a message to our own ID
health.WithPeriodicCheck(
15*time.Second,
3*time.Second,
health.Check{
Name: "discord",
Check: func(ctx context.Context) error {
_, err := dgo.ChannelMessageSend(dgo.State.User.ID, "Test")
return err
},
}),
)
// Register the health check endpoint
http.Handle("/health", health.NewHandler(checker))
// Start the health check server
go log.Fatal().Err(http.ListenAndServe(":8080", nil)).Msg("Error starting health check server")
// for loop to hold the program open until CTRL-C is pressed
for {
select {
case <-cancelChan:
log.Info().Str("func", "main").Msg("CTRL-C pressed. Exiting.")
return
}
}
}