-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (109 loc) · 2.77 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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"benh.codes/mcytbot/commands"
"benh.codes/mcytbot/db"
"github.com/bwmarrin/discordgo"
"github.com/getsentry/sentry-go"
"github.com/joho/godotenv"
)
var guildId string
var botToken string
var s *discordgo.Session
func init() {
godotenv.Load()
botToken = os.Getenv("TOKEN")
guildId = os.Getenv("GUILD")
flag.Parse()
}
func init() {
var err error
s, err = discordgo.New("Bot " + botToken)
if err != nil {
log.Fatalf("Invalid bot parameters: %v", err)
}
}
var (
cmds = commands.GetCommands()
cmdHandlers = commands.GetCommandHandlers()
compHandlers = commands.GetComponentHandlers()
)
func init() {
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
switch i.Type {
case discordgo.InteractionApplicationCommand:
if h, ok := cmdHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
case discordgo.InteractionMessageComponent:
if h, ok := compHandlers[i.MessageComponentData().CustomID]; ok {
h(s, i)
}
}
})
}
func main() {
go func() {
db.Init()
err := sentry.Init(sentry.ClientOptions{
Dsn: "https://[email protected]/6056906",
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
}
s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Println("Bot is up!")
s.State.TrackMembers = true
guilds, _ := s.UserGuilds(100, "", "")
for _, g := range guilds {
fmt.Println("Loading in information for " + g.ID)
fullGuild, _ := s.Guild(g.ID)
s.State.GuildAdd(fullGuild)
var exists bool
if db.DB.QueryRow("SELECT exists (SELECT 1 from guilds WHERE id = $1)", g.ID).Scan(&exists); !exists {
_, err := db.DB.Exec(`INSERT INTO guilds (id) VALUES ($1)`, g.ID)
if err != nil {
fmt.Printf("Error inserting guild into database: %v\n", err)
}
}
}
})
err = s.Open()
if err != nil {
sentry.CaptureException(err)
log.Fatalf("Cannot open the session: %v", err)
}
for _, v := range cmds {
_, err := s.ApplicationCommandCreate(s.State.User.ID, guildId, v)
if err != nil {
sentry.CaptureException(err)
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
}
}
}()
shutdown := make(chan int)
//create a notification channel to shutdown
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigChan
fmt.Println("Shutting down...")
shutdown <- 1
}()
<-shutdown
s.Close()
db.DB.Close()
cmds, _ := s.ApplicationCommands(s.State.User.ID, guildId)
for _, a := range cmds {
err := s.ApplicationCommandDelete(s.State.User.ID, guildId, a.ID)
if err != nil {
sentry.CaptureException(err)
log.Printf("Cannot delete '%v' command: %v", a.Name, err)
}
}
}