diff --git a/src/handler/logger.ts b/src/handler/logger.ts new file mode 100644 index 0000000..087e52a --- /dev/null +++ b/src/handler/logger.ts @@ -0,0 +1,31 @@ +export class DefaultLogger implements Logger { + + clear () { + console.clear() + } + + log(message: string, e: DefaultEvent) { + console.log(`[${DefaultEvent[e]}] ${message}`) + } + +} + +/** + * @enum {string} + */ +export enum DefaultEvent { + WARNING, + ERROR, + MESSAGE, + INTERACTION, + DATABASE, + INFO +} + +export interface Logger { + + clear () : void; + log(message : string, e: T) : void; + +} + diff --git a/src/handler/sern.ts b/src/handler/sern.ts index 0986027..a720282 100644 --- a/src/handler/sern.ts +++ b/src/handler/sern.ts @@ -5,6 +5,7 @@ import type { possibleOutput } from "../types/handler" import { Ok, Result, None, Some } from "ts-results"; import type * as Utils from "./utils/preprocessors/args"; import { CtxHandler } from "./utils/ctxHandler"; +import { DefaultLogger, Logger } from "./logger"; /** @@ -12,14 +13,23 @@ import { CtxHandler } from "./utils/ctxHandler"; */ export class Handler { private wrapper: Wrapper; + private logger : Logger; /** * @constructor * @param {Wrapper} wrapper Some data that is required to run sern handler */ constructor( wrapper: Wrapper, + logger? : Logger, ) { this.wrapper = wrapper; + logger === undefined + ? this.logger = new DefaultLogger() + : this.logger = logger; + + this.logger.clear(); + + this.wrapper.client .on("ready", async () => { if (this.wrapper.init !== undefined) this.wrapper.init(this); @@ -67,20 +77,24 @@ export class Handler { }); if (module.mod.type < CommandType.SLASH) return "This is not a slash command"; - const context = { text: None, slash: Some(interaction) } + const context = { message: None, interaction: Some(interaction) } 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; } + private emitEvent() { + + } + private async commandResult(module: Module | undefined, message: Message, args: string): Promise { if (module === undefined) return "Unknown legacy command"; if (module.visibility === "private" && message.guildId !== this.privateServerId) { return "This command is not availible in this guild!" } 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 context = { message: Some(message), interaction: None } const parsedArgs = module.parse?.(context, ["text", args]) ?? Ok(""); if (parsedArgs.err) return parsedArgs.val; const fn = await module.delegate(context, parsedArgs) @@ -134,7 +148,7 @@ export interface Wrapper { readonly prefix: string, readonly commands: string init?: (handler: Handler) => void, - readonly privateServerId: string + readonly privateServerId: string, } /** * @interface - Modules that are used in command files @@ -145,7 +159,7 @@ export interface Module { visibility: Visibility, type: CommandType, delegate: (eventParams: Context, args: Ok) => Awaitable | void> - parse?: (ctx: Context, args: ParseType) => Utils.ArgType + parse?: (ctx: Context, args: Arg) => Utils.ArgType } /** * @enum { number }; diff --git a/src/handler/utils/readFile.ts b/src/handler/utils/readFile.ts index 2bb79bc..3756563 100644 --- a/src/handler/utils/readFile.ts +++ b/src/handler/utils/readFile.ts @@ -33,7 +33,7 @@ export async function registerModules(handler: Sern.Handler): Promise { switch (mod.type) { case 1: Commands.set(name.substring(0, name.length - 3), { mod, options: [] }); break; case 2: - case 1 | 2: { + case (1 | 2): { const options = ((await import(absPath)).options as ApplicationCommandOptionData[]) Commands.set(name.substring(0, name.length - 3), { mod, options: options ?? [] }); } break; diff --git a/src/types/handler.ts b/src/types/handler.ts index cbf429f..dc76fae 100644 --- a/src/types/handler.ts +++ b/src/types/handler.ts @@ -4,9 +4,8 @@ import type * as Sern from "../handler/sern" export type Visibility = "private" | "public" //Anything that can be sent in a `#send` or `#reply` -export type possibleOutput = string | MessagePayload & MessageOptions; +export type possibleOutput = T | MessagePayload & MessageOptions; export type Nullable = T | null; - export type delegate = Sern.Module["delegate"] /// Thanks @cursorsdottsx @@ -16,12 +15,9 @@ export type ParseType = { // A Sern.Module["delegate"] will carry a Context Parameter export type Context = { - text: Option, - slash: Option -} -export interface Arg { - text: string; - slash: SlashOptions + message: Option, + interaction: Option } +export type Arg = ParseType<{text : string, slash : SlashOptions}> // TypeAlias for interaction.options export type SlashOptions = Omit; \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index fc47586..968a72f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,6 @@ { "compilerOptions": { + "experimentalDecorators": true, "resolveJsonModule": true, "target": "esnext", "module": "commonjs",