mirror of
https://github.com/JonnyBro/JaBa.git
synced 2025-01-19 17:03:47 +05:00
feat(handlers): added support load dev commands in dev servers
This commit is contained in:
parent
d04d304632
commit
78fca15044
3 changed files with 63 additions and 14 deletions
|
@ -7,11 +7,7 @@
|
||||||
"clientSecret": "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
|
"clientSecret": "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
|
||||||
},
|
},
|
||||||
"youtubeCookie": "XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
|
"youtubeCookie": "XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
|
||||||
"support": {
|
"devGuildsIds": [],
|
||||||
"id": "123456789098765432",
|
|
||||||
"logs": "123456789098765432",
|
|
||||||
"invite": "https://discord.gg/discord"
|
|
||||||
},
|
|
||||||
"embed": {
|
"embed": {
|
||||||
"color": "#00FF00",
|
"color": "#00FF00",
|
||||||
"footer": {
|
"footer": {
|
||||||
|
|
|
@ -2,10 +2,24 @@ import logger from "../../../helpers/logger.js";
|
||||||
import differentCommands from "../utils/differentcommands.js";
|
import differentCommands from "../utils/differentcommands.js";
|
||||||
|
|
||||||
export default async function registerCommands(props) {
|
export default async function registerCommands(props) {
|
||||||
const globalCommands = props.commands.filter(cmd => !cmd.options?.devOnly);
|
props.client.once("ready", () => handleRegistration(props.client, props.commands));
|
||||||
props.client.once("ready", () => registerGlobalCommands(props.client, globalCommands));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 registerGlobalCommands = async (client, commands) => {
|
||||||
const appCommandsManager = client.application.commands;
|
const appCommandsManager = client.application.commands;
|
||||||
await appCommandsManager.fetch();
|
await appCommandsManager.fetch();
|
||||||
|
@ -26,3 +40,49 @@ const registerGlobalCommands = async (client, commands) => {
|
||||||
|
|
||||||
logger.log("Registered global 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)`);
|
||||||
|
};
|
||||||
|
|
|
@ -3,7 +3,6 @@ import logger from "../../helpers/logger.js";
|
||||||
import { getFilePaths } from "../../utils/index.js";
|
import { getFilePaths } from "../../utils/index.js";
|
||||||
import { toFileURL } from "../../utils/resolve-file.js";
|
import { toFileURL } from "../../utils/resolve-file.js";
|
||||||
import registerCommands from "./functions/registerCommands.js";
|
import registerCommands from "./functions/registerCommands.js";
|
||||||
import { replyError } from "../../helpers/extenders.js";
|
|
||||||
|
|
||||||
export class CommandHandler {
|
export class CommandHandler {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
|
@ -56,12 +55,6 @@ export class CommandHandler {
|
||||||
|
|
||||||
if (!targetCommand) return;
|
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
|
// Skip if autocomplete handler is not defined
|
||||||
if (isAutocomplete && !targetCommand.autocompleteRun) return;
|
if (isAutocomplete && !targetCommand.autocompleteRun) return;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue