-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
459 lines (421 loc) · 12.7 KB
/
main.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package main
import (
"flag"
"fmt"
"log"
"os/signal"
"time"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
"os"
"github.com/bwmarrin/discordgo"
)
// Bot parameters
var (
GuildID = flag.String("guild", "", "Test guild ID. If not passed - bot registers commands globally")
BotToken = flag.String("token", "", "Bot access token")
RemoveCommands = flag.Bool("rmcmd", true, "Remove all commands after shutdowning or not")
)
var s *discordgo.Session
type User struct {
Uuid string `gorm:"primaryKey"`
Token string `gorm:"unique"`
}
type BestFriend struct {
UserUuid string `gorm:"primaryKey"`
FriendUuid string `gorm:"primaryKey"`
}
func init() { flag.Parse() }
func init() {
if *BotToken == "" {
// get bot token from environment variable
*BotToken = os.Getenv("BOT_TOKEN")
}
if *GuildID == "" {
// get guild ID from environment variable
*GuildID = os.Getenv("GUILD_ID")
}
}
func init() {
var err error
s, err = discordgo.New("Bot " + *BotToken)
if err != nil {
log.Fatalln("Invalid bot parameters:", err)
}
}
var (
commands = []*discordgo.ApplicationCommand{
{
Name: "best_friend",
Description: "Modify your best friend list",
Options: []*discordgo.ApplicationCommandOption{
{
Name: "add",
Description: "Add a user to the best friends list to get notified when they join VC",
Type: discordgo.ApplicationCommandOptionSubCommand,
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionUser,
Name: "user-option",
Description: "User",
Required: true,
},
},
},
{
Name: "remove",
Description: "Remove a user from the best friends list",
Type: discordgo.ApplicationCommandOptionSubCommand,
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionUser,
Name: "user-option",
Description: "User",
Required: true,
},
},
},
{
Name: "list",
Type: discordgo.ApplicationCommandOptionSubCommand,
Description: "List best friends",
},
},
},
}
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"best_friend": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
// Access options in the order provided by the user.
global_options := i.ApplicationCommandData().Options
global_optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(global_options))
for _, opt := range global_options {
log.Println("option:", opt.Name, "type:", opt.Type, "value:", opt.Options)
global_optionMap[opt.Name] = opt
}
options := global_optionMap[global_options[0].Name].Options
optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(options))
for _, opt := range options {
log.Println("option:", opt.Name, "type:", opt.Type, "value:", opt.Options)
optionMap[opt.Name] = opt
}
// As you can see, names of subcommands (nested, top-level)
// and subcommand groups are provided through the arguments.
switch global_options[0].Name {
case "add":
var bestFriendId string
if opt, ok := optionMap["user-option"]; ok {
// get creator id
var userId string
if i.User != nil {
userId = i.User.ID
} else if i.Member != nil {
userId = i.Member.User.ID
} else {
log.Println("No user found???")
}
bestFriend := &BestFriend{
UserUuid: userId,
FriendUuid: opt.UserValue(nil).ID,
}
err := db.Create(bestFriend)
if err.Error != nil {
log.Println("Error creating best friend:", err.Error.Error())
if err.Error.Error() == "constraint failed: UNIQUE constraint failed: best_friends.user_uuid, best_friends.friend_uuid (1555)" {
var _ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "You already have that user in your best friends list.",
},
})
} else {
var _ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Error creating best friend",
},
})
}
return
}
bestFriendId = opt.UserValue(nil).ID
}
var _ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{
{
Title: "Addded best friend",
Description: fmt.Sprintf(
"Added <@%s> to your best friends list.",
bestFriendId,
),
},
},
},
})
case "remove":
var bestFriendId string
if opt, ok := optionMap["user-option"]; ok {
// get creator id
var userId string
if i.User != nil {
userId = i.User.ID
} else if i.Member != nil {
userId = i.Member.User.ID
} else {
log.Println("No user found???")
}
bestFriendId = opt.UserValue(nil).ID
bestFriend := &BestFriend{
UserUuid: userId,
FriendUuid: bestFriendId,
}
err := db.Delete(bestFriend)
if err.Error != nil {
log.Println("Error removing best friend:", err.Error)
var _ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Error removing best friend",
},
})
return
}
if err.RowsAffected == 0 {
var _ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "You don't have that user in your best friends list.",
},
})
return
}
}
var _ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{
{
Title: "Removed best friend",
Description: fmt.Sprintf(
"Removed <@%s> from your best friends list :(",
bestFriendId,
),
},
},
},
})
case "list":
var userId string
if i.User != nil {
userId = i.User.ID
} else if i.Member != nil {
userId = i.Member.User.ID
} else {
log.Println("No user found???")
}
var bestFriends []*BestFriend
err := db.Find(&bestFriends, BestFriend{UserUuid: userId}).Error
if err != nil {
log.Println("Error getting best friends:", err)
var _ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Error getting best friends",
},
})
return
}
if len(bestFriends) == 0 {
var _ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "You don't have any best friends. :'(",
},
})
return
}
var bestFriendsString string = "Your best friends are:\n"
for _, bestFriend := range bestFriends {
bestFriendsString += fmt.Sprintf("<@%s>\n", bestFriend.FriendUuid)
}
var _ = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{
{
Title: "Your best friends",
Description: bestFriendsString,
},
},
},
})
}
},
}
)
func init() {
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
})
}
var db *gorm.DB
func main() {
var err error
db, err = gorm.Open(sqlite.Open("database.db"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
// Migrate the schema
err = db.AutoMigrate(&User{})
if err != nil {
log.Fatal(err)
panic("failed to migrate schema")
}
err = db.AutoMigrate(&BestFriend{})
if err != nil {
log.Fatal(err)
panic("failed to migrate schema")
}
// Discord stuff
s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
})
err = s.Open()
if err != nil {
log.Fatalln("Cannot open the session:", err)
}
log.Println("Adding commands...")
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
for i, v := range commands {
log.Println("v:", v)
cmd, err := s.ApplicationCommandCreate(s.State.User.ID, *GuildID, v)
if err != nil {
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
}
registeredCommands[i] = cmd
}
// react to voice changes
s.AddHandler(func(s *discordgo.Session, v *discordgo.VoiceStateUpdate) {
if v.ChannelID == "" {
return
}
if v.BeforeUpdate != nil && v.BeforeUpdate.ChannelID != "" {
// user was already in a voice channel
return
}
log.Printf("%v joined voice channel %v", v.UserID, v.ChannelID)
// get user name
user, err := s.User(v.UserID)
if err != nil {
log.Printf("failed to get user: %v", err)
return
}
// get channel name
channel, err := s.State.Channel(v.ChannelID)
if err != nil {
log.Printf("failed to get channel: %v", err)
return
}
// get guild
guild, err := s.State.Guild(v.GuildID)
if err != nil {
log.Printf("failed to get guild: %v", err)
return
}
time.Sleep(time.Second * 60)
// check again, if user is in the same VC, then send dm
var userIsInVC bool
for _, member := range guild.VoiceStates {
if member.UserID == user.ID {
if member.ChannelID == v.ChannelID {
userIsInVC = true
}
break
}
}
if !userIsInVC {
return
}
log.Printf("%v is in voice channel %v", user.Username, channel.Name)
// get best friends
var bestFriends []BestFriend
if result := db.Find(&bestFriends, BestFriend{FriendUuid: v.UserID}); result.Error != nil {
log.Fatal(result.Error)
}
// send dm to best friends
outer:
for _, bestFriend := range bestFriends {
bestFriendUser, err := s.User(bestFriend.UserUuid)
if err != nil {
log.Printf("failed to get user: %v", err)
continue
}
// check if best friend is in a VC already
for _, guild := range s.State.Guilds {
for _, vs := range guild.VoiceStates {
if vs.UserID == bestFriendUser.ID {
continue outer
}
}
}
_, err = s.GuildMember(guild.ID, bestFriendUser.ID)
if err != nil {
log.Printf("best friend %v is not in the guild %v", bestFriendUser.Username, guild.Name)
continue
}
// create dm
dmChannel, err := s.UserChannelCreate(bestFriendUser.ID)
if err != nil {
log.Printf("failed to create channel: %v", err)
return
}
// get channel url
channelURL := fmt.Sprintf("https://discordapp.com/channels/%v/%v", guild.ID, channel.ID)
// send embed dm
msg, err := s.ChannelMessageSendEmbed(dmChannel.ID, &discordgo.MessageEmbed{
Title: guild.Name,
Description: fmt.Sprintf(
"%v joined %v",
user.Username,
channel.Name,
),
Author: &discordgo.MessageEmbedAuthor{
Name: user.Username,
IconURL: user.AvatarURL(""),
},
URL: channelURL,
})
if err != nil {
log.Printf("failed to send message: %v", msg)
log.Printf("Because: %v", err)
return
}
}
})
defer s.Close()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
log.Println("Press Ctrl+C to exit")
<-stop
if *RemoveCommands {
log.Println("Removing commands...")
// // We need to fetch the commands, since deleting requires the command ID.
// // We are doing this from the returned commands on line 375, because using
// // this will delete all the commands, which might not be desirable, so we
// // are deleting only the commands that we added.
registeredCommands, err := s.ApplicationCommands(s.State.User.ID, *GuildID)
if err != nil {
log.Fatalf("Could not fetch registered commands: %v", err)
}
for _, v := range registeredCommands {
err := s.ApplicationCommandDelete(s.State.User.ID, *GuildID, v.ID)
if err != nil {
log.Panicf("Cannot delete '%v' command: %v", v.Name, err)
}
}
}
log.Println("Gracefully shutting down.")
}