/
sc_handler_sot_achievement.go
83 lines (75 loc) · 2.1 KB
/
sc_handler_sot_achievement.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
package bot
import (
"encoding/json"
"fmt"
"github.com/bwmarrin/discordgo"
)
// SoTAchievementList represents the JSON structure of the Sea of Thieves achievements API response
type SoTAchievementList struct {
Sorted []SoTSortedAchievement `json:"sorted"`
}
// SoTSortedAchievement is a subpart of the Sea of Thieves achievements API response
type SoTSortedAchievement struct {
Achievement SoTAchievement `json:"achievement"`
}
// SoTAchievement is a single achievement in the Sea of Thieves achievements API response
type SoTAchievement struct {
Sort int `json:"Sort"`
Name string `json:"Name"`
Description string `json:"Description"`
MediaURL string `json:"MediaUrl"`
}
// SlashCmdSoTAchievement handles the /achievement slash command
func (b *Bot) SlashCmdSoTAchievement(s *discordgo.Session, i *discordgo.InteractionCreate) error {
r, err := b.NewRequester(i.Interaction)
if err != nil {
return err
}
al, err := b.SoTGetAchievements(r)
if err != nil {
return err
}
if len(al.Sorted) <= 0 {
return fmt.Errorf("no SoT achievements found")
}
a := al.Sorted[0].Achievement
e := []*discordgo.MessageEmbed{
{
Title: fmt.Sprintf("Your latest Sea of Thieves achievement: %s", a.Name),
Description: a.Description,
Image: &discordgo.MessageEmbedImage{
URL: a.MediaURL,
},
Type: discordgo.EmbedTypeImage,
},
}
if _, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{Embeds: &e}); err != nil {
return err
}
return nil
}
// SoTGetAchievements returns the parsed API response from the Sea of Thieves achievements API
func (b *Bot) SoTGetAchievements(rq *Requester) (SoTAchievementList, error) {
var a SoTAchievementList
hc, err := NewHTTPClient()
if err != nil {
return a, fmt.Errorf(ErrFailedHTTPClient, err)
}
c, err := rq.GetSoTRATCookie()
if err != nil {
return a, err
}
r, err := hc.HTTPReq(APIURLSoTAchievements, ReqMethodGet, nil)
if err != nil {
return a, err
}
r.SetSOTRequest(c)
rd, _, err := hc.Fetch(r)
if err != nil {
return a, err
}
if err := json.Unmarshal(rd, &a); err != nil {
return a, err
}
return a, nil
}