feat(commandhandler): added support validations when using command

This commit is contained in:
Slincnik 2025-01-14 20:21:34 +03:00
parent fb060a35f4
commit 6c7f892ae4
No known key found for this signature in database
4 changed files with 58 additions and 1 deletions

View file

@ -4,11 +4,14 @@ import { getFilePaths } from "@/utils/get-path.js";
import { toFileURL } from "@/utils/resolve-file.js";
import registerCommands from "./functions/registerCommands.js";
import { ExtendedClient } from "@/structures/client.js";
import { CommandFileObject } from "@//types.js";
import { BuiltInValidation, CommandFileObject } from "@/types.js";
import builtInValidationsFunctions from "./validations/index.js";
export class CommandHandler {
client: ExtendedClient;
commands: CommandFileObject[] = [];
builtInValidations: BuiltInValidation[] = [];
constructor(client: ExtendedClient) {
this.client = client;
}
@ -16,6 +19,8 @@ export class CommandHandler {
async init() {
await this.#buildCommands();
this.buildBuiltInValidations();
await registerCommands({
client: this.client,
commands: this.commands,
@ -45,6 +50,12 @@ export class CommandHandler {
}
}
buildBuiltInValidations() {
for (const builtInValidationFunction of builtInValidationsFunctions) {
this.builtInValidations.push(builtInValidationFunction);
}
}
handleCommands() {
this.client.on("interactionCreate", async interaction => {
if (!interaction.isChatInputCommand() && !interaction.isAutocomplete()) return;
@ -58,6 +69,23 @@ export class CommandHandler {
// Skip if autocomplete handler is not defined
if (isAutocomplete && !targetCommand.autocompleteRun) return;
let canRun = true;
for (const validation of this.builtInValidations) {
const stopValidationLoop = validation({
targetCommand,
interaction,
client: this.client,
});
if (stopValidationLoop) {
canRun = false;
break;
}
}
if (!canRun) return;
const command = targetCommand[isAutocomplete ? "autocompleteRun" : "run"]!;
try {

View file

@ -0,0 +1,18 @@
import { BuiltInValidationParams } from "@/types.js";
export default function ({ interaction, targetCommand, client }: BuiltInValidationParams) {
if (interaction.isAutocomplete()) return;
const devGuildsIds = client.configService.get<string[]>("devGuildsIds");
if (!targetCommand.options?.devOnly) return;
if (interaction.inGuild() && !devGuildsIds.includes(interaction.guildId)) {
interaction.reply({
content: "❌ This command is only available in development servers.",
ephemeral: true,
});
return true;
}
}

View file

@ -0,0 +1,3 @@
import devOnly from "./devOnly.js";
export default [devOnly];

8
src/types.d.ts vendored
View file

@ -19,6 +19,14 @@ export type cacheRemindsData = {
reminds: UserReminds[];
};
export type BuiltInValidationParams = {
targetCommand: CommandFileObject;
interaction: Interaction<CacheType>;
client: ExtendedClient;
};
export type BuiltInValidation = ({}: BuiltInValidationParams) => boolean | void;
export type CronTaskData = {
name: string;
schedule: string;