-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_jerkcity.go
146 lines (121 loc) · 3.91 KB
/
cmd_jerkcity.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
package bot
import (
"context"
"fmt"
"net/url"
"strings"
"time"
"github.com/diamondburned/arikawa/v3/api"
"github.com/diamondburned/arikawa/v3/api/cmdroute"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/holedaemon/bot2/internal/api/jerkcity"
"github.com/zikaeroh/ctxlog"
"go.uber.org/zap"
)
var jerkcityEmbedColor = discord.Color(2123412)
func makeEpisodeEmbed(e *jerkcity.Episode) discord.Embed {
return discord.Embed{
Title: e.Title,
Color: jerkcityEmbedColor,
Timestamp: discord.NewTimestamp(e.Time()),
Image: &discord.EmbedImage{
URL: "https://bonequest.com/" + e.Image,
},
URL: fmt.Sprintf("https://bonequest.com/%d", e.Episode),
}
}
func (b *Bot) cmdJerkcityEpisode(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData {
opt := data.Options.Find("number")
number, err := opt.IntValue()
if err != nil {
ctxlog.Error(ctx, "error reading value as int", zap.Error(err))
return respondError("Something about the episode number you gave is wrong. Fix it.")
}
episode, err := b.jerkcity.FetchEpisode(ctx, int(number))
if err != nil {
ctxlog.Error(ctx, "error fetching episode", zap.Error(err), zap.Int64("number", number))
return respondError("Sorry, something went wrong and I couldn't get that episode")
}
return respondEmbeds(makeEpisodeEmbed(episode))
}
func (b *Bot) cmdJerkcityLatest(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData {
high, err := b.jerkcity.FetchMeta(ctx)
if err != nil {
ctxlog.Error(ctx, "error fetching meta payload", zap.Error(err))
return respondError("Couldn't get the latest episode number. Sorry?")
}
episode, err := b.jerkcity.FetchEpisode(ctx, high)
if err != nil {
ctxlog.Error(ctx, "error fetching latest episode", zap.Error(err))
return respondError("Couldn't get the latest episode. Sorry I guess")
}
return respondEmbeds(makeEpisodeEmbed(episode))
}
func (b *Bot) cmdJerkcityRandom(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData {
episode, err := b.jerkcity.FetchQuote(ctx)
if err != nil {
ctxlog.Error(ctx, "error fetching episode", zap.Error(err))
return respondError("Sorry, something went wrong and I wasn't able to get an episode")
}
embed := makeEpisodeEmbed(episode)
embed.Fields = append(embed.Fields, discord.EmbedField{
Name: "Quote",
Value: episode.Quote,
})
return respondEmbeds(embed)
}
func (b *Bot) cmdJerkcitySearch(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData {
so := data.Options.Find("query")
search := so.String()
results, err := b.jerkcity.FetchSearch(ctx, search)
if err != nil {
ctxlog.Error(ctx, "error searching jerkcity API", zap.Error(err))
return respondError("Something happened and I wasn't able to perform the search")
}
embed := discord.Embed{
Title: "Search Results",
Color: jerkcityEmbedColor,
Footer: &discord.EmbedFooter{
Text: fmt.Sprintf("Took %s", time.Second*time.Duration(results.Search.Runtime)),
},
}
if len(results.Episodes) == 0 {
embed.Description = "No results..."
} else {
var sb strings.Builder
stop := 10
for i, e := range results.Episodes {
if i == stop-1 {
break
}
sb.WriteString(
fmt.Sprintf("[%d - %s](https://bonequest.com/%d)\n", e.Episode, e.Title, e.Episode),
)
}
if len(results.Episodes) > stop {
more := len(results.Episodes[9:])
sb.WriteString(
fmt.Sprintf(
"[and %d more](https://bonequest.com/search/?q=%s)",
more,
url.QueryEscape(search),
),
)
}
embed.Description = sb.String()
}
if results.Search.Sums != nil {
embed.Fields = append(embed.Fields, discord.EmbedField{
Name: "Sums",
Value: fmt.Sprintf(
"Dates: %d\nEpisodes: %d\nTags: %d\nTitles: %d\nWords: %d",
results.Search.Sums.Dates,
results.Search.Sums.Episodes,
results.Search.Sums.Tags,
results.Search.Sums.Titles,
results.Search.Sums.Words,
),
})
}
return respondEmbeds(embed)
}