mirror of
https://github.com/JonnyBro/JaBa.git
synced 2025-01-19 17:03:47 +05:00
feat(commandhandler): added support validations when using command
This commit is contained in:
parent
fb060a35f4
commit
6c7f892ae4
4 changed files with 58 additions and 1 deletions
|
@ -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 {
|
||||
|
|
18
src/handlers/command-handler/validations/devOnly.ts
Normal file
18
src/handlers/command-handler/validations/devOnly.ts
Normal 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;
|
||||
}
|
||||
}
|
3
src/handlers/command-handler/validations/index.ts
Normal file
3
src/handlers/command-handler/validations/index.ts
Normal file
|
@ -0,0 +1,3 @@
|
|||
import devOnly from "./devOnly.js";
|
||||
|
||||
export default [devOnly];
|
8
src/types.d.ts
vendored
8
src/types.d.ts
vendored
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue