mrmBot-Matrix/shard.js

151 lines
4.9 KiB
JavaScript
Raw Normal View History

// shard base
import { BaseClusterWorker } from "eris-fleet";
// path stuff
import { readdir } from "fs/promises";
import { readFileSync } from "fs";
import { resolve, dirname } from "path";
import { fileURLToPath } from "url";
// fancy loggings
import { log, error } from "./utils/logger.js";
// initialize command loader
import { load, update } from "./utils/handler.js";
// lavalink stuff
2022-06-25 17:45:42 +00:00
import { checkStatus, connect, reload, status, connected } from "./utils/soundplayer.js";
// database stuff
import database from "./utils/database.js";
// command collections
import { paths } from "./utils/collections.js";
// playing messages
2022-07-26 15:38:42 +00:00
const { messages } = JSON.parse(readFileSync(new URL("./config/messages.json", import.meta.url)));
// other stuff
import { random } from "./utils/misc.js";
// generate help page
import { generateList, createPage } from "./utils/help.js";
2021-07-31 05:04:38 +00:00
// whether a broadcast is currently in effect
let broadcast = false;
2021-07-05 04:15:27 +00:00
class Shard extends BaseClusterWorker {
constructor(bot) {
super(bot);
2021-07-05 04:15:27 +00:00
2022-01-23 05:16:55 +00:00
console.info = (str) => this.ipc.sendToAdmiral("info", str);
2021-07-05 04:15:27 +00:00
this.init();
}
async init() {
// register commands and their info
const soundStatus = await checkStatus();
log("info", "Attempting to load commands...");
for await (const commandFile of this.getFiles(resolve(dirname(fileURLToPath(import.meta.url)), "./commands/"))) {
log("log", `Loading command from ${commandFile}...`);
try {
await load(this.bot, this.clusterID, this.workerID, this.ipc, commandFile, soundStatus);
} catch (e) {
error(`Failed to register command from ${commandFile}: ${e}`);
}
}
const commandArray = await update(this.bot, this.clusterID, this.workerID, this.ipc, soundStatus);
log("info", "Finished loading commands.");
await database.setup(this.ipc);
await this.bot.bulkEditCommands(commandArray);
// register events
2022-02-23 21:51:20 +00:00
log("info", "Attempting to load events...");
for await (const file of this.getFiles(resolve(dirname(fileURLToPath(import.meta.url)), "./events/"))) {
log("log", `Loading event from ${file}...`);
2022-01-25 21:27:31 +00:00
const eventArray = file.split("/");
const eventName = eventArray[eventArray.length - 1].split(".")[0];
const { default: event } = await import(file);
2021-07-05 04:15:27 +00:00
this.bot.on(eventName, event.bind(null, this.bot, this.clusterID, this.workerID, this.ipc));
}
log("info", "Finished loading events.");
// generate docs
if (process.env.OUTPUT && process.env.OUTPUT !== "") {
await generateList();
if (this.clusterID === 0) {
await createPage(process.env.OUTPUT);
log("info", "The help docs have been generated.");
}
}
this.ipc.register("reload", async (message) => {
const path = paths.get(message);
if (!path) return this.ipc.broadcast("reloadFail", { result: "I couldn't find that command!" });
2022-03-31 19:53:22 +00:00
const result = await load(this.bot, this.clusterID, this.workerID, this.ipc, path, await checkStatus(), true);
if (result !== message) return this.ipc.broadcast("reloadFail", { result });
return this.ipc.broadcast("reloadSuccess");
});
2021-05-22 15:10:42 +00:00
2021-07-05 04:15:27 +00:00
this.bot.privateChannels.limit = 0;
2021-05-22 15:10:42 +00:00
this.ipc.register("soundreload", async () => {
const soundStatus = await checkStatus();
2021-05-22 15:10:42 +00:00
if (!soundStatus) {
const length = reload();
2021-05-22 15:10:42 +00:00
return this.ipc.broadcast("soundReloadSuccess", { length });
} else {
return this.ipc.broadcast("soundReloadFail");
}
});
2021-07-31 05:04:38 +00:00
this.ipc.register("playbroadcast", (message) => {
2021-07-31 05:04:38 +00:00
this.bot.editStatus("dnd", {
name: `${message} | @${this.bot.user.username} help`,
2021-07-31 05:04:38 +00:00
});
broadcast = true;
return this.ipc.broadcast("broadcastSuccess");
});
this.ipc.register("broadcastend", () => {
2021-07-31 05:04:38 +00:00
this.bot.editStatus("dnd", {
name: `${random(messages)} | @${this.bot.user.username} help`,
2021-07-31 05:04:38 +00:00
});
broadcast = false;
return this.ipc.broadcast("broadcastEnd");
});
2021-08-14 21:15:21 +00:00
// connect to lavalink
if (!status && !connected) connect(this.bot);
2021-07-31 05:04:38 +00:00
this.activityChanger();
log("info", `Started worker ${this.workerID}.`);
2021-07-31 05:04:38 +00:00
}
// set activity (a.k.a. the gamer code)
activityChanger() {
if (!broadcast) {
this.bot.editStatus("dnd", {
name: `${random(messages)} | @${this.bot.user.username} help`,
});
2021-07-31 05:04:38 +00:00
}
2021-08-01 03:26:48 +00:00
setTimeout(this.activityChanger.bind(this), 900000);
2021-07-05 04:15:27 +00:00
}
async* getFiles(dir) {
const dirents = await readdir(dir, { withFileTypes: true });
for (const dirent of dirents) {
const name = dir + (dir.charAt(dir.length - 1) !== "/" ? "/" : "") + dirent.name;
2021-07-05 04:15:27 +00:00
if (dirent.isDirectory()) {
yield* this.getFiles(name);
} else if (dirent.name.endsWith(".js")) {
yield name;
2021-07-05 04:15:27 +00:00
}
}
}
shutdown(done) {
log("warn", "Shutting down...");
2021-07-05 04:15:27 +00:00
this.bot.editStatus("dnd", {
name: "Restarting/shutting down..."
});
database.stop();
2021-07-05 04:15:27 +00:00
done();
}
}
export default Shard;