-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
192 lines (172 loc) · 5.97 KB
/
app.js
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
// config data is setup here
// added abstraction
var fs = require("fs");
var util = require("util");
// load the config file if exists, write a template and promt user to quit if it doesn't
var settings = {
web: {},
steam: {}
};
var sampleConfig = {
"botname": "Some Name",
"commandChar": ".",
"commands": {
"host": "canKick",
"join": "canKick",
"test": "canKick"
},
"commands_dota": {
"start": "",
"exit": ""
},
"region": 2,
"secret": "",
"chatID": "103582791434793082",
"web": {
"mongodb": "mongo://localhost/dota/",
"redisdb": "redis://127.0.0.1:6379/0",
"enabled": "true",
"baseurl": "http://localhost:5000/",
"port": 80,
"league_name": "My First League",
"web_style": "default"
},
"steam": {
"account_name": "",
"password": "",
"auth_code": ""
},
"dota_bots": {
"1": {
"account_name":"herp",
"password":"derp",
"auth_code":"herp"
}
}
};
try {
var _config = fs.readFileSync("config.json", "utf-8");
if (_config.length) {
var config = JSON.parse(_config);
settings.botname = config.botname || "My Bot";
settings.commandChar = config.commandChar || ".";
settings.commands = config.commands || {"host": "canKick","join": "canKick","test": "canKick"};
settings.commands_dota = config.commands_dota || {"start": "","exit": ""};
settings.region = config.region || 2;
settings.chatID = config.chatID || "103582791434793082";
settings.web.enabled = config.web.enabled || "true";
settings.steam.account_name = config.steam.account_name || "derp";
settings.steam.password = config.steam.password || "herp";
settings.dota_bots = config.dota_bots || {"1":{"account_name":"herp","password":"derp","auth_code":"herp"}};
settings.secret = config.secret || "";
if (config.steam.auth_code !== "") {
settings.steam.auth_code = config.steam.auth_code;
}
for (var account in settings.dota_bots) {
if (settings.dota_bots[account].auth_code !== "") {
settings.dota_bots[account].auth_code = config.dota_bots[account].auth_code;
}
}
try {
var sentry = fs.readFileSync('sentry');
if (sentry.length) {
settings.steam.sha_sentryfile = sentry;
}
for (var account in settings.dota_bots) {
var sentry = fs.readFileSync('sentry_' + settings.dota_bots[account])
if (sentry.length) {
settings.dota_bots[account].sha_sentryfile = sentry;
}
}
} catch (e) {
console.log("No sentry provided");
}
if (settings.web.enabled === "true") {
settings.web.mongodb = config.web.mongodb;
settings.web.redisdb = config.web.redisdb;
settings.web.apikey = config.web.apikey;
settings.web.baseurl = config.web.baseurl;
settings.web.port = config.web.port;
settings.web.league_name = config.web.league_name;
settings.web.style = config.web.style;
}
} else {
console.log("config file empty, generating new and exiting");
fs.writeFile('config.json', JSON.stringify(sampleConfig, null, 2), function(e) {
if (e) {
console.log(e);
process.exit(1);
} else {
console.log("config file generated in root directory (./config.json)");
process.exit(1);
}
});
}
} catch (err) {
if (err.code !== "ENOENT") {
throw err;
process.exit(1);
}
console.log("config file not found, generating and exiting");
fs.writeFile('config.json', JSON.stringify(sampleConfig, null, 2), function(e) {
if (e) {
console.log(err);
process.exit(1);
} else {
console.log("config file generated in root directory (./config.json)");
process.exit(1);
}
});
}
var permissions = {
"canKick": 16,
"canTalk": 8,
"canBan": 256,
"isOwner": 512
};
// prepend the fantasy character (command character) to the keys in settings.commands and add proper permissions
for (var key of Object.keys(settings.commands)) {
for (var perm of Object.keys(permissions)) {
if (settings.commands[key] == perm) {
settings.commands[key] = permissions[perm];
}
}
settings.commands[settings.commandChar + key] = settings.commands[key];
delete(settings.commands[key]);
}
// prepend the fantasy character (command character) to the keys in settings.commands_dota and add proper permissions
for (var key of Object.keys(settings.commands_dota)) {
for (var perm of Object.keys(permissions)) {
if (settings.commands_dota[key] == perm) {
settings.commands_dota[key] = permissions[perm];
}
}
settings.commands_dota[settings.commandChar + key] = settings.commands_dota[key];
delete(settings.commands_dota[key]);
}
util.log(settings.commands_dota);
if (process.platform === "win32") {
var rl = require("readline").createInterface({
input: process.stdin,
output: process.stdout
});
rl.on("SIGINT", function () {
process.emit("SIGINT");
});
}
process.on("SIGINT", function () {
//graceful shutdown
process.exit();
});
// for use with the actual steam bot
module.exports.logOnDetails = settings.steam;
module.exports.botname = settings.botname;
module.exports.commands = settings.commands;
module.exports.commands_dota = settings.commands_dota;
module.exports.commandChar = settings.commandChar;
module.exports.chatID = settings.chatID;
module.exports.region = settings.region;
// account information for the dota bots
module.exports.dota_bots = settings.dota_bots;
// for use with the webserver part
module.exports.web_settings = settings.web;