-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
192 lines (167 loc) · 3.49 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"database/sql"
"github.com/Neirpyc/dota2api"
"github.com/bwmarrin/discordgo"
_ "github.com/go-sql-driver/mysql"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"math/rand"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
var (
L *log.Logger
//apis
D dota2api.Dota2
DB *sql.DB
DG *discordgo.Session
//data
Heroes dota2api.Heroes
Items dota2api.Items
Config config
)
type config struct {
Token string `yaml:"Token"`
MariaDb string `yaml:"MariaDb"`
RemoveImages bool `yaml:"RemoveImages"`
ForceReload bool `yaml:"ForceReload"`
}
func init() {
//create a new logger
L = log.New(os.Stdout, "", log.Ldate|log.Lmicroseconds|log.Ltime|log.Llongfile)
//parse the configuration file
configByte, err := ioutil.ReadFile("config.yaml")
if err != nil {
L.Fatal(err)
}
err = yaml.Unmarshal(configByte, &Config)
if err != nil {
L.Fatal(err)
}
for _, path := range []string{"assets/tmp/", "assets/heroes/full/", "assets/heroes/lg/",
"assets/heroes/vert/", "assets/heroes/sb/", "assets/items/lg"} {
if err := os.MkdirAll(path, 0775); err != nil {
if os.IsNotExist(err) {
L.Fatal(err)
}
}
}
var wg sync.WaitGroup
wg.Add(4)
//seed rand
go func(wg *sync.WaitGroup) {
rand.Seed(time.Now().Unix())
wg.Done()
}(&wg)
//connect to the dota api
go func(wg *sync.WaitGroup) {
var err error
D, err = dota2api.LoadConfigFromFile("config.yaml")
if err != nil {
L.Fatal(err)
}
//download images of heroes and items
var wg0 sync.WaitGroup
wg0.Add(2)
go func(wg *sync.WaitGroup) {
Heroes, err = D.GetHeroes()
if err != nil {
L.Fatal(err)
}
createHeroesImagesList()
wg.Done()
}(&wg0)
go func(wg *sync.WaitGroup) {
Items, err = D.GetItems()
if err != nil {
L.Fatal(err)
}
createItemsImagesList()
wg.Done()
}(&wg0)
wg0.Wait()
wg.Done()
}(&wg)
//connect to database
go func(wg *sync.WaitGroup) {
var err error
DB, err = sql.Open("mysql", Config.MariaDb)
if err != nil {
L.Fatal(err)
}
_, err = DB.Query("USE dota_bot_discord")
if err != nil {
L.Println("Cannot find the 'dota_bot_discord' database: it will be created.")
rows, err := DB.Query("CREATE DATABASE dota_bot_discord")
if err != nil {
L.Fatal(err)
}
err = rows.Close()
if err != nil {
L.Fatal(err)
}
L.Println("Created the 'dota_bot_discord' database successfully!")
_, err = DB.Exec("USE dota_bot_discord")
if err != nil {
L.Fatal(err)
}
_, err = DB.Exec(`create table steam_id
(
discord_id BIGINT PRIMARY KEY not null,
steam_id BIGINT not null
);
`)
if err != nil {
L.Fatal(err)
}
_, err = DB.Exec(`create unique index steam_ide_discord_id_uindex
on steam_id (discord_id);`)
if err != nil {
L.Fatal(err)
}
}
wg.Done()
}(&wg)
//open discord API
go func(wg *sync.WaitGroup) {
var err error
DG, err = discordgo.New("Bot " + Config.Token)
if err != nil {
L.Fatal(err)
}
wg.Done()
}(&wg)
wg.Wait()
}
func main() {
DG.AddHandler(HandleMessage)
err := DG.Open()
if err != nil {
L.Fatal(err)
}
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
if err = os.RemoveAll("assets/tmp/"); err != nil {
L.Println(err)
}
if Config.RemoveImages {
if err = os.RemoveAll("assets/heroes/"); err != nil {
L.Println(err)
}
}
if Config.RemoveImages {
if err = os.RemoveAll("assets/items/"); err != nil {
L.Println(err)
}
}
err = DG.Close()
if err != nil {
L.Fatal(err)
}
}