-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
112 lines (92 loc) · 2.82 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
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/BitCrackers/BitBot/commands"
"github.com/BitCrackers/BitBot/config"
"github.com/BitCrackers/BitBot/database"
"github.com/BitCrackers/BitBot/modlog"
"github.com/BitCrackers/BitBot/responses"
"github.com/sirupsen/logrus"
"github.com/bwmarrin/discordgo"
)
func main() {
// Check for required environment variables.
token := os.Getenv("BITBOT_TOKEN")
if token == "" {
logrus.Fatalf("Couldn't read token from BITBOT_TOKEN environment variable")
}
cfg, err := config.Load()
if err != nil {
logrus.Fatalf("Unable to load config: %v", err)
}
if cfg.GuildID == "" {
panic("you were just saved from waiting 2 hours for discord to register slash commands globally")
}
if cfg.Debug {
logrus.SetLevel(logrus.DebugLevel)
}
session, err := discordgo.New("Bot " + token)
if err != nil {
logrus.Fatalf("Error while creating session: %v", err)
}
modlog, err := modlog.Create(&cfg, session)
if err != nil {
logrus.Fatalf("Unable to create modlog: %v", err)
}
db, err := database.New(session, &cfg, &modlog)
if err != nil {
logrus.Fatalf("Unable to start database: %v", err)
}
defer db.Close()
cmdHandler := commands.CommandHandler{
DB: db,
Config: &cfg,
ModLog: &modlog,
}
// Setup session intents here. GuildMembers is needed for moderation slash commands.
session.Identify.Intents = discordgo.IntentsAll
// Create custom response handler for filters
rh := responses.New()
// Register handlers for filters.
for _, filter := range cfg.Filters {
handler, err := newFilterHandler(&cfg, filter, &rh)
if err != nil {
logrus.Errorf("Error while creating filter: %v", err)
}
fmt.Printf("Added handler: %v.\n", handler)
session.AddHandler(handler)
}
session.AddHandler(aumLog)
if err = session.Open(); err != nil {
logrus.Fatalf("Error while opening session: %v", err)
}
cmds := cmdHandler.Commands()
// Contains all registered commands to avoid deleting commands where an error
// occurred during registration and running into a panic.
var registeredCmds []*commands.Command
for _, cmd := range cmds {
logrus.Infof("Registering slash command: %v", cmd.Name)
if err = cmd.Register(session, cfg.GuildID); err != nil {
logrus.Errorf("Error while registering %v command: %v", cmd.Name, err)
continue
}
registeredCmds = append(registeredCmds, cmd)
}
// Wait until a termination signal is received.
logrus.Infof("Bot is running successfully. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
// Clear slash commands.
for _, cmd := range registeredCmds {
logrus.Infof("Deleting slash command: %v", cmd.Name)
if err = cmd.Delete(); err != nil {
logrus.Errorf("Error while deleting command: %v", err)
}
}
// Gracefully exit.
session.Close()
}