forked from HeroesAwaken/GoBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (81 loc) · 2.32 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
package main
import (
"flag"
"os"
"os/signal"
"runtime"
"syscall"
"github.com/HeroesAwaken/GoAwaken/Log"
"github.com/HeroesAwaken/GoAwaken/core"
"github.com/bwmarrin/discordgo"
_ "github.com/go-sql-driver/mysql"
)
func init() {
flag.StringVar(&configPath, "config", "config.yml", "Path to yml configuration file")
flag.StringVar(&logLevel, "logLevel", "error", "LogLevel [error|warning|note|debug]")
flag.Parse()
log.SetLevel(logLevel)
MyConfig.Load(configPath)
}
var (
configPath string
logLevel string
buffer = make([][]byte, 0)
// MyConfig Default configuration
MyConfig = Config{
MysqlServer: "127.0.0.1:5000",
MysqlUser: "",
MysqlDb: "",
MysqlPw: "",
}
Version = "0.0.1"
mem runtime.MemStats
AppName = "AwakenBot"
)
func main() {
var err error
if MyConfig.DiscordToken == "" {
log.Errorln("No token provided. Please run: airhorn -t <bot token>")
return
}
metricConnection := new(core.InfluxDB)
err = metricConnection.New(MyConfig.InfluxDBHost, MyConfig.InfluxDBDatabase, MyConfig.InfluxDBUser, MyConfig.InfluxDBPassword, AppName, Version)
if err != nil {
log.Fatalln("Error connecting to MetricsDB:", err)
}
if MyConfig.UseSshTunnel {
startRemoveCon(MyConfig.SshAddr, MyConfig.LocalAddr, MyConfig.RemoteAddr)
}
dbConnection := new(core.DB)
dbSQL, err := dbConnection.New(MyConfig.MysqlServer, MyConfig.MysqlDb, MyConfig.MysqlUser, MyConfig.MysqlPw)
if err != nil {
log.Fatalln("Error connecting to DB:", err)
}
err = dbSQL.Ping()
if err != nil {
log.Errorln("Error with database:", err.Error())
return
}
// Create a new Discord session using the provided bot token.
dg, err := discordgo.New("Bot " + MyConfig.DiscordToken)
if err != nil {
log.Errorln("Error creating Discord session:", err)
return
}
bot := NewAwakenBot(dbSQL, dg, metricConnection, "!ha")
// Open the websocket and begin listening.
err = bot.DG.Open()
if err != nil {
log.Errorln("Error opening Discord session:", err)
return
}
// Wait here until CTRL-C or other term signal is received.
log.Noteln("HeroesAwakenBot 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.
bot.DG.Close()
bot.GetUserRolesByDiscordID.Close()
dbSQL.Close()
}