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
|
|
|
// shard base
|
|
|
|
const { Base } = require("eris-sharder");
|
|
|
|
// path stuff
|
|
|
|
const { readdir } = require("fs").promises;
|
|
|
|
// fancy loggings
|
|
|
|
const logger = require("./utils/logger.js");
|
|
|
|
// initialize command loader
|
|
|
|
const handler = require("./utils/handler.js");
|
|
|
|
// lavalink stuff
|
|
|
|
const sound = require("./utils/soundplayer.js");
|
|
|
|
// image processing stuff
|
|
|
|
const image = require("./utils/image.js");
|
|
|
|
// database stuff
|
|
|
|
const database = require("./utils/database.js");
|
|
|
|
// command collections
|
|
|
|
const collections = require("./utils/collections.js");
|
|
|
|
// playing messages
|
2021-04-23 20:03:48 +00:00
|
|
|
const { messages } = require("./messages.json");
|
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
|
|
|
// other stuff
|
|
|
|
const misc = require("./utils/misc.js");
|
|
|
|
// generate help page
|
|
|
|
const helpGenerator =
|
|
|
|
process.env.OUTPUT !== "" ? require("./utils/help.js") : null;
|
2021-05-02 20:16:48 +00:00
|
|
|
const http = require("http");
|
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
|
|
|
|
|
|
|
class Shard extends Base {
|
|
|
|
constructor(bot) {
|
|
|
|
super(bot);
|
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
|
|
|
// register commands and their info
|
|
|
|
const soundStatus = await sound.checkStatus();
|
|
|
|
logger.log("info", "Attempting to load commands...");
|
|
|
|
for await (const commandFile of this.getFiles("./commands/")) {
|
|
|
|
logger.log("log", `Loading command from ${commandFile}...`);
|
|
|
|
try {
|
|
|
|
await handler.load(commandFile, soundStatus);
|
|
|
|
} catch (e) {
|
|
|
|
logger.error(`Failed to register command from ${commandFile}: ${e}`);
|
|
|
|
}
|
|
|
|
}
|
2021-05-02 20:16:48 +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
|
|
|
// register events
|
|
|
|
const events = await readdir("./events/");
|
|
|
|
logger.log("info", `Attempting to load ${events.length} events...`);
|
|
|
|
for (const file of events) {
|
|
|
|
logger.log("log", `Loading event from ${file}...`);
|
|
|
|
const eventName = file.split(".")[0];
|
|
|
|
const event = require(`./events/${file}`);
|
2021-04-29 21:56:32 +00:00
|
|
|
this.bot.on(eventName, event.bind(null, this.bot, this.clusterID, this.ipc));
|
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-02 20:16:48 +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
|
|
|
// connect to image api if enabled
|
|
|
|
if (process.env.API === "true") {
|
|
|
|
for (const server of image.servers) {
|
|
|
|
try {
|
|
|
|
await image.connect(server);
|
|
|
|
} catch (e) {
|
|
|
|
logger.error(e);
|
|
|
|
}
|
|
|
|
}
|
2021-04-22 14:21:58 +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
|
|
|
|
2021-04-22 14:21:58 +00:00
|
|
|
// generate docs
|
|
|
|
await helpGenerator.generateList();
|
|
|
|
if (this.clusterID === 0 && helpGenerator) {
|
|
|
|
await helpGenerator.createPage(process.env.OUTPUT);
|
|
|
|
logger.log("info", "The help docs have been generated.");
|
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-04 21:28:34 +00:00
|
|
|
if (process.env.METRICS !== "" && process.env.METRICS !== undefined) {
|
2021-05-03 13:30:07 +00:00
|
|
|
const httpServer = http.createServer(async (req, res) => {
|
2021-05-02 20:16:48 +00:00
|
|
|
if (req.method !== "GET") {
|
|
|
|
res.statusCode = 405;
|
|
|
|
return res.end("GET only");
|
|
|
|
}
|
|
|
|
res.write(`# HELP connected_workers Number of workers connected
|
2021-05-03 13:30:07 +00:00
|
|
|
# TYPE connected_workers gauge
|
2021-05-02 20:16:48 +00:00
|
|
|
connected_workers ${image.connections.length}
|
|
|
|
# HELP running_jobs Number of running jobs on this worker
|
2021-05-03 13:30:07 +00:00
|
|
|
# TYPE running_jobs gauge
|
2021-05-02 20:16:48 +00:00
|
|
|
# HELP queued_jobs Number of queued jobs on this worker
|
2021-05-03 13:30:07 +00:00
|
|
|
# TYPE queued_jobs gauge
|
2021-05-02 20:16:48 +00:00
|
|
|
# HELP max_jobs Number of max allowed jobs on this worker
|
2021-05-03 13:30:07 +00:00
|
|
|
# TYPE max_jobs gauge
|
2021-05-03 13:49:55 +00:00
|
|
|
# HELP command_count Number of times a command has been run
|
|
|
|
# TYPE command_count counter
|
2021-05-02 20:16:48 +00:00
|
|
|
`);
|
2021-05-03 13:30:07 +00:00
|
|
|
const servers = await image.getStatus();
|
|
|
|
for (const [i, w] of servers.entries()) {
|
|
|
|
res.write(`running_jobs{worker="${i}"} ${w.runningJobs}\n`);
|
|
|
|
res.write(`queued_jobs{worker="${i}"} ${w.queued}\n`);
|
|
|
|
res.write(`max_jobs{worker="${i}"} ${w.max}\n`);
|
|
|
|
}
|
2021-05-03 13:49:55 +00:00
|
|
|
const counts = await database.getCounts();
|
|
|
|
for (const [i, w] of Object.entries(counts)) {
|
|
|
|
res.write(`command_count{command="${i}"} ${w}\n`);
|
|
|
|
}
|
2021-05-03 13:30:07 +00:00
|
|
|
res.end();
|
2021-05-02 20:16:48 +00:00
|
|
|
});
|
|
|
|
httpServer.listen(process.env.METRICS, () => {
|
|
|
|
logger.log("info", `Serving metrics at ${process.env.METRICS}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// handle process stop
|
|
|
|
process.on("SIGINT", () => {
|
|
|
|
logger.log("warn", "SIGINT detected, shutting down...");
|
|
|
|
this.bot.editStatus("dnd", {
|
|
|
|
name: "Restarting/shutting down..."
|
|
|
|
});
|
|
|
|
for (const command in collections.commands) {
|
|
|
|
handler.unload(command);
|
|
|
|
}
|
|
|
|
this.bot.disconnect();
|
|
|
|
require("./utils/database.js").stop();
|
|
|
|
process.exit(0);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
async* getFiles(dir) {
|
|
|
|
const dirents = await readdir(dir, { withFileTypes: true });
|
|
|
|
for (const dirent of dirents) {
|
|
|
|
if (dirent.isDirectory()) {
|
|
|
|
yield* this.getFiles(dir + dirent.name);
|
|
|
|
} else {
|
|
|
|
yield dir + (dir.charAt(dir.length - 1) !== "/" ? "/" : "") + dirent.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async launch() {
|
2021-05-11 16:12:01 +00:00
|
|
|
await this.init();
|
2021-04-29 21:56:32 +00:00
|
|
|
|
|
|
|
this.ipc.register("stat", (message) => {
|
|
|
|
collections.stats = message;
|
|
|
|
});
|
2021-04-30 17:31:53 +00:00
|
|
|
|
2021-05-11 16:12:01 +00:00
|
|
|
this.ipc.register("restart", async () => {
|
|
|
|
await this.bot.editStatus("dnd", {
|
2021-05-11 17:13:33 +00:00
|
|
|
name: "esmBot is restarting, please stand by."
|
2021-05-11 16:12:01 +00:00
|
|
|
});
|
2021-04-30 17:31:53 +00:00
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.ipc.register("reload", async (message) => {
|
|
|
|
const result = await handler.unload(message.cmd);
|
|
|
|
if (result) return this.ipc.broadcast("reloadFail", { result: result });
|
|
|
|
const result2 = await handler.load(collections.paths.get(message.cmd));
|
|
|
|
if (result2) return this.ipc.broadcast("reloadFail", { result: result2 });
|
|
|
|
return this.ipc.broadcast("reloadSuccess");
|
|
|
|
});
|
2021-04-21 18:08:52 +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
|
|
|
// connect to lavalink
|
|
|
|
if (!sound.status && !sound.connected) await sound.connect(this.bot);
|
|
|
|
|
2021-04-15 00:57:35 +00:00
|
|
|
this.bot.privateChannels.limit = 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
|
|
|
await database.setup();
|
|
|
|
|
|
|
|
// set activity (a.k.a. the gamer code)
|
|
|
|
(async function activityChanger() {
|
|
|
|
this.bot.editStatus("dnd", {
|
|
|
|
name: `${misc.random(messages)} | @${this.bot.user.username} help`,
|
|
|
|
});
|
|
|
|
setTimeout(activityChanger.bind(this), 900000);
|
|
|
|
}).bind(this)();
|
|
|
|
|
|
|
|
logger.log("info", `Started cluster ${this.clusterID}.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-05-02 20:16:48 +00:00
|
|
|
module.exports = Shard;
|