This repository has been archived by the owner on Jan 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
128 lines (110 loc) · 2.55 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
package main
import (
"errors"
"flag"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
"gorm.io/gorm"
"log"
"os"
"os/signal"
)
var s *discordgo.Session
var database *gorm.DB
func init() { flag.Parse() }
func exists(path string) bool {
_, err := os.Stat(path)
return !errors.Is(err, os.ErrNotExist)
}
func init() {
if exists(".env") {
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
}
}
var err error
token := os.Getenv("CHEST_BOT_TOKEN")
if token == "" {
log.Fatal("CHEST_BOT_TOKEN not set")
}
s, err = discordgo.New("Bot " + token)
database = buildDatabase()
if err != nil {
log.Fatalf("Invalid bot parameters: %v", err)
}
}
var (
GuildID = ""
Cleanup = flag.Bool("cleanup", true, "Cleanup of commands")
)
func commands() []*discordgo.ApplicationCommand {
var commands []*discordgo.ApplicationCommand
commands = append(commands, adminCommands...)
commands = append(commands, funCommands...)
commands = append(commands, economyCommands...)
commands = append(commands, skillCommands...)
commands = append(commands, teamCommands...)
return commands
}
func init() {
ms := []map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
adminCommandHandlers,
economyCommandHandlers,
funCommandHandlers,
skillCommandHandlers,
teamCommandHandlers,
}
res := map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){}
for _, m := range ms {
for k, v := range m {
res[k] = v
}
}
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := res[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
})
}
func Setup() {
SetupCore()
SetupTeam()
SetupEconomy()
SetupSkill()
}
func SetupWeb() {
}
func main() {
s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Println("Bot is up!")
})
err := s.Open()
if err != nil {
log.Fatalf("Cannot open the session: %v", err)
}
Setup()
commands := commands()
for _, v := range commands {
_, err := s.ApplicationCommandCreate(s.State.User.ID, GuildID, v)
if err != nil {
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
}
}
defer s.Close()
stop := make(chan os.Signal)
signal.Notify(stop, os.Interrupt)
<-stop
log.Println("Gracefully shutting down")
if !*Cleanup {
return
}
cmds, err := s.ApplicationCommands(s.State.User.ID, GuildID)
for cmdId := range cmds {
cmd := cmds[cmdId]
err := s.ApplicationCommandDelete(s.State.User.ID, GuildID, cmd.ID)
if err != nil {
log.Fatalf("Cannot delete slash command %q: %v", cmd.Name, err)
}
}
}