From 7fae0c15824c2c066aa13c4a53ee52ec3f3ff9df Mon Sep 17 00:00:00 2001 From: Essem Date: Mon, 22 Aug 2022 13:03:27 -0500 Subject: [PATCH] Added command blacklist, added ability to toggle command types --- config/commands.json | 7 +++++++ shard.js | 26 ++++++++++++++++++++++---- utils/handler.js | 9 +++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 config/commands.json diff --git a/config/commands.json b/config/commands.json new file mode 100644 index 0000000..5294ada --- /dev/null +++ b/config/commands.json @@ -0,0 +1,7 @@ +{ + "types": { + "classic": true, + "application": true + }, + "blacklist": [] +} \ No newline at end of file diff --git a/shard.js b/shard.js index 5e2ee7b..ebeb3e0 100644 --- a/shard.js +++ b/shard.js @@ -17,6 +17,8 @@ import database from "./utils/database.js"; import { paths } from "./utils/collections.js"; // playing messages const { messages } = JSON.parse(readFileSync(new URL("./config/messages.json", import.meta.url))); +// command config +const { types } = JSON.parse(readFileSync(new URL("./config/commands.json", import.meta.url))); // other stuff import { random } from "./utils/misc.js"; // generate help page @@ -33,6 +35,11 @@ class Shard extends BaseClusterWorker { } async init() { + if (!types.classic && !types.application) { + error("Both classic and application commands are disabled! Please enable at least one command type in config/commands.json."); + this.ipc.totalShutdown(true); + return; + } // register commands and their info const soundStatus = await checkStatus(); log("info", "Attempting to load commands..."); @@ -44,11 +51,17 @@ class Shard extends BaseClusterWorker { error(`Failed to register command from ${commandFile}: ${e}`); } } - const commandArray = await update(this.bot, this.clusterID, this.workerID, this.ipc, soundStatus); + if (types.application) { + const commandArray = await update(this.bot, this.clusterID, this.workerID, this.ipc, soundStatus); + try { + await this.bot.bulkEditCommands(commandArray); + } catch { + log("error", "Failed to send command data to Discord, slash commands may be unavailable."); + } + } log("info", "Finished loading commands."); await database.setup(this.ipc); - await this.bot.bulkEditCommands(commandArray); // register events log("info", "Attempting to load events..."); @@ -56,6 +69,13 @@ class Shard extends BaseClusterWorker { log("log", `Loading event from ${file}...`); const eventArray = file.split("/"); const eventName = eventArray[eventArray.length - 1].split(".")[0]; + if (eventName === "messageCreate" && !types.classic) { + log("warn", `Skipped loading event from ${file} because classic commands are disabled...`); + continue; + } else if (eventName === "interactionCreate" && !types.application) { + log("warn", `Skipped loading event from ${file} because application commands are disabled`); + continue; + } const { default: event } = await import(file); this.bot.on(eventName, event.bind(null, this.bot, this.clusterID, this.workerID, this.ipc)); } @@ -78,8 +98,6 @@ class Shard extends BaseClusterWorker { return this.ipc.broadcast("reloadSuccess"); }); - this.bot.privateChannels.limit = 0; - this.ipc.register("soundreload", async () => { const soundStatus = await checkStatus(); if (!soundStatus) { diff --git a/utils/handler.js b/utils/handler.js index 9be014b..89ceffb 100644 --- a/utils/handler.js +++ b/utils/handler.js @@ -1,6 +1,10 @@ import { paths, commands, info, sounds, categories, aliases as _aliases } from "./collections.js"; import { log } from "./logger.js"; +import { readFileSync } from "fs"; + +const { blacklist } = JSON.parse(readFileSync(new URL("../config/commands.json", import.meta.url))); + let queryValue = 0; // load command into memory @@ -14,6 +18,11 @@ export async function load(client, cluster, worker, ipc, command, soundStatus, s const commandArray = command.split("/"); const commandName = commandArray[commandArray.length - 1].split(".")[0]; + if (blacklist.includes(commandName)) { + log("warn", `Skipped loading blacklisted command ${command}...`); + return; + } + props.init(); paths.set(commandName, command);