2020-08-28 02:34:12 +00:00
|
|
|
const magick = require("../build/Release/image.node");
|
2021-01-10 01:50:29 +00:00
|
|
|
const { Worker } = require("worker_threads");
|
2020-09-01 22:10:19 +00:00
|
|
|
const fetch = require("node-fetch");
|
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
|
|
|
const fs = require("fs");
|
2021-06-18 05:10:11 +00:00
|
|
|
const WebSocket = require("ws");
|
2020-10-18 21:53:35 +00:00
|
|
|
const fileType = require("file-type");
|
2021-01-10 01:50:29 +00:00
|
|
|
const path = require("path");
|
2021-01-18 20:11:28 +00:00
|
|
|
const { EventEmitter } = require("events");
|
|
|
|
const logger = require("./logger.js");
|
2020-10-18 21:53:35 +00:00
|
|
|
|
2021-04-19 14:31:39 +00:00
|
|
|
const formats = ["image/jpeg", "image/png", "image/webp", "image/gif", "video/mp4", "video/webm", "video/mov"];
|
2020-08-28 02:34:12 +00:00
|
|
|
|
2021-05-18 03:37:40 +00:00
|
|
|
exports.jobs = {};
|
2021-01-18 20:11:28 +00:00
|
|
|
|
2021-06-18 05:10:11 +00:00
|
|
|
exports.connections = new Map();
|
2021-01-18 20:11:28 +00:00
|
|
|
|
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
|
|
|
exports.servers = JSON.parse(fs.readFileSync("./servers.json", { encoding: "utf8" })).image;
|
|
|
|
|
2020-12-26 02:27:45 +00:00
|
|
|
const chooseServer = async (ideal) => {
|
|
|
|
if (ideal.length === 0) throw "No available servers";
|
|
|
|
const sorted = ideal.sort((a, b) => {
|
2021-01-08 18:08:10 +00:00
|
|
|
return b.load - a.load;
|
2020-12-26 02:27:45 +00:00
|
|
|
});
|
|
|
|
return sorted[0];
|
|
|
|
};
|
|
|
|
|
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
|
|
|
exports.repopulate = async () => {
|
|
|
|
const data = await fs.promises.readFile("./servers.json", { encoding: "utf8" });
|
|
|
|
this.servers = JSON.parse(data).image;
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2021-06-19 00:10:13 +00:00
|
|
|
exports.getRunning = async () => {
|
|
|
|
let serversLeft = this.connections.size;
|
|
|
|
const statuses = [];
|
|
|
|
for (const address of this.connections.keys()) {
|
|
|
|
const connection = this.connections.get(address);
|
|
|
|
if (connection.readyState !== 0 && connection.readyState !== 1) {
|
|
|
|
serversLeft--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const controller = new AbortController(); // eslint-disable-line no-undef
|
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
|
|
|
const timeout = setTimeout(() => {
|
2021-06-19 00:10:13 +00:00
|
|
|
controller.abort();
|
|
|
|
}, 2000);
|
|
|
|
try {
|
|
|
|
const statusRequest = await fetch(`http://${address}:8080/running`, { signal: controller.signal });
|
|
|
|
clearTimeout(timeout);
|
|
|
|
const status = await statusRequest.json();
|
|
|
|
serversLeft--;
|
|
|
|
statuses.push(status);
|
|
|
|
} catch (e) {
|
|
|
|
if (e.name === "AbortError") {
|
2021-05-18 00:08:06 +00:00
|
|
|
serversLeft--;
|
|
|
|
continue;
|
2021-06-19 00:10:13 +00:00
|
|
|
} else if (e.code === "ECONNREFUSED") {
|
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
|
|
|
serversLeft--;
|
2021-06-19 00:10:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw e;
|
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
|
|
|
}
|
2021-06-19 00:10:13 +00:00
|
|
|
}
|
|
|
|
if (!serversLeft) {
|
|
|
|
return statuses;
|
|
|
|
} else {
|
|
|
|
throw new Error("Loop ended before all servers could be checked");
|
|
|
|
}
|
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
|
|
|
};
|
|
|
|
|
2021-06-27 01:58:22 +00:00
|
|
|
exports.connect = async (server) => {
|
2021-06-19 00:10:13 +00:00
|
|
|
const connection = new WebSocket(`ws://${server}:8080/sock`);
|
|
|
|
connection.on("message", async (msg) => {
|
|
|
|
const opcode = msg.readUint8(0);
|
|
|
|
const req = msg.slice(37, msg.length);
|
|
|
|
const uuid = msg.slice(1, 37).toString();
|
|
|
|
if (opcode === 0x00) { // Job queued
|
|
|
|
if (this.jobs[req]) {
|
|
|
|
this.jobs[req].event.emit("uuid", uuid);
|
|
|
|
}
|
|
|
|
} else if (opcode === 0x01) { // Job completed successfully
|
|
|
|
// the image API sends all job responses over the same socket; make sure this is ours
|
|
|
|
if (this.jobs[uuid]) {
|
|
|
|
const imageReq = await fetch(`http://${server}:8080/image?id=${uuid}`);
|
|
|
|
const image = await imageReq.buffer();
|
|
|
|
// The response data is given as the file extension/ImageMagick type of the image (e.g. "png"), followed
|
|
|
|
// by a newline, followed by the image data.
|
2021-01-18 20:11:28 +00:00
|
|
|
|
2021-06-19 00:10:13 +00:00
|
|
|
this.jobs[uuid].event.emit("image", image, imageReq.headers.get("ext"));
|
2021-01-18 20:11:28 +00:00
|
|
|
}
|
2021-06-19 00:10:13 +00:00
|
|
|
} else if (opcode === 0x02) { // Job errored
|
|
|
|
if (this.jobs[uuid]) {
|
|
|
|
this.jobs[uuid].event.emit("error", new Error(req));
|
2021-05-06 21:40:05 +00:00
|
|
|
}
|
2021-06-19 00:10:13 +00:00
|
|
|
}
|
2021-01-18 20:11:28 +00:00
|
|
|
});
|
2021-06-19 00:10:13 +00:00
|
|
|
connection.on("error", (e) => {
|
|
|
|
logger.error(e.toString());
|
|
|
|
});
|
|
|
|
connection.once("close", () => {
|
|
|
|
for (const uuid of Object.keys(this.jobs)) {
|
2021-06-27 22:06:52 +00:00
|
|
|
if (this.jobs[uuid].addr === server) {
|
|
|
|
this.jobs[uuid].event.emit("error", "Job ended prematurely due to a closed connection; please run your image job again");
|
|
|
|
delete this.jobs[uuid];
|
|
|
|
}
|
2021-06-19 00:10:13 +00:00
|
|
|
}
|
|
|
|
//logger.log(`Lost connection to ${server}, attempting to reconnect...`);
|
|
|
|
this.connections.delete(server);
|
|
|
|
});
|
|
|
|
this.connections.set(server, connection);
|
2021-01-18 20:11:28 +00:00
|
|
|
};
|
|
|
|
|
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
|
|
|
exports.disconnect = async () => {
|
2021-06-18 05:10:11 +00:00
|
|
|
for (const connection of this.connections.values()) {
|
|
|
|
connection.close();
|
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
|
|
|
}
|
2021-05-18 03:37:40 +00:00
|
|
|
for (const uuid of Object.keys(this.jobs)) {
|
|
|
|
this.jobs[uuid].event.emit("error", "Job ended prematurely (not really an error; just run your image job again)");
|
|
|
|
delete this.jobs[uuid];
|
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
|
|
|
}
|
2021-06-18 05:10:11 +00:00
|
|
|
this.connections.clear();
|
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2021-06-19 00:10:13 +00:00
|
|
|
const getIdeal = async () => {
|
|
|
|
let serversLeft = this.connections.size;
|
2021-06-28 22:59:05 +00:00
|
|
|
if (serversLeft < this.servers.length) {
|
2021-06-19 00:10:13 +00:00
|
|
|
for (const server of this.servers) {
|
2021-01-18 20:11:28 +00:00
|
|
|
try {
|
2021-06-28 22:59:05 +00:00
|
|
|
if (!this.connections.has(server)) await this.connect(server);
|
2021-01-18 20:11:28 +00:00
|
|
|
} catch (e) {
|
2021-06-19 00:10:13 +00:00
|
|
|
logger.error(e);
|
2021-01-18 20:11:28 +00:00
|
|
|
}
|
2021-06-19 00:10:13 +00:00
|
|
|
}
|
|
|
|
serversLeft = this.connections.size;
|
|
|
|
}
|
|
|
|
const idealServers = [];
|
|
|
|
for (const address of this.connections.keys()) {
|
|
|
|
const connection = this.connections.get(address);
|
|
|
|
if (connection.readyState !== 0 && connection.readyState !== 1) {
|
|
|
|
serversLeft--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const controller = new AbortController(); // eslint-disable-line no-undef
|
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
controller.abort();
|
2021-07-02 04:42:12 +00:00
|
|
|
}, 5000);
|
2021-06-19 00:10:13 +00:00
|
|
|
try {
|
|
|
|
const statusRequest = await fetch(`http://${address}:8080/status`, { signal: controller.signal });
|
|
|
|
clearTimeout(timeout);
|
|
|
|
const status = await statusRequest.text();
|
|
|
|
serversLeft--;
|
|
|
|
idealServers.push({
|
|
|
|
addr: address,
|
|
|
|
load: parseInt(status)
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
if (e.name === "AbortError") {
|
2021-05-17 19:23:29 +00:00
|
|
|
serversLeft--;
|
|
|
|
continue;
|
2021-06-19 00:10:13 +00:00
|
|
|
} else if (e.code === "ECONNREFUSED") {
|
2020-12-26 02:27:45 +00:00
|
|
|
serversLeft--;
|
2021-06-19 00:10:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
} finally {
|
2021-05-17 19:23:29 +00:00
|
|
|
clearTimeout(timeout);
|
2021-01-18 20:11:28 +00:00
|
|
|
}
|
2021-06-19 00:10:13 +00:00
|
|
|
}
|
|
|
|
if (!serversLeft) {
|
|
|
|
const server = await chooseServer(idealServers);
|
|
|
|
return { addr: server.addr, sock: this.connections.get(server.addr) };
|
|
|
|
} else {
|
|
|
|
throw new Error("Loop ended before all servers could be checked");
|
|
|
|
}
|
2021-01-18 20:11:28 +00:00
|
|
|
};
|
|
|
|
|
2021-06-19 00:10:13 +00:00
|
|
|
const start = async (object, num) => {
|
|
|
|
const currentServer = await getIdeal();
|
|
|
|
const data = Buffer.concat([Buffer.from([0x01 /* queue job */]), Buffer.from(num.length.toString()), Buffer.from(num), Buffer.from(JSON.stringify(object))]);
|
|
|
|
currentServer.sock.send(data);
|
|
|
|
const event = new EventEmitter();
|
|
|
|
this.jobs[num] = { event, addr: currentServer.addr };
|
|
|
|
const uuid = await new Promise((resolve, reject) => {
|
|
|
|
event.once("uuid", (uuid) => resolve(uuid));
|
|
|
|
event.once("error", reject);
|
2020-12-26 02:27:45 +00:00
|
|
|
});
|
2021-06-19 00:10:13 +00:00
|
|
|
delete this.jobs[num];
|
|
|
|
this.jobs[uuid] = { event: event, addr: currentServer.addr };
|
|
|
|
return { uuid: uuid, event: event };
|
2020-12-26 02:27:45 +00:00
|
|
|
};
|
|
|
|
|
2020-11-26 17:48:19 +00:00
|
|
|
exports.check = (cmd) => {
|
|
|
|
return magick[cmd] ? true : false;
|
|
|
|
};
|
|
|
|
|
2021-06-28 22:59:05 +00:00
|
|
|
exports.getType = async (image, extraReturnTypes) => {
|
2020-11-26 17:48:19 +00:00
|
|
|
if (!image.startsWith("http")) {
|
|
|
|
const imageType = await fileType.fromFile(image);
|
|
|
|
if (imageType && formats.includes(imageType.mime)) {
|
|
|
|
return imageType.mime;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
let type;
|
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
|
|
|
const controller = new AbortController(); // eslint-disable-line no-undef
|
2020-11-26 17:48:19 +00:00
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
controller.abort();
|
|
|
|
}, 25000);
|
|
|
|
try {
|
2021-05-22 15:10:42 +00:00
|
|
|
const imageRequest = await fetch(image, {
|
|
|
|
signal: controller.signal, headers: {
|
|
|
|
"Range": "bytes=0-1023"
|
|
|
|
}
|
|
|
|
});
|
2021-01-04 16:29:18 +00:00
|
|
|
clearTimeout(timeout);
|
2021-05-15 03:02:50 +00:00
|
|
|
const size = imageRequest.headers.has("Content-Range") ? imageRequest.headers.get("Content-Range").split("/")[1] : imageRequest.headers.get("Content-Length");
|
2021-06-28 22:59:05 +00:00
|
|
|
if (parseInt(size) > 26214400 && extraReturnTypes) { // 25 MB
|
2021-05-07 03:01:30 +00:00
|
|
|
type = "large";
|
|
|
|
return type;
|
|
|
|
}
|
2020-11-26 17:48:19 +00:00
|
|
|
const imageBuffer = await imageRequest.buffer();
|
|
|
|
const imageType = await fileType.fromBuffer(imageBuffer);
|
|
|
|
if (imageType && formats.includes(imageType.mime)) {
|
|
|
|
type = imageType.mime;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (error.name === "AbortError") {
|
|
|
|
throw Error("Timed out");
|
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
};
|
|
|
|
|
2021-07-02 04:42:12 +00:00
|
|
|
exports.run = (object) => {
|
2021-03-16 03:29:48 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2021-01-10 01:50:29 +00:00
|
|
|
if (process.env.API === "true") {
|
2021-03-16 03:29:48 +00:00
|
|
|
// Connect to best image server
|
|
|
|
const num = Math.floor(Math.random() * 100000).toString().slice(0, 5);
|
|
|
|
const timeout = setTimeout(() => {
|
2021-05-18 03:37:40 +00:00
|
|
|
if (this.jobs[num]) delete this.jobs[num];
|
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
|
|
|
reject("the image request timed out after 25 seconds. Try uploading your image elsewhere.");
|
2021-03-16 03:29:48 +00:00
|
|
|
}, 25000);
|
|
|
|
start(object, num).catch(err => { // incredibly hacky code incoming
|
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
|
|
|
clearTimeout(timeout);
|
2021-03-16 03:29:48 +00:00
|
|
|
if (err instanceof Error) return reject(err);
|
|
|
|
return err;
|
|
|
|
}).then((data) => {
|
2021-01-18 20:11:28 +00:00
|
|
|
clearTimeout(timeout);
|
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
|
|
|
if (!data.event) reject("Not connected to image server");
|
2021-03-16 03:29:48 +00:00
|
|
|
data.event.once("image", (image, type) => {
|
2021-05-18 03:37:40 +00:00
|
|
|
delete this.jobs[data.uuid];
|
2021-01-18 20:11:28 +00:00
|
|
|
const payload = {
|
2021-05-22 15:10:42 +00:00
|
|
|
// Take just the image data
|
2021-01-18 20:11:28 +00:00
|
|
|
buffer: image,
|
|
|
|
type: type
|
|
|
|
};
|
|
|
|
resolve(payload);
|
|
|
|
});
|
2021-03-16 03:29:48 +00:00
|
|
|
data.event.once("error", (err) => {
|
2021-06-02 01:27:28 +00:00
|
|
|
delete this.jobs[data.uuid];
|
2021-01-10 01:50:29 +00:00
|
|
|
reject(err);
|
2021-01-18 20:11:28 +00:00
|
|
|
});
|
2021-03-18 14:29:03 +00:00
|
|
|
return;
|
2021-03-16 03:29:48 +00:00
|
|
|
}).catch(err => reject(err));
|
2021-01-10 01:50:29 +00:00
|
|
|
} else {
|
|
|
|
// Called from command (not using image API)
|
|
|
|
const worker = new Worker(path.join(__dirname, "image-runner.js"), {
|
2020-11-26 17:48:19 +00:00
|
|
|
workerData: object
|
|
|
|
});
|
2021-05-06 21:40:05 +00:00
|
|
|
worker.once("message", (data) => {
|
2020-11-26 17:48:19 +00:00
|
|
|
resolve({
|
|
|
|
buffer: Buffer.from([...data.buffer]),
|
2021-01-10 01:50:29 +00:00
|
|
|
type: data.fileExtension
|
2020-11-26 17:48:19 +00:00
|
|
|
});
|
|
|
|
});
|
2021-05-06 21:40:05 +00:00
|
|
|
worker.once("error", reject);
|
2020-11-17 14:52:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|