/
main.go
91 lines (73 loc) · 2.28 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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/bwmarrin/discordgo"
"github.com/caromo/rinako/collections"
"github.com/jinzhu/gorm"
)
var allowedRoleTitles []string
var rinako Rinako
func main() {
var configFile, logFile string
flag.StringVar(&configFile, "c", "config.toml", "Configuration file")
flag.StringVar(&logFile, "l", "rinako.log.txt", "Log file")
flag.Parse()
if err := os.MkdirAll(filepath.Dir(logFile), 0770); err == nil {
if fi, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0660); err == nil {
log.SetOutput(fi)
} else {
fmt.Printf("Failed to open log file due to err %v\n", err)
}
} else {
fmt.Printf("Failed to create log file due to err %v\n", err)
}
config, err := ReadConfig(configFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to load config file %s: %v", configFile, err)
os.Exit(1)
}
db, err := InitializeDB(config.DBPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open DB %s: %v", config.DBPath, err)
os.Exit(1)
}
rinako.config = config
rinako.db = db
db.AutoMigrate(&collections.Server{})
db.AutoMigrate(&collections.User{})
//keep this in for "backwards compatibility"
for _, rd := range rinako.config.AllowedRoles {
allowedRoleTitles = append(allowedRoleTitles, rd.Role)
}
rinako.session, err = discordgo.New("Bot " + rinako.config.AuthToken)
// Register the messageCreate func as a callback for MessageCreate events.
// dg.AddHandler(roleMessageCreate)
rinako.session.AddHandler(messageCreate)
// In this example, we only care about receiving message events.
rinako.session.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsGuildMessages)
// Open a websocket connection to Discord and begin listening.
err = rinako.session.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
// Wait here until CTRL-C or other term signal is received.
fmt.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
// Cleanly close down the Discord session.
rinako.session.Close()
}
// Rinako represents an instance of Rinako
type Rinako struct {
session *discordgo.Session
config *Config
db *gorm.DB
}