-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
373 lines (318 loc) · 11.5 KB
/
utils.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/bwmarrin/discordgo"
)
func AddGamertagToDB(discordID, xblID string) {
databaseLock.Lock()
databaseMap[discordID] = xblID
databaseLock.Unlock()
dirtyDatabase = true
}
func AddRoleCheckCooldown(discordID string) {
cooldownLock.Lock()
cooldownMap[discordID] = time.Now().Add(time.Hour * 1)
cooldownLock.Unlock()
}
func CheckCooldown(discordID string) (bool, time.Duration) {
cooldownLock.Lock()
expirationTime, exists := cooldownMap[discordID]
cooldownLock.Unlock()
if exists {
return expirationTime.After(time.Now()), expirationTime.Sub(time.Now())
}
return false, 0
}
// This function will query Spartan Assault & Spartan Strike legacy platform achievements
// as they can be bugged if we check the profile with RequestPlayerAchievements
// while querying individual title IDs will return the correct state
func CheckLegacyAssaultStrikeAchievements(discordID string) (map[string]GameStatus, error) {
// We'll skip over Spartan Assault X1/XSX because this endpoint can only detect older versions
gamesToCheck := map[string]GameStatus{
hsaTitleID: NOT_FOUND, // Windows version
hsa360TitleID: NOT_FOUND,
hsaWPTitleID: NOT_FOUND,
hsaIOSTitleID: NOT_FOUND,
hssTitleID: NOT_FOUND,
hssWPTitleID: NOT_FOUND,
hssIOSTitleID: NOT_FOUND,
}
// Hardcode the achievement count for these game to reduce the number of calls to the API as they're unlikely to change
// We have to check 7 games (8th being X1/XSX which can be found by the other endpoint)
// but this endpoint will only give the achievements the player has.
// This means we either check against hardcoded numbers or call another endpoint which will give
// the total amount of achievements, which would double the number of calls per user to 14 just for SS/SA
const (
AssaultAchievCount = 25 // Windows/WP
AssaultIOSAchievCount = 20 // 5 unobtainable achievements in iOS
Assault360AchievCount = 28
StrikeAchievCount = 20 // Spartan Strike has 20 achievements on all 3 platforms
)
// This function should never be called before RequestPlayerAchievements so we'll skip the checks
xbID, _ := GetGamertagID(discordID)
for titleID := range gamesToCheck {
url := "https://xbl.io/api/v2/achievements/x360/" + xbID + "/title/" + titleID
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Add("X-Authorization", tokens.OpenXBL)
req.Header.Add("Accept", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, errors.New("Whoops! Server responded with an error! Apologies, please try again!")
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.New("Whoops! Server responded with an error! Apologies, please try again!")
}
var playerAchievsInfo AchievListInfo
err = json.Unmarshal(body, &playerAchievsInfo)
if err != nil {
return nil, errors.New("Whoops! Server responded with an error! Apologies, please try again!")
}
achievCountToCheck := 0
switch titleID {
case hsaTitleID:
fallthrough
case hsaWPTitleID:
achievCountToCheck = AssaultAchievCount
case hsaIOSTitleID:
achievCountToCheck = AssaultIOSAchievCount
case hsa360TitleID:
achievCountToCheck = Assault360AchievCount
case hssTitleID:
fallthrough
case hssWPTitleID:
fallthrough
case hssIOSTitleID:
achievCountToCheck = StrikeAchievCount
}
if playerAchievsInfo.PagingInfo.TotalRecords == achievCountToCheck {
gamesToCheck[titleID] = COMPLETED
} else if playerAchievsInfo.PagingInfo.TotalRecords == 0 {
gamesToCheck[titleID] = NOT_FOUND
} else {
gamesToCheck[titleID] = NOT_COMPLETED
}
}
return gamesToCheck, nil
}
func CheckTimedAchievs(session *discordgo.Session) {
today := time.Now().UTC()
if today.Hour() != 9 || today.Minute() != 0 { // TODO: Find a better way to activate at 9 AM UTC
return
}
targetChannelID := "984160204440633454" // general-mcc channel ID
baseText := "Remember to grab your <@&%d> achievement today! %s\n\n***If this message helped you get the achievement, make sure to react with <:pepeok:1117969363627159622> so I can remove the role from you!***"
if timedRole, exists := timedAchievRoles[today.Day()]; exists {
specificText := fmt.Sprintf("Simply start up a mission or load into a multiplayer game in %s", timedRole.Game)
session.ChannelMessageSend(targetChannelID, fmt.Sprintf(baseText, timedRole.ID, specificText))
}
for _, date := range destinationVacationDates {
if today.Day() == date.Day && today.Month() == date.Month {
session.ChannelMessageSend(targetChannelID, fmt.Sprintf(baseText, 990602317575368724, "Simply load up a Custom Game on Halo 2 Classic Zanzibar, go to the beach and look at the sign next to the water!"))
break
}
}
for _, date := range elderSignsDates {
if today.Day() == date.Day && today.Month() == date.Month {
session.ChannelMessageSend(targetChannelID, fmt.Sprintf(baseText, 990602348659363850, "Simply load up a Custom Game on Halo 3 Valhalla and look at the Sigil on the wall. Remember you need to have looked at 2 different ones for it to unlock!"))
break
}
}
}
func GetAllGuildMembers(s *discordgo.Session, guildID string) []*discordgo.Member {
guildMembers, _ := s.GuildMembers(guildID, "", 1000)
// Discord API can only return a maximum of 1000 members.
// To get all the members for guilds that have more than this limit
// we check the length of the returned slice and if it's 1000 we try to grab
// the next 1000 starting from the last member in the previous request using it's ID,
// repeating this until we get a slice with less than 1000.
gotAll := len(guildMembers) < 1000
for !gotAll {
lastID := guildMembers[len(guildMembers)-1].User.ID
tempGMembers, _ := s.GuildMembers(guildID, lastID, 1000)
if len(tempGMembers) < 1000 {
gotAll = true
}
guildMembers = append(guildMembers, tempGMembers...)
}
return guildMembers
}
func GetAsPtr[T any](v T) *T {
return &v
}
func GetCompletionSymbol(status GameStatus) string {
switch status {
case NOT_FOUND:
return "❌"
case NOT_COMPLETED:
return "🔶"
case COMPLETED:
return "✅"
}
return "❔"
}
func GetGamertagID(discordID string) (xuid string, exists bool) {
databaseLock.Lock()
xuid, exists = databaseMap[discordID]
databaseLock.Unlock()
return
}
func HasRole(member *discordgo.Member, roleID string) bool {
for _, id := range member.Roles {
if id == roleID {
return true
}
}
return false
}
func HasRoles(member *discordgo.Member, rolesID []string) map[string]bool {
rolesMap := make(map[string]bool)
for _, searchID := range rolesID {
rolesMap[searchID] = false
}
for _, id := range member.Roles {
if _, exists := rolesMap[id]; exists {
rolesMap[id] = true
}
}
return rolesMap
}
func IsStaff(member *discordgo.Member) bool {
// Pillar / Oracle (Mod) / Guardian (Admin) / Founder roles
staffRoles := []string{"987989822813650974", "984081125657964664", "984080972108668959", "1075504782023852102"}
result := HasRoles(member, staffRoles)
for _, roleID := range staffRoles {
if result[roleID] {
return true
}
}
return false
}
func LogCommand(cmdName, author string) {
infoLog.Println(cmdName + " command used by " + author)
}
func KeepAliveRequest() {
req, _ := http.NewRequest("GET", "https://xbl.io/api/v2/account", nil)
req.Header.Add("X-Authorization", tokens.OpenXBL)
req.Header.Add("Accept", "application/json")
client := &http.Client{}
resp, _ := client.Do(req)
if resp != nil {
resp.Body.Close()
}
// We don't care about the result, we just want to do a GET request on OpenXBL
// so our token gets refreshed and future requests after idling won't fail
}
func ReplyToMsg(s *discordgo.Session, m *discordgo.Message, replyMsg string) (*discordgo.Message, error) {
return s.ChannelMessageSendReply(m.ChannelID, replyMsg, &discordgo.MessageReference{
MessageID: m.ID,
ChannelID: m.ChannelID,
})
}
func RespondACKToInteraction(s *discordgo.Session, i *discordgo.Interaction) error {
return s.InteractionRespond(i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
})
}
func RespondACKPing(s *discordgo.Session, i *discordgo.Interaction) error {
return s.InteractionRespond(i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponsePong,
})
}
func RespondFollowUpToInteraction(s *discordgo.Session, i *discordgo.Interaction, respondMsg string) (*discordgo.Message, error) {
return s.FollowupMessageCreate(i, true, &discordgo.WebhookParams{
Content: respondMsg,
})
}
func RespondToInteraction(s *discordgo.Session, i *discordgo.Interaction, respondMsg string) error {
return s.InteractionRespond(i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: respondMsg,
},
})
}
func RespondToInteractionEphemeral(s *discordgo.Session, i *discordgo.Interaction, respondMsg string) error {
return s.InteractionRespond(i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: respondMsg,
Flags: discordgo.MessageFlagsEphemeral,
},
})
}
func RequestPlayerAchievements(discordID string) ([]GameStatsResp, error) {
xbID, ok := GetGamertagID(discordID)
if !ok {
return nil, errors.New("Please set your gamertag first using the `/gamertag` command")
}
url := "https://xbl.io/api/v2/achievements/player/" + xbID
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Add("X-Authorization", tokens.OpenXBL)
req.Header.Add("Accept", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, errors.New("Whoops! Server responded with an error! Apologies, please try again!")
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
var objMap map[string]json.RawMessage
err = decoder.Decode(&objMap)
if err != nil {
return nil, errors.New("Whoops! Server responded with an error! Apologies, please try again!")
}
var gamesStats []GameStatsResp
err = json.Unmarshal(objMap["titles"], &gamesStats)
if err != nil {
return nil, errors.New("Whoops! Server responded with an error! Apologies, please try again!")
}
if len(gamesStats) == 0 {
return nil, errors.New("You have either not played any games or your Xbox profile is private.")
}
return gamesStats, nil
}
func RequestPlayerGT(gamerTag string) (string, error) {
// Gamertags with a suffix should not include the hashtag
urlTag := strings.ReplaceAll(gamerTag, "#", "")
urlTag = strings.ReplaceAll(urlTag, " ", "%20")
url := "https://xbl.io/api/v2/friends/search?gt=" + urlTag
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
req.Header.Add("X-Authorization", tokens.OpenXBL)
req.Header.Add("Accept", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", errors.New("Whoops! Server responded with an error! Apologies, please try again!")
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
var objMap map[string]json.RawMessage
err = decoder.Decode(&objMap)
if err != nil {
return "", errors.New("Server responded with garbage! Not your fault. Please try again now!")
}
var respID []GTResp
err = json.Unmarshal(objMap["profileUsers"], &respID)
if err != nil {
return "", errors.New("Hmm, that gamertag didn't work! Please make sure you typed the gamertag correctly.")
}
return respID[0].ID, nil
}