feat(handlers): added support load dev commands in dev servers

This commit is contained in:
Slincnik 2025-01-11 14:54:25 +03:00
parent d04d304632
commit 78fca15044
No known key found for this signature in database
3 changed files with 63 additions and 14 deletions

View file

@ -7,11 +7,7 @@
"clientSecret": "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
},
"youtubeCookie": "XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"support": {
"id": "123456789098765432",
"logs": "123456789098765432",
"invite": "https://discord.gg/discord"
},
"devGuildsIds": [],
"embed": {
"color": "#00FF00",
"footer": {

View file

@ -2,10 +2,24 @@ import logger from "../../../helpers/logger.js";
import differentCommands from "../utils/differentcommands.js";
export default async function registerCommands(props) {
const globalCommands = props.commands.filter(cmd => !cmd.options?.devOnly);
props.client.once("ready", () => registerGlobalCommands(props.client, globalCommands));
props.client.once("ready", () => handleRegistration(props.client, props.commands));
}
const handleRegistration = async (client, commands) => {
const devOnlyCommands = commands.filter(cmd => cmd.options?.devOnly);
const globalCommands = commands.filter(cmd => !cmd.options?.devOnly);
const devGuildsIds = client.configService.get("devGuildsIds");
await registerGlobalCommands(client, globalCommands);
await registerDevCommands(client, devOnlyCommands, devGuildsIds);
};
/**
*
* @param {import("../../../structures/client.js").ExtendedClient} client
* @param {*} commands
*/
const registerGlobalCommands = async (client, commands) => {
const appCommandsManager = client.application.commands;
await appCommandsManager.fetch();
@ -26,3 +40,49 @@ const registerGlobalCommands = async (client, commands) => {
logger.log("Registered global commands");
};
/**
*
* @param {import("../../../structures/client.js").ExtendedClient} client
* @param {*} commands
*/
const registerDevCommands = async (client, commands, guildsIds) => {
const devGuilds = [];
for (const guildId of guildsIds) {
const guild = client.guilds.cache.get(guildId) || (await client.guilds.fetch(guildId));
if (!guild) {
logger.error(`Could not register dev commands, guild ${guildId} not found`);
continue;
}
devGuilds.push(guild);
}
const guildCommandsManagers = [];
for (const guild of devGuilds) {
const guildCommandsManager = guild.commands;
await guildCommandsManager.fetch();
guildCommandsManagers.push(guildCommandsManager);
}
await Promise.all(
commands.map(async ({ data }) => {
guildCommandsManagers.map(async guildCommands => {
const targetCommand = guildCommands.cache.find(cmd => cmd.name === data.name);
if (targetCommand && differentCommands(targetCommand, data)) {
await targetCommand.edit(data).catch(() => logger.error(`Failed to update command: ${data.name} in ${guildCommands.guild.name} server`));
logger.log(`Edited command globally: ${data.name}`);
} else if (!targetCommand) {
await guildCommands.create(data).catch(() => logger.error(`Failed to register command: ${data.name} in ${guildCommands.guild.name} server`));
}
});
}),
);
logger.log(`Registered dev commands in ${devGuilds.length} server(s)`);
};

View file

@ -3,7 +3,6 @@ import logger from "../../helpers/logger.js";
import { getFilePaths } from "../../utils/index.js";
import { toFileURL } from "../../utils/resolve-file.js";
import registerCommands from "./functions/registerCommands.js";
import { replyError } from "../../helpers/extenders.js";
export class CommandHandler {
constructor(client) {
@ -56,12 +55,6 @@ export class CommandHandler {
if (!targetCommand) return;
const ownerId = this.client.configService.get("owner.id");
if (targetCommand.data.ownerOnly && interaction.user.id !== ownerId) {
return replyError(interaction, "misc:OWNER_ONLY", null, { ephemeral: true });
}
// Skip if autocomplete handler is not defined
if (isAutocomplete && !targetCommand.autocompleteRun) return;