forked from TalkTakesTime/Pokemon-Showdown-Bot
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.js
225 lines (184 loc) · 6.08 KB
/
main.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"use strict";
/**
* This is the main file for the Pokemon Showdown bot.
*
* Modified by DaWoblefet for use with BoTTT III with original work by TalkTakesTime, Quinella, and Morfent.
*
* Some parts of this code are taken from the Pokémon Showdown server code, so credits also go to Guangcong Luo and other Pokémon Showdown contributors.
* https://github.com/smogon/pokemon-showdown
*
* @license MIT license
*/
// Logs various bits of information to the console based on config settings.
global.info = function(text) {
if (config.debuglevel > 3) return;
if (!colors) global.colors = require("colors");
console.log("info".cyan + " " + text);
};
global.debug = function(text) {
if (config.debuglevel > 2) return;
if (!colors) global.colors = require("colors");
console.log("debug".blue + " " + text);
};
global.recv = function(text) {
if (config.debuglevel > 0) return;
if (!colors) global.colors = require("colors");
console.log("recv".grey + " " + text);
};
global.cmdr = function(text) { // Receiving commands
if (config.debuglevel !== 1) return;
if (!colors) global.colors = require("colors");
console.log("cmdr".grey + " " + text);
};
global.dsend = function(text) {
if (config.debuglevel > 1) return;
if (!colors) global.colors = require("colors");
console.log("send".grey + " " + text);
};
global.error = function(text) {
if (!colors) global.colors = require("colors");
console.log("error".red + " " + text);
};
global.ok = function(text) {
if (config.debuglevel > 4) return;
if (!colors) global.colors = require("colors");
console.log("ok".green + " " + text);
};
// Turns string to its lowercase equivalent, then removes all non-alphanumeric characters.
global.toID = function(text) {
return text.toLowerCase().replace(/[^a-z0-9]/g, "");
};
// Check if everything that is needed is available
try {
require("colors");
}
catch (e) {
console.log("Dependencies are not installed! Please run `npm install` first.");
process.exit(-1);
}
// First dependencies and welcome message
const { inspect } = require("util");
global.colors = require("colors");
console.log("------------------------------------".yellow);
console.log("| Welcome to Pokemon Showdown Bot! |".yellow);
console.log("------------------------------------".yellow);
console.log();
// Config and config.js watching...
global.fs = require('fs');
if (!('existsSync' in fs)) {
fs.existsSync = require('path').existsSync;
}
if (!fs.existsSync("./config.js")) {
error("config.js doesn't exist; are you sure you renamed config-example.js to config.js?");
process.exit(-1);
}
global.config = require("./config.js");
let checkCommandCharacter = function() {
if (!/[^a-z0-9 ]/i.test(config.commandcharacter)) {
error("invalid command character; should at least contain one non-alphanumeric character");
process.exit(-1);
}
};
checkCommandCharacter();
let watchFile = function() {
try {
return fs.watchFile.apply(fs, arguments);
}
catch (e) {
error("your version of node does not support `fs.watchFile`");
}
};
if (config.watchconfig) {
watchFile("./config.js", function(curr, prev) {
if (curr.mtime <= prev.mtime) return;
try {
delete require.cache[require.resolve("./config.js")];
config = require("./config.js");
info("reloaded config.js");
checkCommandCharacter();
} catch (e) {}
});
}
// The actual connection.
info("starting server");
let WebSocketClient = require("websocket").client;
global.Commands = require("./commands.js").commands;
global.Parse = require("./parser.js").parse;
global.Aliases = require("./aliases.js").aliases;
global.Connection = null;
const MESSAGE_THROTTLE = 300;
let queue = [];
let dequeueTimeout = null;
let lastSentAt = 0;
global.send = function(data) {
if (!data || !Connection.connected) return false;
let now = Date.now();
if (now < lastSentAt + MESSAGE_THROTTLE - 5) {
queue.push(data);
if (!dequeueTimeout) {
dequeueTimeout = setTimeout(dequeue, now - lastSentAt + MESSAGE_THROTTLE);
}
return false;
}
if (!Array.isArray(data)) {
data = [data.toString()];
}
data = JSON.stringify(data);
dsend(data);
Connection.send(data);
lastSentAt = now;
if (dequeueTimeout) {
if (queue.length) {
dequeueTimeout = setTimeout(dequeue, MESSAGE_THROTTLE);
}
else {
dequeueTimeout = null;
}
}
};
function dequeue() {
send(queue.shift());
}
let connect = function(retry) {
if (retry) {
info("retrying...");
}
let ws = new WebSocketClient();
ws.on("connectFailed", function(err) {
error("Could not connect to server " + config.server + ": " + inspect(err));
info("retrying in one minute");
setTimeout(function() {connect(true);}, 60000);
});
ws.on("connect", function(con) {
Connection = con;
ok("connected to server " + config.server);
con.on("error", function(err) {
error("connection error: " + err);
});
con.on("close", function() {
error("connection closed: " + inspect(arguments) + " at " + new Date().toLocaleString());
info("retrying in " + config.timeout + " seconds");
setTimeout(function() {connect(true);}, config.timeout * 1000);
});
con.on("message", function(response) {
if (response.type !== 'utf8') return false;
let message = response.utf8Data;
recv(message);
// SockJS messages sent from the server begin with 'a'
// this filters out other SockJS response types (heartbeats in particular)
if (message.charAt(0) !== 'a') return false;
Parse.data(message);
});
});
// The connection itself
let id = Math.floor(Math.random() * 1000);
let chars = "abcdefghijklmnopqrstuvwxyz0123456789_";
let str = "";
for (let i = 0, l = chars.length; i < 8; i++) {
str += chars.charAt(Math.floor(Math.random() * l));
}
let conStr = "wss://" + config.server + ':' + config.port + "/showdown/" + id + '/' + str + "/websocket";
info("connecting to " + conStr + " - secondary protocols: " + (config.secprotocols.join(", ") || "none"));
ws.connect(conStr, config.secprotocols);
};
connect();