-
Notifications
You must be signed in to change notification settings - Fork 1
/
discord.go
302 lines (278 loc) · 7.75 KB
/
discord.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
package main
import (
"fmt"
"regexp"
"strings"
"github.com/bwmarrin/discordgo"
)
type DiscordBot struct {
Session *discordgo.Session
AllowMessageContentIntent bool
registeredCommands []*discordgo.ApplicationCommand
}
func LaunchDiscordBot(botToken string, allowMessageContentIntent bool) (*DiscordBot, error) {
dg, err := discordgo.New("Bot " + botToken)
if err != nil {
return nil, err
}
dg.AddHandler(messageCreate)
dg.AddHandler(interactionCreate)
dg.Identify.Intents |= discordgo.IntentsGuildMessages
if allowMessageContentIntent {
dg.Identify.Intents |= discordgo.IntentMessageContent
}
err = dg.Open()
if err != nil {
return nil, err
}
bot := DiscordBot{
Session: dg,
AllowMessageContentIntent: allowMessageContentIntent,
}
bot.setupSlashCommands()
return &bot, nil
}
func (bot *DiscordBot) CloseDiscordBot() {
for _, val := range bot.registeredCommands {
err := bot.Session.ApplicationCommandDelete(bot.Session.State.User.ID, "", val.ID)
if err == nil {
logger.Sugar().Infof("Deleted a command '%#v'", val.Name)
} else {
logger.Sugar().Errorf("Cannot delete command '%v': %v", val.Name, err)
}
}
bot.Session.Close()
}
func (bot *DiscordBot) setupSlashCommands() {
commands := []*discordgo.ApplicationCommand{
{
Name: "regular",
Description: "Return a schedule for regular match",
},
{
Name: "bankara",
Description: "Return a schedule for both Open and Challenge match",
},
{
Name: "open",
Description: "Return a schedule for Open match",
},
{
Name: "challenge",
Description: "Return a schedule for Challenge match",
},
{
Name: "salmon",
Description: "Return a schedule for Salmon Run",
},
{
Name: "x",
Description: "Return a schedule for X Match",
},
{
Name: "rule",
Description: "Search both schedules from Open and Challenge match by rule name",
Options: []*discordgo.ApplicationCommandOption{
{
Name: "rule",
Description: "a rule name to search",
Type: discordgo.ApplicationCommandOptionString,
Required: true,
Choices: []*discordgo.ApplicationCommandOptionChoice{
{
Name: "turf-war",
NameLocalizations: map[discordgo.Locale]string{
discordgo.Japanese: "ナワバリバトル",
},
Value: "TURF_WAR",
},
{
Name: "area",
NameLocalizations: map[discordgo.Locale]string{
discordgo.Japanese: "ガチエリア",
},
Value: "AREA",
},
{
Name: "rainmarker",
NameLocalizations: map[discordgo.Locale]string{
discordgo.Japanese: "ガチホコバトル",
},
Value: "GOAL",
},
{
Name: "tower-control",
NameLocalizations: map[discordgo.Locale]string{
discordgo.Japanese: "ガチヤグラ",
},
Value: "LOFT",
},
{
Name: "clam-blitz",
NameLocalizations: map[discordgo.Locale]string{
discordgo.Japanese: "ガチアサリ",
},
Value: "CLAM",
},
},
},
},
},
}
bot.registeredCommands = make([]*discordgo.ApplicationCommand, len(commands))
for idx, val := range commands {
registered, err := bot.Session.ApplicationCommandCreate(bot.Session.State.User.ID, "", val)
if err == nil {
logger.Sugar().Infof("Created a command '%#v'", val.Name)
} else {
logger.Sugar().Errorf("Cannot create command '%#v': %#v", val.Name, err)
}
bot.registeredCommands[idx] = registered
}
}
func printWeaponsList(weapons []WeaponInfo) string {
return fmt.Sprintf("%s\n%s\n%s\n%s", weapons[0].Name, weapons[1].Name, weapons[2].Name, weapons[3].Name)
}
func createMessageEmbedFromTimeSlotInfo(srs SearchResultSlot) *discordgo.MessageEmbed {
if srs.tsi == nil {
return &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{
Name: srs.mode.getModeName(),
},
Description: "Not Found!",
}
}
if srs.mode.getIdentifier() == "SALMON" {
return &discordgo.MessageEmbed{
Title: srs.tsi.Stage.Name,
Author: &discordgo.MessageEmbedAuthor{
Name: srs.mode.getModeName(),
},
Description: fmt.Sprintf("%d/%d %d時~%d/%d %d時\n\n%s",
srs.tsi.StartTime.Month(), srs.tsi.StartTime.Day(), srs.tsi.StartTime.Hour(),
srs.tsi.EndTime.Month(), srs.tsi.EndTime.Day(), srs.tsi.EndTime.Hour(),
printWeaponsList(srs.tsi.Weapons)),
Color: srs.mode.getColor(),
}
} else {
return &discordgo.MessageEmbed{
Title: srs.tsi.Rule.Name,
Author: &discordgo.MessageEmbedAuthor{
Name: srs.mode.getModeName(),
},
Description: fmt.Sprintf("%d/%d %d時~%d/%d %d時\n\n%s\n%s",
srs.tsi.StartTime.Month(), srs.tsi.StartTime.Day(), srs.tsi.StartTime.Hour(),
srs.tsi.EndTime.Month(), srs.tsi.EndTime.Day(), srs.tsi.EndTime.Hour(),
srs.tsi.Stages[0].Name, srs.tsi.Stages[1].Name),
Color: srs.mode.getColor(),
}
}
}
func createStageInfoEmbeds(sr SearchResult) []*discordgo.MessageEmbed {
var embeds []*discordgo.MessageEmbed
for _, slot := range sr.Slots {
embeds = append(embeds, createMessageEmbedFromTimeSlotInfo(slot))
}
return embeds
}
func isMentioned(user *discordgo.User, mentions []*discordgo.User, messageContent string) bool {
for _, mention := range mentions {
if mention.ID == user.ID {
return true
}
}
return false
}
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.Bot {
return
}
input := m.Message.Content
if input == "" {
return
}
// remove mention syntax
regex := regexp.MustCompile(` *<@&?\d+?> *`)
input = regex.ReplaceAllString(input, "")
// remove spaces
input = strings.ReplaceAll(input, " ", "")
// parse
query := Parse(input)
// ignore when no match
if query.OriginalText == "" {
return
}
// query
scheduleStore.MaybeRefresh()
sr := scheduleStore.Search(query)
// reply
var err error
if sr.Found {
_, err = s.ChannelMessageSendEmbedsReply(m.ChannelID, createStageInfoEmbeds(sr), m.Reference())
} else {
if isMentioned(s.State.User, m.Mentions, input) {
_, err = s.ChannelMessageSendReply(m.ChannelID, "Not Found!", m.Reference())
}
}
if err != nil {
logger.Sugar().Error(err)
}
}
func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
commandName2mode := map[string]string{
"regular": "REGULAR",
"bankara": "BANKARA",
"open": "OPEN",
"challenge": "CHALLENGE",
"salmon": "SALMON",
"x": "X",
}
commandName := i.ApplicationCommandData().Name
var query *SearchQuery
modeName, found := commandName2mode[commandName]
if found {
query = &SearchQuery{Mode: getMode(modeName)}
}
if commandName == "rule" {
opts := i.ApplicationCommandData().Options
if len(opts) > 0 {
query = &SearchQuery{Mode: getMode("BYRULE"), Rule: opts[0].Value.(string)}
}
}
// check command structure is valid
if query == nil {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Invalid command!",
},
})
if err != nil {
logger.Sugar().Error(err)
}
}
// if valid, query to schedule store
scheduleStore.MaybeRefresh()
sr := scheduleStore.Search(query)
// reply
var err error
if sr.Found {
embeds := createStageInfoEmbeds(sr)
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: embeds,
},
})
} else {
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Not Found!",
},
})
}
if err != nil {
logger.Sugar().Error(err)
}
}