Refactor command reloading

This commit is contained in:
Essem 2022-09-10 23:06:40 -05:00
parent 86f6811e6e
commit 239d69d6d3
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
4 changed files with 28 additions and 30 deletions

View file

@ -27,9 +27,7 @@ class ImageCommand extends Command {
params: {}
};
if (this.type === "application") {
await this.acknowledge();
}
if (this.type === "application") await this.acknowledge();
if (this.constructor.requiresImage) {
try {

View file

@ -6,6 +6,7 @@ class MusicAIOCommand extends Command {
async run() {
let cmd = this.type === "classic" ? this.args[0] : this.optionsArray[0].name;
if (cmd === "music" || this.constructor.aliases.includes(cmd)) return "https://projectlounge.pw/robotdance.gif";
await this.acknowledge();
if (this.type === "classic") {
this.origOptions.args.shift();
} else {

View file

@ -8,7 +8,7 @@ import { fileURLToPath } from "url";
// fancy loggings
import { log, error } from "./utils/logger.js";
// initialize command loader
import { load, update } from "./utils/handler.js";
import { load, send } from "./utils/handler.js";
// lavalink stuff
import { checkStatus, connect, reload, status, connected } from "./utils/soundplayer.js";
// database stuff
@ -47,22 +47,14 @@ class Shard extends BaseClusterWorker {
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);
await load(this.bot, commandFile, soundStatus);
} catch (e) {
error(`Failed to register command from ${commandFile}: ${e}`);
}
}
if (types.application) {
const commandArray = await update(this.bot, this.clusterID, this.workerID, this.ipc, soundStatus);
try {
log("info", "Sending application command data to Discord...");
let cmdArray = commandArray.main;
if (process.env.ADMIN_SERVER && process.env.ADMIN_SERVER !== "") {
await this.bot.bulkEditGuildCommands(process.env.ADMIN_SERVER, commandArray.private);
} else {
cmdArray = [...commandArray.main, ...commandArray.private];
}
await this.bot.bulkEditCommands(cmdArray);
await send(this.bot);
} catch (e) {
log("error", e);
log("error", "Failed to send command data to Discord, slash/message commands may be unavailable.");
@ -99,9 +91,13 @@ class Shard extends BaseClusterWorker {
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!" });
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");
try {
const result = await load(this.bot, path, await checkStatus(), true);
if (result !== message) return this.ipc.broadcast("reloadFail", { result });
return this.ipc.broadcast("reloadSuccess");
} catch (result) {
return this.ipc.broadcast("reloadFail", { result });
}
});
this.ipc.register("soundreload", async () => {

View file

@ -8,7 +8,7 @@ const { blacklist } = JSON.parse(readFileSync(new URL("../config/commands.json",
let queryValue = 0;
// load command into memory
export async function load(client, cluster, worker, ipc, command, soundStatus, slashReload = false) {
export async function load(client, command, soundStatus, slashReload = false) {
const { default: props } = await import(`${command}?v=${queryValue}`);
queryValue++;
if (props.requires.includes("sound") && soundStatus) {
@ -52,19 +52,10 @@ export async function load(client, cluster, worker, ipc, command, soundStatus, s
commandInfo.type = 3;
} else {
commands.set(commandName, props);
}
if (slashReload && props.slashAllowed) {
const commandList = await client.getCommands();
const oldCommand = commandList.filter((item) => {
return item.name === commandName;
})[0];
await client.editCommand(oldCommand.id, {
name: commandName,
type: 1,
description: props.description,
options: props.flags
});
}
if (slashReload && props.slashAllowed) {
await send(client);
}
if (Object.getPrototypeOf(props).name === "SoundboardCommand") sounds.set(commandName, props.file);
@ -124,4 +115,16 @@ export async function update() {
main: commandArray,
private: privateCommandArray
};
}
export async function send(bot) {
const commandArray = await update();
log("info", "Sending application command data to Discord...");
let cmdArray = commandArray.main;
if (process.env.ADMIN_SERVER && process.env.ADMIN_SERVER !== "") {
await bot.bulkEditGuildCommands(process.env.ADMIN_SERVER, commandArray.private);
} else {
cmdArray = [...commandArray.main, ...commandArray.private];
}
await bot.bulkEditCommands(cmdArray);
}