Refactor command reloading
This commit is contained in:
parent
86f6811e6e
commit
239d69d6d3
4 changed files with 28 additions and 30 deletions
|
@ -27,9 +27,7 @@ class ImageCommand extends Command {
|
||||||
params: {}
|
params: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (this.type === "application") {
|
if (this.type === "application") await this.acknowledge();
|
||||||
await this.acknowledge();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.constructor.requiresImage) {
|
if (this.constructor.requiresImage) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -6,6 +6,7 @@ class MusicAIOCommand extends Command {
|
||||||
async run() {
|
async run() {
|
||||||
let cmd = this.type === "classic" ? this.args[0] : this.optionsArray[0].name;
|
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";
|
if (cmd === "music" || this.constructor.aliases.includes(cmd)) return "https://projectlounge.pw/robotdance.gif";
|
||||||
|
await this.acknowledge();
|
||||||
if (this.type === "classic") {
|
if (this.type === "classic") {
|
||||||
this.origOptions.args.shift();
|
this.origOptions.args.shift();
|
||||||
} else {
|
} else {
|
||||||
|
|
20
shard.js
20
shard.js
|
@ -8,7 +8,7 @@ import { fileURLToPath } from "url";
|
||||||
// fancy loggings
|
// fancy loggings
|
||||||
import { log, error } from "./utils/logger.js";
|
import { log, error } from "./utils/logger.js";
|
||||||
// initialize command loader
|
// initialize command loader
|
||||||
import { load, update } from "./utils/handler.js";
|
import { load, send } from "./utils/handler.js";
|
||||||
// lavalink stuff
|
// lavalink stuff
|
||||||
import { checkStatus, connect, reload, status, connected } from "./utils/soundplayer.js";
|
import { checkStatus, connect, reload, status, connected } from "./utils/soundplayer.js";
|
||||||
// database stuff
|
// database stuff
|
||||||
|
@ -47,22 +47,14 @@ class Shard extends BaseClusterWorker {
|
||||||
for await (const commandFile of this.getFiles(resolve(dirname(fileURLToPath(import.meta.url)), "./commands/"))) {
|
for await (const commandFile of this.getFiles(resolve(dirname(fileURLToPath(import.meta.url)), "./commands/"))) {
|
||||||
log("log", `Loading command from ${commandFile}...`);
|
log("log", `Loading command from ${commandFile}...`);
|
||||||
try {
|
try {
|
||||||
await load(this.bot, this.clusterID, this.workerID, this.ipc, commandFile, soundStatus);
|
await load(this.bot, commandFile, soundStatus);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
error(`Failed to register command from ${commandFile}: ${e}`);
|
error(`Failed to register command from ${commandFile}: ${e}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (types.application) {
|
if (types.application) {
|
||||||
const commandArray = await update(this.bot, this.clusterID, this.workerID, this.ipc, soundStatus);
|
|
||||||
try {
|
try {
|
||||||
log("info", "Sending application command data to Discord...");
|
await send(this.bot);
|
||||||
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);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log("error", e);
|
log("error", e);
|
||||||
log("error", "Failed to send command data to Discord, slash/message commands may be unavailable.");
|
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) => {
|
this.ipc.register("reload", async (message) => {
|
||||||
const path = paths.get(message);
|
const path = paths.get(message);
|
||||||
if (!path) return this.ipc.broadcast("reloadFail", { result: "I couldn't find that command!" });
|
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);
|
try {
|
||||||
|
const result = await load(this.bot, path, await checkStatus(), true);
|
||||||
if (result !== message) return this.ipc.broadcast("reloadFail", { result });
|
if (result !== message) return this.ipc.broadcast("reloadFail", { result });
|
||||||
return this.ipc.broadcast("reloadSuccess");
|
return this.ipc.broadcast("reloadSuccess");
|
||||||
|
} catch (result) {
|
||||||
|
return this.ipc.broadcast("reloadFail", { result });
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
this.ipc.register("soundreload", async () => {
|
this.ipc.register("soundreload", async () => {
|
||||||
|
|
|
@ -8,7 +8,7 @@ const { blacklist } = JSON.parse(readFileSync(new URL("../config/commands.json",
|
||||||
let queryValue = 0;
|
let queryValue = 0;
|
||||||
|
|
||||||
// load command into memory
|
// 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}`);
|
const { default: props } = await import(`${command}?v=${queryValue}`);
|
||||||
queryValue++;
|
queryValue++;
|
||||||
if (props.requires.includes("sound") && soundStatus) {
|
if (props.requires.includes("sound") && soundStatus) {
|
||||||
|
@ -52,19 +52,10 @@ export async function load(client, cluster, worker, ipc, command, soundStatus, s
|
||||||
commandInfo.type = 3;
|
commandInfo.type = 3;
|
||||||
} else {
|
} else {
|
||||||
commands.set(commandName, props);
|
commands.set(commandName, props);
|
||||||
|
}
|
||||||
|
|
||||||
if (slashReload && props.slashAllowed) {
|
if (slashReload && props.slashAllowed) {
|
||||||
const commandList = await client.getCommands();
|
await send(client);
|
||||||
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 (Object.getPrototypeOf(props).name === "SoundboardCommand") sounds.set(commandName, props.file);
|
if (Object.getPrototypeOf(props).name === "SoundboardCommand") sounds.set(commandName, props.file);
|
||||||
|
@ -125,3 +116,15 @@ export async function update() {
|
||||||
private: privateCommandArray
|
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);
|
||||||
|
}
|
Loading…
Reference in a new issue