diff --git a/src/handler/events/readyEvent.ts b/src/handler/events/readyEvent.ts index ec2cb21..6ea4a67 100644 --- a/src/handler/events/readyEvent.ts +++ b/src/handler/events/readyEvent.ts @@ -2,8 +2,10 @@ import { concatMap, first, from, fromEvent, map, pipe, tap } from "rxjs"; import { basename } from 'path'; import * as Files from '../utilities/readFile'; import type Wrapper from '../structures/wrapper'; -import type { Command } from "../structures/commands/command"; -import type { ApplicationCommandOptionData } from "discord.js"; +import type { Modules } from "../structures/structxports"; +import type { HandlerCallback, ModuleDefs, ModuleHandlers, ModuleStates, ModuleType } from "../structures/commands/moduleHandler"; +import { CommandType } from "../sern"; + export const onReady = ( wrapper : Wrapper ) => { const { client, init, commands, } = wrapper; fromEvent(client, 'ready') @@ -20,17 +22,32 @@ export const onReady = ( wrapper : Wrapper ) => { .subscribe( () => console.log(Files.Commands)); } -function setCommands ( { mod, absPath } : { mod : Command, absPath : string } ) { - const options = mod.getOptions() ?? [] as ApplicationCommandOptionData[]; - const name = mod.getName() ?? Files.fmtFileName(basename(absPath)); +const handler = ( name : string ) => + ({ + [CommandType.TEXT] : mod => { + mod.alias.forEach ( a => Files.Alias.set(a,mod)); + Files.Commands.set( name, mod ); + }, + [CommandType.SLASH]: mod => { + Files.Commands.set( name, mod); + }, + [CommandType.BOTH] : mod => { + Files.Commands.set ( name, mod); + mod.alias.forEach (a => Files.Alias.set(a, mod)); + } + }) as ModuleHandlers; - mod.getAlias()?.forEach( n => Files.Alias.set( n, { mod, options } )); +const registerModules = (name : string, mod : ModuleStates[T]) => + (handler(name)[mod.type] as HandlerCallback)(mod); - Files.Commands.set(name, { mod, options }); +function setCommands ( { mod, absPath } : { mod : Modules.Module, absPath : string } ) { + const name = mod.name ?? Files.fmtFileName(basename(absPath)); + registerModules(name, mod); } async function createCommandCache( - arr: Promise<{mod: Command, absPath: string}[]> + arr: Promise<{mod: Modules.Module, absPath: string}[]> ) { + console.log(await arr); from(await arr).subscribe ( setCommands ); } diff --git a/src/handler/structures/commands/module.ts b/src/handler/structures/commands/module.ts index a07d4ba..ce5a458 100644 --- a/src/handler/structures/commands/module.ts +++ b/src/handler/structures/commands/module.ts @@ -1,4 +1,4 @@ -import type { ApplicationCommandOptionData, Awaitable } from "discord.js"; +import type { ApplicationCommandOptionData, Awaitable, PartialWebhookMixin } from "discord.js"; import type { possibleOutput } from "../../../types/handler"; import type { CommandType } from "../../sern"; @@ -15,7 +15,7 @@ export type Slash = { type : CommandType.SLASH; options : ApplicationCommandOpti export type Both = { type : CommandType.BOTH; alias : string[] | []; options : ApplicationCommandOptionData[] | [] } export type Module = - (BaseModule & Slash) | (BaseModule & Both) | (BaseModule & Text); + (BaseModule & Slash) | (BaseModule & Both) | (BaseModule & Text); diff --git a/src/handler/structures/commands/moduleHandler.ts b/src/handler/structures/commands/moduleHandler.ts index 00992c0..c30c239 100644 --- a/src/handler/structures/commands/moduleHandler.ts +++ b/src/handler/structures/commands/moduleHandler.ts @@ -15,8 +15,7 @@ export type ModuleType = keyof ModuleDefs; // The keys mapped to a constructed union with its type export type ModuleStates = { [ K in ModuleType ] : { type : K } & ModuleDefs[K] }; // A handler callback that is called on each ModuleDef -export type HandlerCallback= ( params : ModuleStates[K] ) => unknown; +export type HandlerCallback = ( params : ModuleStates[K] ) => unknown; //An object that acts as the mapped object to handler export type ModuleHandlers = { [K in ModuleType] : HandlerCallback }; - diff --git a/src/handler/utilities/messageHelpers.ts b/src/handler/utilities/messageHelpers.ts index d22e0ad..390842f 100644 --- a/src/handler/utilities/messageHelpers.ts +++ b/src/handler/utilities/messageHelpers.ts @@ -1,4 +1,4 @@ -import type { Message } from 'discord.js'; +import { ChannelType, Message } from 'discord.js'; /** * @param message The message object @@ -8,7 +8,7 @@ import type { Message } from 'discord.js'; */ export function isNotFromDM ( message: Message ) { - return message.channel.type !== 'DM'; + return message.channel.type !== ChannelType.DM; } /** diff --git a/src/handler/utilities/readFile.ts b/src/handler/utilities/readFile.ts index adc4dfb..00d2e3f 100644 --- a/src/handler/utilities/readFile.ts +++ b/src/handler/utilities/readFile.ts @@ -2,15 +2,11 @@ import type { ApplicationCommandOptionData } from 'discord.js'; import { readdirSync, statSync } from 'fs'; import { join } from 'path'; -import type { Command } from '../structures/commands/command'; +import type { Module } from '../structures/commands/module'; -export type CommandVal = { - mod: Command; - options: ApplicationCommandOptionData[]; -}; -export const Commands = new Map(); -export const Alias = new Map>(); +export const Commands = new Map(); +export const Alias = new Map(); // Courtesy @Townsy45 function readPath(dir: string, arrayOfFiles: string[] = []): string[] { @@ -37,13 +33,13 @@ export const fmtFileName = (n: string) => n.substring(0, n.length - 3); export async function buildData(commandDir: string ): Promise< { - mod: Command; + mod: Module; absPath: string; }[] > { return Promise.all( getCommands(commandDir).map( async (absPath) => { - return { mod: (await import(absPath)).default as Command, absPath }; + return { mod: (await import(absPath)).module as Module, absPath }; }), ); } diff --git a/src/types/handler.ts b/src/types/handler.ts index 71a576b..f55845c 100644 --- a/src/types/handler.ts +++ b/src/types/handler.ts @@ -7,13 +7,13 @@ import type { Awaitable, } from 'discord.js'; -import type Module from '../handler/structures/module'; +import type { Modules } from '../handler/structures/structxports'; export type Visibility = 'private' | 'public'; // Anything that can be sent in a `#send` or `#reply` export type possibleOutput = T | (MessagePayload & MessageOptions); -export type execute = Module['execute']; +export type execute = Modules.Module['execute']; // Thanks @cursorsdottsx export type ParseType = {