-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
161 lines (133 loc) · 4.24 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
160
161
package main
import (
"database/sql"
"flag"
"fmt"
"log"
"os"
"os/signal"
_ "github.com/mattn/go-sqlite3"
"github.com/bwmarrin/discordgo"
"github.com/clbx/juicebot/cmd"
"github.com/clbx/juicebot/util"
)
var (
config util.JuiceBotConfig
db *sql.DB
s *discordgo.Session
GuildID = flag.String("guild", "", "Test guild ID. If not passed - bot registers commands globally")
BotToken = flag.String("token", "", "Bot access token")
RemoveCommands = flag.Bool("rmcmd", true, "Remove all commands after shutdowning or not")
ConfigPath = flag.String("config", "./config.yaml", "Path to the config file")
// integerOptionMinValue = 1.0
// dmPermission = false
// defaultMemberPermissions int64 = discordgo.PermissionManageServer
commands = []*discordgo.ApplicationCommand{
cmd.PingCommand,
cmd.DogCommand,
cmd.AddGameCommand,
cmd.RemoveGameCommand,
}
commandHandlers map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate)
)
func init() {
tokenEnv := os.Getenv("TOKEN")
if tokenEnv != "" {
log.Printf("Token loaded from Environment Variable")
BotToken = &tokenEnv
}
configEnv := os.Getenv("CONFIG")
if configEnv != "" {
log.Printf("Config path loaded from Environment Variable")
ConfigPath = &configEnv
}
flag.Parse()
var err error
s, err = discordgo.New("Bot " + *BotToken)
if err != nil {
log.Fatalf("Invalid bot parameters: %v", err)
}
config = *util.NewJuiceBotConfig(*ConfigPath)
if config.Debug {
fmt.Printf("%+v\n", config)
}
// Setup DB
db, err := sql.Open("sqlite3", config.DB.Path)
if err != nil {
log.Fatalf("Could not open sqlite db")
}
err = util.InitDB(db)
if err != nil {
log.Fatalf("%s", err)
}
//Add Commands here
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"ping": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
cmd.PingAction(s, i)
},
"dog": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
cmd.DogAction(s, i, &config) // Adjust 'DogAction' to accept JuiceBotConfig
},
"addgame": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
cmd.AddGameAction(s, i, &config, db)
},
"removegame": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
cmd.RemoveGameAction(s, i, &config, db)
},
}
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
})
// Add Handlers here.
// s.AddHandler(cmd.DogHandler)
s.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
cmd.DogHandler(s, m, &config)
})
s.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
cmd.CalloutHandler(s, m, &config)
})
}
func main() {
s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
})
err := s.Open()
if err != nil {
log.Fatalf("Cannot open the session: %v", err)
}
log.Println("Adding commands...")
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
for i, v := range commands {
cmd, err := s.ApplicationCommandCreate(s.State.User.ID, *GuildID, v)
if err != nil {
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
}
log.Printf("Added command: %v", v.Name)
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
if *RemoveCommands {
log.Println("Removing commands...")
// // We need to fetch the commands, since deleting requires the command ID.
// // We are doing this from the returned commands on line 375, because using
// // this will delete all the commands, which might not be desirable, so we
// // are deleting only the commands that we added.
// registeredCommands, err := s.ApplicationCommands(s.State.User.ID, *GuildID)
// if err != nil {
// log.Fatalf("Could not fetch registered commands: %v", err)
// }
for _, v := range registeredCommands {
err := s.ApplicationCommandDelete(s.State.User.ID, *GuildID, v.ID)
if err != nil {
log.Panicf("Cannot delete '%v' command: %v", v.Name, err)
}
}
}
log.Println("Gracefully shutting down.")
}