-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
96 lines (81 loc) · 2.43 KB
/
main.go
File metadata and controls
96 lines (81 loc) · 2.43 KB
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
package main
import (
"log"
"os"
"os/signal"
"strings"
"syscall"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
)
func init() {
err := godotenv.Load()
if err != nil {
log.Print(err)
}
}
func main() {
Token := os.Getenv("TOKEN")
dg, err := discordgo.New("Bot " + Token)
if err != nil {
log.Panic("error creating Discord session,", err)
return
}
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsGuildMembers | discordgo.IntentsDirectMessages
dg.ShouldReconnectOnError = true
dg.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
})
err = dg.Open()
if err != nil {
log.Panic("error opening connection,", err)
return
}
defer dg.Close()
dg.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
switch i.Type {
case discordgo.InteractionApplicationCommand:
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
go h(s, i)
}
case discordgo.InteractionModalSubmit:
modalCommand := strings.Split(i.ModalSubmitData().CustomID, "-")
if h, ok := modalHandlers[modalCommand[0]]; ok {
go h(s, i)
}
case discordgo.InteractionMessageComponent:
if h, ok := componentHandlers[i.MessageComponentData().CustomID]; ok {
go h(s, i)
}
}
})
for _, guild := range dg.State.Guilds {
registerCommands := make([]*discordgo.ApplicationCommand, len(commands))
for i, command := range commands {
cmd, err := dg.ApplicationCommandCreate(dg.State.User.ID, guild.ID, command)
if err != nil {
log.Printf("could not create '%s' command: %v", command.Name, err)
}
registerCommands[i] = cmd
log.Printf("Created '%s' command", cmd.Name)
}
// delete commands that are not registered in commands.go
commands, err := dg.ApplicationCommands(dg.State.User.ID, guild.ID)
if err != nil {
log.Printf("could not get commands for guild %s: %v", guild.ID, err)
}
for _, command := range commands {
if _, ok := commandHandlers[command.Name]; !ok {
err := dg.ApplicationCommandDelete(dg.State.User.ID, guild.ID, command.ID)
if err != nil {
log.Printf("could not delete '%s' command: %v", command.Name, err)
}
}
}
}
log.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
defer log.Print("Bot is shutting down.")
}