-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
159 lines (137 loc) · 3.57 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
153
154
155
156
157
158
159
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/diamondburned/arikawa/v3/api"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/gateway"
"github.com/diamondburned/arikawa/v3/state"
_ "github.com/mattn/go-sqlite3"
)
func init() {
var err error
db, err = InitDB()
if err != nil {
log.Fatalln(err)
}
}
func main() {
appID := discord.AppID(mustSnowflakeEnv("APP_ID"))
token := mustEnv("DISCORD_TOKEN")
// setup bot
s := state.New("Bot " + token)
s.AddIntents(gateway.IntentGuilds)
s.AddHandler(MakeCommandHandlers(s, commandsGlobal))
// executed when either you join a guild or when the bot starts
// https://discord.com/developers/docs/topics/gateway#guilds
s.AddHandler(func(e *gateway.GuildCreateEvent) {
// TODO add a better option for whether to clobber old commands
guild := e.Guild.ID
if _, ok := os.LookupEnv("CLOBBER_CMDS"); ok {
cmds, err := s.Commands(appID)
if err != nil {
log.Println("error getting commands:", err)
}
removeCommands(s, guild, cmds)
}
registerCommands(s, appID, guild)
})
if err := s.Open(context.Background()); err != nil {
log.Fatalln("failed to open:", err)
}
defer s.Close()
// setup cleanup channel for ctrl+c
// closing this unblocks
cleanup := make(chan struct{})
// make this so all background goroutines can finish cleaning up
cleanupWaitGroup := &sync.WaitGroup{}
go func() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
<-sig
close(cleanup)
}()
// setup ticker for cleaning tokens
ticker := time.NewTicker(5 * time.Minute)
cleanupWaitGroup.Add(1)
go func() {
for {
select {
case <-cleanup:
db.CleanupTokens()
cleanupWaitGroup.Done()
return
case <-ticker.C:
db.CleanupTokens()
}
}
}()
// block until ctrl+c or kill
log.Println("bot is running")
<-cleanup
log.Println("cleaning up")
ticker.Stop()
// cleanupWaitGroup.Add(1)
// go func() {
// // remove commands from guilds so people don't try and use the bot while it's down
// cleanupCommands(s, activeCommands)
// cleanupWaitGroup.Done()
// }()
cleanupWaitGroup.Add(1)
go func() {
err := db.db.Close()
if err != nil {
log.Println("error closing db:", err)
}
cleanupWaitGroup.Done()
}()
cleanupWaitGroup.Wait()
log.Println("exiting")
}
func registerCommands(s *state.State, appID discord.AppID, guildID discord.GuildID) error {
// extract command definitions from command global variable
definitions := make([]api.CreateCommandData, 0, len(commandsGlobal))
for _, v := range commandsGlobal {
definitions = append(definitions, v.Data)
}
// register command definitions with discord
for _, command := range definitions {
_, err := s.CreateGuildCommand(appID, guildID, command)
if err != nil {
return fmt.Errorf("failed to create guild command: %w", err)
}
}
return nil
}
func removeCommands(s *state.State, guild discord.GuildID, activeCommands []discord.Command) {
for _, cmd := range activeCommands {
// only remove commands from current guild
if cmd.GuildID != guild {
continue
}
err := s.DeleteGuildCommand(cmd.AppID, guild, cmd.ID)
if err != nil {
log.Println("couldn't delete command", cmd.Name, "with id", cmd)
}
}
}
func mustSnowflakeEnv(env string) discord.Snowflake {
s, err := discord.ParseSnowflake(os.Getenv(env))
if err != nil {
log.Fatalf("Invalid snowflake for $%s: %v", env, err)
}
return s
}
func mustEnv(name string) string {
s := os.Getenv(name)
if s == "" {
log.Fatalln("No environment variable named", name)
}
return s
}