-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
executable file
·88 lines (75 loc) · 2.19 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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"os/signal"
"github.com/bwmarrin/discordgo"
)
// BotConfig
// Bot configuration that will tell it how to connect to the Discord Application.
// The bot expects a `config.json` file with the following fields:
// bot_token, guild_id, remove_commands
type BotConfig struct {
GuildID string `json:"guild_id"`
BotToken string `json:"bot_token"`
RemoveCommands bool `json:"remove_commands"`
}
// GlobalConfig
// Global configuration struct
var GlobalConfig BotConfig
// Session pointer for our discord session
var session *discordgo.Session
// This function runs prior to the main function. Executes some basic setup stuff.
func init() {
// Read config file
content, err := ioutil.ReadFile("./config.json")
if err != nil {
log.Fatal("Error when opening file: ", err)
}
err = json.Unmarshal(content, &GlobalConfig)
if err != nil {
log.Fatal("Error during Unmarshal(): ", err)
}
session, err = discordgo.New("Bot " + GlobalConfig.BotToken)
if err != nil {
log.Fatalf("Invalid bot parameters: %v", err)
}
// Add handlers for different components (commands / message components)
session.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
switch i.Type {
case discordgo.InteractionApplicationCommand:
if handler, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
handler(s, i)
}
case discordgo.InteractionMessageComponent:
if h, ok := componentsHandlers[i.MessageComponentData().CustomID]; ok {
h(s, i)
}
}
})
}
func main() {
// Print status and open up the bot session
session.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Println("Bot is up!")
})
err := session.Open()
if err != nil {
log.Fatalf("Cannot open the session: %v", err)
}
// Register each application command
for _, v := range commands {
_, err := session.ApplicationCommandCreate(session.State.User.ID, GlobalConfig.GuildID, v)
if err != nil {
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
}
}
// End the session gracefully
defer session.Close()
stop := make(chan os.Signal)
signal.Notify(stop, os.Interrupt)
<-stop
log.Println("Gracefully shutting down")
}