-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
175 lines (149 loc) · 4.37 KB
/
Copy pathmain.go
File metadata and controls
175 lines (149 loc) · 4.37 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"database/sql"
"fmt"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/7cav/cavbot2/utils"
_ "github.com/go-sql-driver/mysql"
"github.com/7cav/cavbot2/commands"
"github.com/bwmarrin/discordgo"
)
var Version = "dev"
var (
Token string
GuildID string
LogLevel string
BMToken string
)
func init() {
Token = os.Getenv("DISCORD_TOKEN")
GuildID = os.Getenv("GUILD_ID")
LogLevel = os.Getenv("LOG_LEVEL")
BMToken = os.Getenv("BM_TOKEN")
if Token == "" {
panic("No token provided. Please set DISCORD_TOKEN environment variable")
}
if GuildID == "" {
panic("No GuildID provided. Please set GUILD_ID environment variable")
}
if BMToken == "" {
panic("No BM_TOKEN provided. Please set BM_TOKEN environment variable")
}
if LogLevel == "" {
LogLevel = "default"
}
utils.InitLogger(LogLevel)
}
func initLOACache() {
dsn := os.Getenv("FORUM_DB_DSN")
if dsn == "" {
utils.Warn("FORUM_DB_DSN not set, LOA cache disabled")
return
}
nodeIDs := []int{180}
if s := os.Getenv("LOA_NODE_IDS"); s != "" {
nodeIDs = nil
for _, part := range strings.Split(s, ",") {
if id, err := strconv.Atoi(strings.TrimSpace(part)); err == nil {
nodeIDs = append(nodeIDs, id)
}
}
}
db, err := sql.Open("mysql", dsn)
if err != nil {
utils.Warn("Failed to open forum DB connection, LOA cache disabled", "error", err)
return
}
db.SetMaxOpenConns(2)
db.SetConnMaxIdleTime(30 * time.Second)
utils.GlobalLOACache.Refresh(db, nodeIDs)
go func() {
defer utils.RecoverPanic("loa-refresh")
ticker := time.NewTicker(15 * time.Minute)
defer ticker.Stop()
for range ticker.C {
utils.GlobalLOACache.Refresh(db, nodeIDs)
}
}()
}
func main() {
defer utils.InitSentry(Version)()
utils.Info("CavBot2 starting", "version", Version)
initLOACache()
dg, err := discordgo.New("Bot " + Token)
if err != nil {
panic(fmt.Sprintf("Error creating Discord session: %v", err))
}
// IntentsGuildMembers is a Privileged Gateway Intent — must be toggled on
// in the Discord Developer Portal for this bot application, otherwise
// dg.Open() fails at runtime with no compile-time signal.
dg.Identify.Intents = discordgo.IntentsAllWithoutPrivileged | discordgo.IntentsGuildMembers
registry := commands.NewRegistry()
dg.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
defer utils.RecoverPanic("interaction-handler")
switch i.Type {
case discordgo.InteractionApplicationCommand:
if h, ok := registry.GetHandler(i.ApplicationCommandData().Name); ok {
h(s, i)
}
case discordgo.InteractionMessageComponent:
customID := i.MessageComponentData().CustomID
parts := strings.Split(customID, "::")
if len(parts) > 0 {
if h, ok := registry.GetHandler(parts[0]); ok {
h(s, i)
}
}
}
})
err = dg.Open()
if err != nil {
panic(fmt.Sprintf("Error opening connection: %v", err))
}
defer func() {
err := dg.Close()
if err != nil {
panic(fmt.Sprintf("Error closing Discord connection: %v", err))
}
}()
registeredCommandNames := make(map[string]struct{}, len(registry.GetCommands()))
for _, cmd := range registry.GetCommands() {
registeredCommandNames[cmd.Name] = struct{}{}
}
utils.Info("Removing deprecated commands")
existingCommands, err := dg.ApplicationCommands(dg.State.User.ID, GuildID)
if err != nil {
utils.Warn("Warning: Could not fetch existing commands:", "error", err)
} else {
for _, cmd := range existingCommands {
if _, exists := registeredCommandNames[cmd.Name]; !exists {
err := dg.ApplicationCommandDelete(dg.State.User.ID, GuildID, cmd.ID)
if err != nil {
utils.Warn("Warning: Could not delete deprecated command", "command", cmd.Name, "error", err)
} else {
utils.Info("Removed deprecated command", "command", cmd.Name)
}
}
}
}
utils.Info("Registering commands")
registeredCommands := make([]*discordgo.ApplicationCommand, len(registry.GetCommands()))
for i, cmd := range registry.GetCommands() {
rcmd, err := dg.ApplicationCommandCreate(dg.State.User.ID, GuildID, cmd)
if err != nil {
panic(fmt.Sprintf("Cannot create command %v: %v", cmd.Name, err))
}
registeredCommands[i] = rcmd
}
commands.StartJoinerReportScheduler(dg, GuildID)
utils.Info("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
utils.Info("Shutting down")
}