From c78741f565a606ebc3fc7496aa2288bab4d17522 Mon Sep 17 00:00:00 2001 From: jacoobes Date: Fri, 4 Feb 2022 15:09:26 -0600 Subject: [PATCH] parse context (works)? --- src/handler/sern/sern.ts | 10 +++++----- src/types/handler/handler.ts | 11 ++++++++--- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/handler/sern/sern.ts b/src/handler/sern/sern.ts index b4fcba9..0a4d9d6 100644 --- a/src/handler/sern/sern.ts +++ b/src/handler/sern/sern.ts @@ -1,4 +1,4 @@ -import type { Context, MessagePackage, Nullable, ParseType, Visibility } from "../../types/handler/handler"; +import type { Arg, Context, MessagePackage, Nullable, ParseType, Visibility } from "../../types/handler/handler"; import { CommandType } from "../../types/handler/handler"; import { Files } from "../utils/readFile" import type { ApplicationCommandOptionData, Awaitable, Client, CommandInteraction, CommandInteractionOptionResolver, Message} from "discord.js"; @@ -66,7 +66,7 @@ export namespace Sern { if(module.mod.type < CommandType.SLASH) return "This is not a slash command"; const context = {text: None, slash: Some(interaction)} - const parsedArgs = module.mod.parse?.(context, "slash", interaction.options) ?? Ok(""); + const parsedArgs = module.mod.parse?.(context, ["slash", interaction.options ] ) ?? Ok(""); if(parsedArgs.err) return parsedArgs.val; const fn = await module.mod.delegate(context, parsedArgs); return fn?.val; @@ -79,7 +79,7 @@ export namespace Sern { } if (module.type === CommandType.SLASH) return `This may be a slash command and not a legacy command` const context = {text: Some(message), slash: None} - const parsedArgs = module.parse?.(context, "text", args) ?? Ok(""); + const parsedArgs = module.parse?.(context, ["text", args] ) ?? Ok(""); if(parsedArgs.err) return parsedArgs.val; let fn = await module.delegate(context, parsedArgs) return fn?.val @@ -128,9 +128,9 @@ export namespace Sern { visibility : Visibility, type: CommandType, delegate : ( eventParams : Context , args: Ok ) => Awaitable | void> - parse? : (ctx: Context, cmdType : K, ...args: ParseType[K] ) => Utils.ArgType + parse? : (ctx: Context, args: ParseType ) => Utils.ArgType } - + } diff --git a/src/types/handler/handler.ts b/src/types/handler/handler.ts index b061215..1881df3 100644 --- a/src/types/handler/handler.ts +++ b/src/types/handler/handler.ts @@ -20,13 +20,18 @@ export enum CommandType { SLASH = 4, } +/// Thanks @cursorsdottsx +export type ParseType = { + [K in keyof T] : T[K] extends unknown ? [k : K, args: T[K] ] : never; +}[keyof T]; + export type Context = { text : Option, slash : Option } -export type ParseType = { - text : [arg: string]; - slash : [SlashOptions] +export interface Arg { + text : string; + slash : SlashOptions }; export type SlashOptions = Omit; \ No newline at end of file