mirror of
https://github.com/JonnyBro/JaBa.git
synced 2025-01-31 22:54:10 +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 { toFileURL } from "@/utils/resolve-file.js";
|
||||||
import registerCommands from "./functions/registerCommands.js";
|
import registerCommands from "./functions/registerCommands.js";
|
||||||
import { ExtendedClient } from "@/structures/client.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 {
|
export class CommandHandler {
|
||||||
client: ExtendedClient;
|
client: ExtendedClient;
|
||||||
commands: CommandFileObject[] = [];
|
commands: CommandFileObject[] = [];
|
||||||
|
builtInValidations: BuiltInValidation[] = [];
|
||||||
|
|
||||||
constructor(client: ExtendedClient) {
|
constructor(client: ExtendedClient) {
|
||||||
this.client = client;
|
this.client = client;
|
||||||
}
|
}
|
||||||
|
@ -16,6 +19,8 @@ export class CommandHandler {
|
||||||
async init() {
|
async init() {
|
||||||
await this.#buildCommands();
|
await this.#buildCommands();
|
||||||
|
|
||||||
|
this.buildBuiltInValidations();
|
||||||
|
|
||||||
await registerCommands({
|
await registerCommands({
|
||||||
client: this.client,
|
client: this.client,
|
||||||
commands: this.commands,
|
commands: this.commands,
|
||||||
|
@ -45,6 +50,12 @@ export class CommandHandler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buildBuiltInValidations() {
|
||||||
|
for (const builtInValidationFunction of builtInValidationsFunctions) {
|
||||||
|
this.builtInValidations.push(builtInValidationFunction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
handleCommands() {
|
handleCommands() {
|
||||||
this.client.on("interactionCreate", async interaction => {
|
this.client.on("interactionCreate", async interaction => {
|
||||||
if (!interaction.isChatInputCommand() && !interaction.isAutocomplete()) return;
|
if (!interaction.isChatInputCommand() && !interaction.isAutocomplete()) return;
|
||||||
|
@ -58,6 +69,23 @@ export class CommandHandler {
|
||||||
// Skip if autocomplete handler is not defined
|
// Skip if autocomplete handler is not defined
|
||||||
if (isAutocomplete && !targetCommand.autocompleteRun) return;
|
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"]!;
|
const command = targetCommand[isAutocomplete ? "autocompleteRun" : "run"]!;
|
||||||
|
|
||||||
try {
|
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[];
|
reminds: UserReminds[];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type BuiltInValidationParams = {
|
||||||
|
targetCommand: CommandFileObject;
|
||||||
|
interaction: Interaction<CacheType>;
|
||||||
|
client: ExtendedClient;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type BuiltInValidation = ({}: BuiltInValidationParams) => boolean | void;
|
||||||
|
|
||||||
export type CronTaskData = {
|
export type CronTaskData = {
|
||||||
name: string;
|
name: string;
|
||||||
schedule: string;
|
schedule: string;
|
||||||
|
|
Loading…
Reference in a new issue