-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (117 loc) · 3.37 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
package main
import (
"flag"
"kodachi/bot/commands"
kodachiEvents "kodachi/bot/events"
"kodachi/bot/handlers"
"kodachi/bot/models"
kodachiTasks "kodachi/bot/tasks"
"log"
"os"
"os/signal"
"time"
"github.com/bwmarrin/discordgo"
"github.com/go-co-op/gocron"
"github.com/joho/godotenv"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
var (
REGISTER_COMMANDS = flag.Bool("register-commands", true, "True by default (useful in development)")
TESTING = flag.Bool("testing", false, "")
)
var s *discordgo.Session
var db *gorm.DB
// Parse CLI Arguments
func init() { flag.Parse() }
// Initiate discord session
func init() {
// Load .env only if --testing=true
if *TESTING {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
}
// Load BotToken
BotToken := os.Getenv("BOT_TOKEN")
var err error
s, err = discordgo.New("Bot " + BotToken)
if err != nil {
log.Fatalf("Invalid bot parameters: %v", err)
}
s.Identify.Intents |= discordgo.IntentGuildMembers
s.Identify.Intents |= discordgo.IntentGuildWebhooks
s.Identify.Intents |= discordgo.IntentMessageContent
}
// Initiate database connection
func init() {
var err error
db, err = gorm.Open(postgres.Open(os.Getenv("POSTGRES_DSN")), &gorm.Config{})
if err != nil {
log.Fatalf("Could not connect to database: %v", err)
}
if !db.Migrator().HasTable(&models.Config{}) {
db.Migrator().CreateTable(&models.Config{})
}
if !db.Migrator().HasColumn(&models.Config{}, "welcome_channel_id") {
db.Migrator().AddColumn(&models.Config{}, "welcome_channel_id")
}
if !db.Migrator().HasTable(&models.Birthday{}) {
db.Migrator().CreateTable(&models.Birthday{})
}
if !db.Migrator().HasTable(&models.TreeMember{}) {
db.Migrator().CreateTable(&models.TreeMember{})
}
}
// Add Handlers
func init() {
s.AddHandler(handlers.InteractionCreateHandler(db))
s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
})
s.AddHandler(kodachiEvents.WelcomeMessageEventHandler(db))
}
// Create websocket connection to Discord
func init() {
err := s.Open()
if err != nil {
log.Fatalf("Cannot open the session: %v", err)
}
}
// Add Tasks
func init() {
scheduler := gocron.NewScheduler(time.UTC)
scheduler.Every(1).Day().At("00:00").Do(kodachiTasks.BirthdayCheck(db, s))
scheduler.StartAsync()
}
func main() {
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands.Commands))
guildId := "" // Empty to register global commands
if *REGISTER_COMMANDS {
log.Println("Adding commands...")
for i, command := range commands.Commands {
cmd, err := s.ApplicationCommandCreate(s.State.User.ID, guildId, command)
if err != nil {
log.Panicf("Cannot create '%v' command: %v", command.Name, err)
}
registeredCommands[i] = cmd
}
}
defer s.Close()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
log.Println("Press Ctrl+C to exit")
<-stop
CLEAN_COMMANDS_AFTER_SHUTDOWN := os.Getenv("CLEAN_COMMANDS_AFTER_SHUTDOWN")
if CLEAN_COMMANDS_AFTER_SHUTDOWN == "true" {
log.Println("Removing commands...")
for _, command := range registeredCommands {
err := s.ApplicationCommandDelete(s.State.User.ID, guildId, command.ID)
if err != nil {
log.Panicf("Cannot delete '%v' command: %v", command.Name, err)
}
}
}
log.Println("Gracefully shutting down.")
}