-
Notifications
You must be signed in to change notification settings - Fork 3
/
birthday.go
354 lines (303 loc) · 10.3 KB
/
birthday.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
package slash
import (
"fmt"
"time"
"github.com/bwmarrin/discordgo"
"github.com/ritsec/ops-bot-iii/commands/slash/permission"
"github.com/ritsec/ops-bot-iii/data"
"github.com/ritsec/ops-bot-iii/logging"
"github.com/sirupsen/logrus"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)
func Birthday() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *discordgo.InteractionCreate)) {
minValue := float64(1)
return &discordgo.ApplicationCommand{
Name: "birthday",
Description: "Add or edit birthday",
DefaultMemberPermissions: &permission.Member,
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionSubCommand,
Name: "delete",
Description: "Delete a birthday",
},
{
Type: discordgo.ApplicationCommandOptionSubCommand,
Name: "add",
Description: "Add a birthday",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionInteger,
Name: "month",
Description: "The month of your birthday",
Required: true,
Choices: []*discordgo.ApplicationCommandOptionChoice{
{
Name: "January",
Value: 1,
},
{
Name: "February",
Value: 2,
},
{
Name: "March",
Value: 3,
},
{
Name: "April",
Value: 4,
},
{
Name: "May",
Value: 5,
},
{
Name: "June",
Value: 6,
},
{
Name: "July",
Value: 7,
},
{
Name: "August",
Value: 8,
},
{
Name: "September",
Value: 9,
},
{
Name: "October",
Value: 10,
},
{
Name: "November",
Value: 11,
},
{
Name: "December",
Value: 12,
},
},
},
{
Type: discordgo.ApplicationCommandOptionInteger,
Name: "day",
Description: "The day of your birthday",
Required: true,
MinValue: &minValue,
MaxValue: 31,
},
},
},
},
},
func(s *discordgo.Session, i *discordgo.InteractionCreate) {
span := tracer.StartSpan(
"commands.slash.birthday:Birthday",
tracer.ResourceName("/birthday"),
)
defer span.Finish()
switch i.ApplicationCommandData().Options[0].Name {
case "delete":
birthdayDelete(s, i, span.Context())
case "add":
birthdayAdd(s, i, span.Context())
}
}
}
// birthdayDelete deletes a birthday from the database
func birthdayDelete(s *discordgo.Session, i *discordgo.InteractionCreate, ctx ddtrace.SpanContext) {
span := tracer.StartSpan(
"commands.slash.birthday:BirthdayDelete",
tracer.ResourceName("/birthday delete"),
tracer.ChildOf(ctx),
)
defer span.Finish()
// check if birthday exists
exists, err := data.Birthday.Exists(i.Member.User.ID, span.Context())
if err != nil {
logging.Error(s, "Birthday has been removed", i.Member.User, span)
return
}
if exists {
// birthday exists, remove it
tx, err := time.LoadLocation("America/New_York")
if err != nil {
logging.Error(s, "encounted error when loading timezone", i.Member.User, span, logrus.Fields{"err": err.Error()})
return
}
entBirthday, err := data.Birthday.Get(i.Member.User.ID, span.Context())
if err != nil {
logging.Error(s, "encounted error when getting birthday from user", i.Member.User, span, logrus.Fields{"err": err.Error()})
return
}
// check if birthday is today
now := time.Now().In(tx)
if int(entBirthday.Month) == int(now.Month()) && int(entBirthday.Day) == int(now.Day()) {
// respond to user that birthday cannot be today
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "You cannot remove your birthday today",
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
logging.Error(s, "encounted error when responding to user", i.Member.User, span, logrus.Fields{"err": err.Error()})
}
return
}
// remove birthday
_, err = data.Birthday.Delete(i.Member.User.ID, span.Context())
if err != nil {
// respond to user that error occured when removing birthday
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Error occured when removing birthday",
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
logging.Error(s, "encounted error when responding to user", i.Member.User, span, logrus.Fields{"err": err.Error()})
}
return
}
logging.Debug(s, "Birthday has been removed", i.Member.User, span)
// respond to user that birthday has been removed
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Birthday has been removed",
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
logging.Error(s, "encounted error when responding to user", i.Member.User, span, logrus.Fields{"err": err.Error()})
return
}
} else {
// birthday does not exist
// respond to user that birthday does not exist
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Birthday does not exist",
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
logging.Error(s, "encounted error when responding to user", i.Member.User, span, logrus.Fields{"err": err.Error()})
return
}
}
}
// birthdayAdd is subcommand of birthday that adds a birthday to the database
func birthdayAdd(s *discordgo.Session, i *discordgo.InteractionCreate, ctx ddtrace.SpanContext) {
span := tracer.StartSpan(
"commands.slash.birthday:BirthdayAdd",
tracer.ResourceName("/birthday add"),
tracer.ChildOf(ctx),
)
defer span.Finish()
// get user parameters
month := int(i.ApplicationCommandData().Options[0].Options[0].IntValue())
day := int(i.ApplicationCommandData().Options[0].Options[1].IntValue())
tz, err := time.LoadLocation("America/New_York")
if err != nil {
logging.Error(s, "encounted error when loading timezone", i.Member.User, span, logrus.Fields{"err": err.Error()})
return
}
// check if birthday is today
now := time.Now().In(tz)
if month == int(now.Month()) && day == int(now.Day()) {
// respond to user that birthday cannot be today
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "You cannot set your birthday to be today",
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
logging.Error(s, "encounted error when responding to user", i.Member.User, span, logrus.Fields{"err": err.Error()})
}
return
}
// check is birthday already exists
exists, err := data.Birthday.Exists(i.Member.User.ID, span.Context())
if err != nil {
logging.Error(s, "encounted error when checking if birthday exists", i.Member.User, span, logrus.Fields{"err": err.Error()})
return
}
if exists {
// birthday exists, update existing one
// get current birthday
entBirthday, err := data.Birthday.Get(i.Member.User.ID, span.Context())
if err != nil {
logging.Error(s, "encounted error when getting birthday from user", i.Member.User, span, logrus.Fields{"err": err.Error()})
return
}
// check if birthday is today
if int(entBirthday.Month) == int(now.Month()) && int(entBirthday.Day) == int(now.Day()) {
// respond to user that birthday cannot be today
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "You cannot change your birthday today",
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
logging.Error(s, "encounted error when responding to user", i.Member.User, span, logrus.Fields{"err": err.Error()})
}
return
}
// update birthday
_, err = entBirthday.Update().SetDay(day).SetMonth(month).Save(data.Ctx)
if err != nil {
logging.Error(s, "encounted error when updating birthday", i.Member.User, span, logrus.Fields{"err": err.Error()})
return
}
// log that birthday has been updated
logging.Debug(s, "Updated birthday for "+i.Member.User.Username, i.Member.User, span)
// send user message that birthday was successfully updated
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("Changed birtday to %d/%d", month, day),
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
logging.Error(s, "encounted error when responding to user", i.Member.User, span, logrus.Fields{"err": err.Error()})
return
}
} else {
// birthday does not exist create new one
// create new birthday for user
_, err = data.Birthday.Create(i.Member.User.ID, day, month, span.Context())
if err != nil {
logging.Error(s, "encounted error when creating birthday", i.Member.User, span, logrus.Fields{"err": err.Error()})
return
}
// log that birthday has been updated
logging.Debug(s, "Created birthday for "+i.Member.User.Username, i.Member.User, span)
// send user message that birthday was successfully updated
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Created birthday for " + i.Member.User.Username + " ",
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
logging.Error(s, "encounted error when responding to user", i.Member.User, span, logrus.Fields{"err": err.Error()})
return
}
}
}