diff --git a/src/handler/events/messageEvent.ts b/src/handler/events/messageEvent.ts index b623148..aba4052 100644 --- a/src/handler/events/messageEvent.ts +++ b/src/handler/events/messageEvent.ts @@ -1,7 +1,10 @@ import type { Message } from "discord.js"; -import { filter, fromEvent, Observable } from "rxjs"; +import { map, filter, fromEvent, Observable, of, concatMap } from "rxjs"; +import { None, Some } from "ts-results"; +import Context from "../structures/context"; import type Wrapper from "../structures/wrapper"; -import { isNotFromDM, isNotFromBot, hasPrefix } from "../utilities/messageHelpers"; +import { isNotFromDM, isNotFromBot, hasPrefix, fmt } from "../utilities/messageHelpers"; +import * as Files from '../utilities/readFile'; export const onMessageCreate = ( wrapper : Wrapper) => { const { client, defaultPrefix } = wrapper; @@ -9,9 +12,21 @@ export const onMessageCreate = ( wrapper : Wrapper) => { .pipe ( filter( isNotFromBot ), filter( isNotFromDM ), - filter(m => hasPrefix(m, defaultPrefix)), + filter( m => hasPrefix(m, defaultPrefix)), + concatMap ( m => of(fmt(m, defaultPrefix)) + .pipe ( + map(([prefix, ...args ]) =>{ + return [Files.Commands.get(prefix) ?? Files.Alias.get(prefix), new Context(Some(m), None), args ] as const; + }), + filter( ([mod]) => mod !== undefined), + map ( ([mod, ctx, args ]) => { + const parsedArgs = mod!.parse?.(ctx, args); + return mod!.execute(ctx, parsedArgs); + }) + ) + ) - ).subscribe(console.log) + ).subscribe() } diff --git a/src/handler/events/readyEvent.ts b/src/handler/events/readyEvent.ts index 6ea4a67..1084da9 100644 --- a/src/handler/events/readyEvent.ts +++ b/src/handler/events/readyEvent.ts @@ -47,7 +47,7 @@ function setCommands ( { mod, absPath } : { mod : Modules.Module, absPath : stri async function createCommandCache( 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 ce5a458..40c01c8 100644 --- a/src/handler/structures/commands/module.ts +++ b/src/handler/structures/commands/module.ts @@ -1,27 +1,19 @@ -import type { ApplicationCommandOptionData, Awaitable, PartialWebhookMixin } from "discord.js"; -import type { possibleOutput } from "../../../types/handler"; +import type { ApplicationCommandOptionData, Awaitable } from "discord.js"; +import type { parseArgs, possibleOutput } from "../../../types/handler"; import type { CommandType } from "../../sern"; +import type Context from "../context"; export interface BaseModule { name? : string; description : string; - execute() : Awaitable + execute(ctx: Context, args: unknown) : Awaitable } - -export type Text = { type : CommandType.TEXT; alias : string[] | [] }; -export type Slash = { type : CommandType.SLASH; options : ApplicationCommandOptionData[] | [] }; -export type Both = { type : CommandType.BOTH; alias : string[] | []; options : ApplicationCommandOptionData[] | [] } +export type Text = { type : CommandType.TEXT; alias : string[] | [], parse? : parseArgs }; +export type Slash = { type : CommandType.SLASH; options : ApplicationCommandOptionData[] | [], parse? : parseArgs }; +export type Both = { type : CommandType.BOTH; alias : string[] | []; options : ApplicationCommandOptionData[] | [], parse? : parseArgs } export type Module = (BaseModule & Slash) | (BaseModule & Both) | (BaseModule & Text); - - - - - - - - diff --git a/src/types/handler.ts b/src/types/handler.ts index f55845c..0c87efd 100644 --- a/src/types/handler.ts +++ b/src/types/handler.ts @@ -7,10 +7,10 @@ import type { Awaitable, } from 'discord.js'; -import type { Modules } from '../handler/structures/structxports'; +import type { Context, Modules } from '../handler/structures/structxports'; export type Visibility = 'private' | 'public'; - +export type parseArgs = ( ctx: Context, args : string[] ) => T | possibleOutput; // Anything that can be sent in a `#send` or `#reply` export type possibleOutput = T | (MessagePayload & MessageOptions); export type execute = Modules.Module['execute'];