-
Notifications
You must be signed in to change notification settings - Fork 938
/
setup.go
409 lines (334 loc) · 11.2 KB
/
setup.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
404
405
406
407
408
409
package rsvp
import (
"context"
"fmt"
"regexp"
"strconv"
"strings"
"sync"
"time"
"unicode/utf8"
"github.com/jonas747/dcmd/v4"
"github.com/jonas747/discordgo/v2"
"github.com/jonas747/dstate/v4"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/scheduledevents2"
"github.com/jonas747/yagpdb/rsvp/models"
"github.com/jonas747/yagpdb/timezonecompanion"
"github.com/volatiletech/sqlboiler/boil"
)
type SetupState int
const (
SetupStateChannel SetupState = iota
SetupStateTitle
SetupStateMaxParticipants
SetupStateWhen
SetupStateWhenConfirm
)
type SetupSession struct {
mu sync.Mutex
plugin *Plugin
setupMessages []int64
CreatedOnMessageID int64
GuildID int64
AuthorID int64
SetupChannel int64
State SetupState
MaxParticipants int
Title string
Channel int64
When time.Time
LastAction time.Time
stopCH chan bool
stopped bool
// the following fields are only set if 1) sendInitialMessage was called
// with interaction data and 2) the interaction response was sent
// successfully
followupMessageID int64
interactionToken string
}
func (s *SetupSession) handleMessage(m *discordgo.Message) {
if s.CreatedOnMessageID == m.ID {
return
}
s.mu.Lock()
defer s.mu.Unlock()
s.setupMessages = append(s.setupMessages, m.ID)
s.LastAction = time.Now()
if strings.EqualFold(m.Content, "exit") || strings.EqualFold(m.Content, "stop") {
s.sendMessage("RSVP Event setup cancelled")
go s.remove()
return
}
switch s.State {
case SetupStateChannel:
s.handleMessageSetupStateChannel(m)
case SetupStateTitle:
s.handleMessageSetupStateTitle(m)
case SetupStateMaxParticipants:
s.handleMessageSetupStateMaxParticipants(m)
case SetupStateWhen:
s.handleMessageSetupStateWhen(m)
case SetupStateWhenConfirm:
s.handleMessageSetupStateWhenConfirm(m)
}
}
func (s *SetupSession) handleMessageSetupStateChannel(m *discordgo.Message) {
targetChannel := int64(0)
gs := bot.State.GetGuild(m.GuildID)
if gs == nil {
logger.WithField("guild", m.GuildID).Error("Guild not found")
return
}
if strings.EqualFold(m.Content, "here") || strings.EqualFold(m.Content, "this") || strings.EqualFold(m.Content, "this one") {
// current channel
targetChannel = s.SetupChannel
} else if strings.HasPrefix(m.Content, "<#") && strings.HasSuffix(m.Content, ">") {
// channel mention
idStr := m.Content[2 : len(m.Content)-1]
if parsed, err := strconv.ParseInt(idStr, 10, 64); err == nil {
if gs.GetChannel(parsed) != nil {
targetChannel = parsed
}
}
} else {
// search by name
nameSearch := strings.ReplaceAll(m.Content, " ", "-")
for _, v := range gs.Channels {
if strings.EqualFold(v.Name, nameSearch) {
targetChannel = v.ID
break
}
}
}
if targetChannel == 0 {
// search by ID
if parsed, err := strconv.ParseInt(m.Content, 10, 64); err == nil {
if gs.GetChannel(parsed) != nil {
targetChannel = parsed
}
}
}
if targetChannel == 0 {
s.sendMessage("Couldn't find that channel, say `this` or `here` for the current channel, otherwise type the name, id or mention it.")
return
}
hasPerms, err := bot.AdminOrPermMS(m.GuildID, targetChannel, dstate.MemberStateFromMember(m.Member), discordgo.PermissionSendMessages)
if err != nil {
s.sendMessage("Failed retrieving your pems, check with bot owner")
logger.WithError(err).WithField("guild", gs.ID).Error("failed calculating permissions")
return
}
if !hasPerms {
s.sendMessage("You don't have permissions to send messages there, please pick another channel")
return
}
s.Channel = targetChannel
s.State = SetupStateTitle
s.sendMessage("Using channel <#%d>. Please enter a title for the event now!", s.Channel)
}
func (s *SetupSession) handleMessageSetupStateTitle(m *discordgo.Message) {
if utf8.RuneCountInString(m.Content) > 256 {
s.sendMessage("Title can only be 256 characters long! Enter a new title now.")
return
}
s.Title = m.Content
s.State = SetupStateMaxParticipants
s.sendMessage("Set title of the event to **%s**, Enter the max number of people able to join (or 0 for no limit)", s.Title)
}
func (s *SetupSession) handleMessageSetupStateMaxParticipants(m *discordgo.Message) {
participants, err := strconv.ParseInt(m.Content, 10, 32)
if err != nil {
s.sendMessage("That wasn't a number! Please enter a number.")
return
}
s.MaxParticipants = int(participants)
s.State = SetupStateWhen
s.sendMessage("Set max participants to **%d**, now please enter when this event starts, in either your registered time zone (using the `setz` command) or UTC. (example: `tomorrow 10pm`, `10 may 2pm UTC`)", s.MaxParticipants)
}
var UTCRegex = regexp.MustCompile(`(?i)\butc\b`)
func (s *SetupSession) handleMessageSetupStateWhen(m *discordgo.Message) {
registeredTimezone := timezonecompanion.GetUserTimezone(s.AuthorID)
if registeredTimezone == nil || UTCRegex.MatchString(m.Content) {
registeredTimezone = time.UTC
}
now := time.Now().In(registeredTimezone)
t, err := dateParser.Parse(m.Content, now)
// t, err := dateparse.ParseAny(m.Content)
if err != nil || t == nil {
s.sendMessage("Couldn't understand that date, Please try changing the format a little bit and try again\n||Error: %v||", err)
return
}
s.When = t.Time
s.State = SetupStateWhenConfirm
in := common.HumanizeDuration(common.DurationPrecisionMinutes, t.Time.Sub(now))
s.sendMessage("Set the starting time of the event to **%s** (in **%s**), is this correct? (`yes/no`)", t.Time.Format("02 Jan 2006 15:04 MST"), in)
}
func (s *SetupSession) handleMessageSetupStateWhenConfirm(m *discordgo.Message) {
lower := strings.ToLower(m.Content)
if len(lower) < 1 {
return
}
if lower[0] == 'y' {
s.Finish()
} else {
s.State = SetupStateWhen
s.sendMessage("Please enter when this event starts. (example: `tomorrow 10pm`, `10 may 2pm`)")
}
}
func (s *SetupSession) Finish() {
// reserve the message
reservedMessage, err := common.BotSession.ChannelMessageSendEmbed(s.Channel, &discordgo.MessageEmbed{Description: "Setting up RSVP Event..."})
if err != nil {
if code, _ := common.DiscordError(err); code != 0 {
if code == discordgo.ErrCodeMissingPermissions || code == discordgo.ErrCodeMissingAccess {
s.sendMessage("The bot doesn't have permissions to send embed messages there, check the permissions again...")
go s.remove()
return
}
}
s.abortError("failed reserving message", err)
return
}
localID, err := common.GenLocalIncrID(s.GuildID, "rsvp_session")
if err != nil {
s.abortError("failed generating local ID", err)
return
}
// insert the model
m := &models.RSVPSession{
MessageID: reservedMessage.ID,
AuthorID: s.AuthorID,
ChannelID: s.Channel,
GuildID: s.GuildID,
LocalID: localID,
CreatedAt: time.Now(),
StartsAt: s.When,
Title: s.Title,
MaxParticipants: s.MaxParticipants,
SendReminders: true,
}
err = m.InsertG(context.Background(), boil.Infer())
if err != nil {
s.abortError("failed inserting model", err)
return
}
// set up the proper message
err = UpdateEventEmbed(m)
if err != nil {
m.DeleteG(context.Background())
s.abortError("failed updating the embed", err)
return
}
err = AddReactions(m.ChannelID, m.MessageID, m.MaxParticipants > 0)
if err != nil {
m.DeleteG(context.Background())
s.abortError("failed adding reactions", err)
return
}
err = scheduledevents2.ScheduleEvent("rsvp_update_session", m.GuildID, NextUpdateTime(m), m.MessageID)
if err != nil {
m.DeleteG(context.Background())
s.abortError("failed scheduling update", err)
return
}
go s.remove()
// finish by deleting the setup messages
toDelete := s.setupMessages
if len(toDelete) > 100 {
toDelete = toDelete[len(toDelete)-100:]
}
common.BotSession.ChannelMessagesBulkDelete(s.SetupChannel, toDelete)
if s.followupMessageID != 0 {
common.BotSession.DeleteInteractionResponse(common.BotApplication.ID, s.interactionToken)
common.BotSession.DeleteFollowupMessage(common.BotApplication.ID, s.interactionToken, s.followupMessageID)
}
}
func (s *SetupSession) abortError(msg string, err error) {
logger.WithField("guild", s.GuildID).WithError(err).Error(msg)
s.sendMessage("An error occurred, the setup has been canceled, please retry in a moment.")
go s.remove()
}
func (s *SetupSession) loopCheckActive() {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-s.stopCH:
return
case <-ticker.C:
}
s.mu.Lock()
since := time.Since(s.LastAction)
if since < time.Minute*3 {
s.mu.Unlock()
continue
}
s.sendMessage("Event setup timed out, you waited over 3 minutes before doing anything, if you still want to set up a event then you have to restart from the beginning by issuing the `event create` commmand")
s.mu.Unlock()
// Session expired, remove it
s.remove()
}
}
func (s *SetupSession) remove() {
s.mu.Lock()
stopped := s.stopped
s.stopped = true
s.mu.Unlock()
if stopped {
return
}
close(s.stopCH)
s.plugin.setupSessionsMU.Lock()
defer s.plugin.setupSessionsMU.Unlock()
for i, v := range s.plugin.setupSessions {
if v == s {
s.plugin.setupSessions = append(s.plugin.setupSessions[:i], s.plugin.setupSessions[i+1:]...)
}
}
}
func (s *SetupSession) sendMessage(msgf string, args ...interface{}) {
m, err := common.BotSession.ChannelMessageSend(s.SetupChannel, "[RSVP Event Setup]: "+fmt.Sprintf(msgf, args...))
if err != nil {
logger.WithError(err).WithField("guild", s.GuildID).WithField("channel", s.SetupChannel).Error("failed sending setup message")
} else {
s.setupMessages = append(s.setupMessages, m.ID)
}
}
func (s *SetupSession) sendInitialMessage(data *dcmd.Data, msgf string, args ...interface{}) {
send := &discordgo.MessageSend{Content: "[RSVP Event Setup]: " + fmt.Sprintf(msgf, args...)}
msgs, err := data.SendFollowupMessage(send, discordgo.AllowedMentions{})
if err != nil {
logger.WithError(err).WithField("guild", s.GuildID).WithField("channel", s.SetupChannel).Error("failed sending setup message")
return
}
switch data.TriggerType {
case dcmd.TriggerTypeSlashCommands:
s.followupMessageID = msgs[0].ID
s.interactionToken = data.SlashCommandTriggerData.Interaction.Token
default:
s.setupMessages = append(s.setupMessages, msgs[0].ID)
}
}
const (
EmojiJoining = "✅"
EmojiMaybe = "❔"
EmojiNotJoining = "❌"
EmojiWaitlist = "🕐"
)
var EventReactionsWithLimit = []string{EmojiJoining, EmojiNotJoining, EmojiWaitlist, EmojiMaybe}
var EventReactionsNoLimit = []string{EmojiJoining, EmojiNotJoining, EmojiMaybe}
func AddReactions(channelID, messageID int64, isLimit bool) error {
list := EventReactionsNoLimit
if isLimit {
list = EventReactionsWithLimit
}
for _, r := range list {
err := common.BotSession.MessageReactionAdd(channelID, messageID, r)
if err != nil {
return err
}
}
return nil
}