-
Notifications
You must be signed in to change notification settings - Fork 0
/
BotClubeDoLivro.go
91 lines (73 loc) · 2 KB
/
BotClubeDoLivro.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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/BurntSushi/toml"
"github.com/Lukaesebrot/dgc"
"github.com/bwmarrin/discordgo"
"google.golang.org/api/books/v1"
"google.golang.org/api/option"
)
var ctx = context.Background()
var config tomlConfig
var bookService *books.Service
func main() {
loadConfig()
authGoogleBooksAPI()
discordSession := authDiscordAPI()
handleCommands(discordSession)
//Allow to terminate application with CTRL+C(SIGINT)
defer func() {
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
discordSession.Close()
}()
}
func loadConfig() {
fmt.Println("Loading config ")
if _, err := toml.DecodeFile("config.toml", &config); err != nil {
fmt.Println("There was an error while trying to load config. ", err)
return
}
}
func authGoogleBooksAPI() {
fmt.Println("Authenticating Google Books API. ")
var err error
bookService, err = books.NewService(ctx, option.WithAPIKey(config.Keys.GoogleToken))
if err != nil {
fmt.Println("Google doesn't want to authenticate us. Maybe something is wrong? ", err)
return
}
}
func authDiscordAPI() (discordSession *discordgo.Session) {
fmt.Println("Authenticating Discord. ")
discordSession, err := discordgo.New("Bot " + config.Keys.DiscordToken)
if err != nil {
fmt.Println("Yeah, it didn't work. Authentication at discord failed :/ ", err)
return
}
err = discordSession.Open()
if err != nil {
fmt.Println("There was an error while trying to open a Discord connection. ", err)
return
}
return discordSession
}
func handleCommands(discordSession *discordgo.Session) {
fmt.Println("Registering commands. ")
router := dgc.Create(&dgc.Router{
Prefixes: []string{config.Command.Prefix},
Commands: []*dgc.Command{},
})
router.RegisterCmd(&dgc.Command{
Name: config.Command.CommandName,
Aliases: config.Command.CommandAliases,
IgnoreCase: true,
Handler: bookCommandHandler,
})
router.Initialize(discordSession)
}