-
-
Notifications
You must be signed in to change notification settings - Fork 286
/
bot.go
403 lines (337 loc) · 12.4 KB
/
bot.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package bot
import (
"context"
"errors"
"fmt"
"github.com/automuteus/automuteus/v8/bot/command"
"github.com/automuteus/automuteus/v8/bot/tokenprovider"
"github.com/automuteus/automuteus/v8/internal/server"
"github.com/automuteus/automuteus/v8/pkg/amongus"
"github.com/automuteus/automuteus/v8/pkg/discord"
"github.com/automuteus/automuteus/v8/pkg/game"
"github.com/automuteus/automuteus/v8/pkg/premium"
"github.com/automuteus/automuteus/v8/pkg/rediskey"
"github.com/automuteus/automuteus/v8/pkg/settings"
storageutils "github.com/automuteus/automuteus/v8/pkg/storage"
"github.com/automuteus/automuteus/v8/pkg/token"
"github.com/automuteus/automuteus/v8/storage"
"github.com/bwmarrin/discordgo"
"github.com/top-gg/go-dbl"
"log"
"os"
"strconv"
"sync"
"time"
)
type Bot struct {
version string
commit string
official bool
url string
// mapping of socket connections to the game connect codes
ConnsToGames map[string]string
StatusEmojis AlivenessEmojis
EndGameChannels map[string]chan EndGameMessage
ChannelsMapLock sync.RWMutex
PrimarySession *discordgo.Session
TokenProvider *tokenprovider.TokenProvider
TopGGClient *dbl.Client
RedisInterface *RedisInterface
StorageInterface *storage.StorageInterface
PostgresInterface *storageutils.PsqlInterface
logPath string
captureTimeout int
}
// MakeAndStartBot does what it sounds like
// TODO collapse these fields into proper structs?
func MakeAndStartBot(version, commit, botToken, topGGToken, url, emojiGuildID string, numShards, shardID int, redisInterface *RedisInterface, storageInterface *storage.StorageInterface, psql *storageutils.PsqlInterface, logPath string) *Bot {
dg, err := discordgo.New("Bot " + botToken)
if err != nil {
log.Println("error creating Discord session,", err)
return nil
}
if numShards > 1 {
log.Printf("Identifying to the Discord API with %d total shards, and shard ID=%d\n", numShards, shardID)
dg.ShardCount = numShards
dg.ShardID = shardID
}
bot := Bot{
version: version,
commit: commit,
official: os.Getenv("AUTOMUTEUS_OFFICIAL") != "",
url: url,
ConnsToGames: make(map[string]string),
StatusEmojis: emptyStatusEmojis(),
EndGameChannels: make(map[string]chan EndGameMessage),
ChannelsMapLock: sync.RWMutex{},
PrimarySession: dg,
RedisInterface: redisInterface,
StorageInterface: storageInterface,
PostgresInterface: psql,
logPath: logPath,
captureTimeout: GameTimeoutSeconds,
}
dg.LogLevel = discordgo.LogInformational
dg.AddHandler(bot.handleVoiceStateChange)
dg.AddHandler(bot.newGuild(emojiGuildID))
dg.AddHandler(bot.leaveGuild)
dg.AddHandler(bot.rateLimitEventCallback)
// Slash commands
dg.AddHandler(bot.handleInteractionCreate)
dg.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Println("Bot is now online according to discord Ready handler")
})
dg.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsGuildVoiceStates | discordgo.IntentsGuilds)
token.WaitForToken(bot.RedisInterface.client, botToken)
token.LockForToken(bot.RedisInterface.client, botToken)
// Open a websocket connection to Discord and begin listening.
err = dg.Open()
if err != nil {
log.Println("Could not connect Bot to the Discord Servers with error:", err)
return nil
}
log.Println("Finished identifying to the Discord API. Now ready for incoming events")
listeningTo := os.Getenv("AUTOMUTEUS_LISTENING")
if listeningTo == "" {
listeningTo = "/help"
}
// pretty sure this needs to happen per-shard
status := &discordgo.UpdateStatusData{
IdleSince: nil,
Activities: []*discordgo.Activity{&discordgo.Activity{
Name: listeningTo,
Type: discordgo.ActivityTypeListening,
}},
AFK: false,
Status: "",
}
err = dg.UpdateStatusComplex(*status)
if err != nil {
log.Println(err)
}
if topGGToken != "" {
dblClient, err := dbl.NewClient(topGGToken)
if err != nil {
log.Println("Error creating Top.gg client: ", err)
}
bot.TopGGClient = dblClient
} else {
log.Println("No TOP_GG_TOKEN provided")
}
return &bot
}
func (bot *Bot) InitTokenProvider(tp *tokenprovider.TokenProvider) {
tp.Init(bot.RedisInterface.client, bot.PrimarySession)
}
func (bot *Bot) StartMetricsServer(nodeID string) error {
return server.PrometheusMetricsServer(bot.RedisInterface.client, nodeID, "2112")
}
func (bot *Bot) Close() {
bot.PrimarySession.Close()
bot.RedisInterface.Close()
bot.StorageInterface.Close()
}
var EmojiLock = sync.Mutex{}
func (bot *Bot) newGuild(emojiGuildID string) func(s *discordgo.Session, m *discordgo.GuildCreate) {
return func(s *discordgo.Session, m *discordgo.GuildCreate) {
gid, err := strconv.ParseUint(m.Guild.ID, 10, 64)
if err != nil {
log.Println(err)
}
go func() {
_, err = bot.PostgresInterface.EnsureGuildExists(gid, m.Guild.Name)
if err != nil {
log.Println(err)
}
}()
log.Printf("Added to new Guild, id %s, name %s", m.Guild.ID, m.Guild.Name)
bot.RedisInterface.AddUniqueGuildCounter(m.Guild.ID)
if emojiGuildID == "" {
log.Println("[This is not an error] No explicit guildID provided for emojis; using the current guild default")
emojiGuildID = m.Guild.ID
}
// only check/add emojis to the server denoted for emojis, OR, this server that we picked as a fallback above ^
uploadMissingEmojis := emojiGuildID == m.Guild.ID
EmojiLock.Lock()
// only add the emojis if they haven't been added already. Saves api calls for bots in guilds
if bot.StatusEmojis.isEmpty() {
allEmojis, err := s.GuildEmojis(emojiGuildID)
if err != nil {
log.Println(err)
} else {
bot.verifyEmojis(s, emojiGuildID, true, allEmojis, uploadMissingEmojis)
bot.verifyEmojis(s, emojiGuildID, false, allEmojis, uploadMissingEmojis)
}
}
EmojiLock.Unlock()
games := bot.RedisInterface.LoadAllActiveGames(m.Guild.ID)
for _, connCode := range games {
gsr := GameStateRequest{
GuildID: m.Guild.ID,
ConnectCode: connCode,
}
lock, dgs := bot.RedisInterface.GetDiscordGameStateAndLock(gsr)
for lock == nil {
lock, dgs = bot.RedisInterface.GetDiscordGameStateAndLock(gsr)
}
if dgs != nil && dgs.ConnectCode != "" {
log.Println("Resubscribing to Redis events for an old game: " + connCode)
killChan := make(chan EndGameMessage)
go bot.SubscribeToGameByConnectCode(gsr.GuildID, dgs.ConnectCode, killChan)
dgs.Subscribed = true
bot.RedisInterface.SetDiscordGameState(dgs, lock)
bot.ChannelsMapLock.Lock()
bot.EndGameChannels[dgs.ConnectCode] = killChan
bot.ChannelsMapLock.Unlock()
}
lock.Release(ctx)
}
}
}
func (bot *Bot) leaveGuild(_ *discordgo.Session, m *discordgo.GuildDelete) {
log.Println("Bot was removed from Guild " + m.ID)
bot.RedisInterface.LeaveUniqueGuildCounter(m.ID)
err := bot.StorageInterface.DeleteGuildSettings(m.ID)
if err != nil {
log.Println(err)
}
}
func (bot *Bot) forceEndGame(gsr GameStateRequest) {
// lock because we don't want anyone else modifying while we delete
lock, dgs := bot.RedisInterface.GetDiscordGameStateAndLock(gsr)
for lock == nil {
lock, dgs = bot.RedisInterface.GetDiscordGameStateAndLock(gsr)
}
deleted := dgs.DeleteGameStateMsg(bot.PrimarySession, true)
if deleted {
go server.RecordDiscordRequests(bot.RedisInterface.client, server.MessageCreateDelete, 1)
}
bot.RedisInterface.SetDiscordGameState(dgs, lock)
bot.RedisInterface.RemoveOldGame(dgs.GuildID, dgs.ConnectCode)
// Note, this shouldn't be necessary with the TTL of the keys, but it can't hurt to clean up...
bot.RedisInterface.DeleteDiscordGameState(dgs)
}
func MessageDeleteWorker(s *discordgo.Session, msgChannelID, msgID string, waitDur time.Duration) {
log.Printf("Message worker is sleeping for %s before deleting message", waitDur.String())
time.Sleep(waitDur)
err := s.ChannelMessageDelete(msgChannelID, msgID)
if err != nil {
log.Println(err)
}
}
func (bot *Bot) RefreshGameStateMessage(gsr GameStateRequest, sett *settings.GuildSettings) bool {
lock, dgs := bot.RedisInterface.GetDiscordGameStateAndLock(gsr)
for lock == nil {
lock, dgs = bot.RedisInterface.GetDiscordGameStateAndLock(gsr)
}
// don't try to edit this message, because we're about to delete it
RemovePendingDGSEdit(dgs.GameStateMsg.MessageID)
// note, this checks the variables being set, not whether or not the actual Discord message still exists
gameExists := dgs.GameStateMsg.Exists()
if !gameExists {
return false // no-op; no active game to refresh
}
deleted := dgs.DeleteGameStateMsg(bot.PrimarySession, false) // delete the old message
created := dgs.CreateMessage(bot.PrimarySession, bot.gameStateResponse(dgs, sett), dgs.GameStateMsg.MessageChannelID, dgs.GameStateMsg.LeaderID)
if deleted && created {
go server.RecordDiscordRequests(bot.RedisInterface.client, server.MessageCreateDelete, 2)
} else if deleted || created {
go server.RecordDiscordRequests(bot.RedisInterface.client, server.MessageCreateDelete, 1)
}
bot.RedisInterface.SetDiscordGameState(dgs, lock)
// if for whatever reason the message failed to create, this would catch it
return dgs.GameStateMsg.Exists()
}
func (bot *Bot) getInfo() command.BotInfo {
totalGuilds := rediskey.GetGuildCounter(context.Background(), bot.RedisInterface.client)
activeGames := rediskey.GetActiveGames(context.Background(), bot.RedisInterface.client, GameTimeoutSeconds)
totalUsers := rediskey.GetTotalUsers(context.Background(), bot.RedisInterface.client)
if totalUsers == rediskey.NotFound {
totalUsers = rediskey.RefreshTotalUsers(context.Background(), bot.RedisInterface.client, bot.PostgresInterface.Pool)
}
totalGames := rediskey.GetTotalGames(context.Background(), bot.RedisInterface.client)
if totalGames == rediskey.NotFound {
totalGames = rediskey.RefreshTotalGames(context.Background(), bot.RedisInterface.client, bot.PostgresInterface.Pool)
}
return command.BotInfo{
Version: bot.version,
Commit: bot.commit,
ShardID: bot.PrimarySession.ShardID,
ShardCount: bot.PrimarySession.ShardCount,
TotalGuilds: totalGuilds,
ActiveGames: activeGames,
TotalUsers: totalUsers,
TotalGames: totalGames,
}
}
func linkPlayer(redis *RedisInterface, dgs *GameState, userID, color string) (command.LinkStatus, error) {
var auData amongus.PlayerData
found := false
if game.IsColorString(color) {
auData, found = dgs.GameData.GetByColor(color)
}
if found {
foundID := dgs.AttemptPairingByUserIDs(auData, map[string]interface{}{userID: struct{}{}})
if foundID != "" {
err := redis.AddUsernameLink(dgs.GuildID, userID, auData.Name)
if err != nil {
log.Println(err)
}
return command.LinkSuccess, nil
} else {
err := fmt.Sprintf("No player in the current game was found matching %s", discord.MentionByUserID(userID))
return command.LinkNoPlayer, errors.New(err)
}
} else {
err := fmt.Errorf("no game data found for player %s and color %s", discord.MentionByUserID(userID), color)
return command.LinkNoGameData, err
}
}
func unlinkPlayer(dgs *GameState, userID string) command.UnlinkStatus {
// if we found the player and cleared their data
success := dgs.ClearPlayerData(userID)
if success {
return command.UnlinkSuccess
} else {
return command.UnlinkNoPlayer
}
}
func getTrackingChannel(guild *discordgo.Guild, userID string) string {
// loop over all the channels in the discord and cross-reference with the one that the .au new author is in
for _, v := range guild.VoiceStates {
// if the User who typed au new is in a voice channel
if v.UserID == userID {
return v.ChannelID
}
}
return ""
}
func (bot *Bot) newGame(dgs *GameState) (_ command.NewStatus, activeGames int64) {
if dgs.GameStateMsg.Exists() {
if v, ok := bot.EndGameChannels[dgs.ConnectCode]; ok {
v <- true
}
delete(bot.EndGameChannels, dgs.ConnectCode)
dgs.Reset()
} else {
premStatus, days, err := bot.PostgresInterface.GetGuildOrUserPremiumStatus(
bot.official, bot.TopGGClient, dgs.GuildID, dgs.GameStateMsg.LeaderID)
if err != nil {
log.Println("Error in /newgame get premium:", err)
}
premTier := premium.FreeTier
if !premium.IsExpired(premStatus, days) {
premTier = premStatus
}
// Premium users should always be allowed to start new games; only check the free guilds
if premTier == premium.FreeTier {
activeGames = rediskey.GetActiveGames(context.Background(), bot.RedisInterface.client, GameTimeoutSeconds)
if activeGames > command.DefaultMaxActiveGames {
return command.NewLockout, activeGames
}
}
}
dgs.ConnectCode = generateConnectCode(dgs.GuildID)
dgs.Subscribed = true
return command.NewSuccess, activeGames
}