added default logger

This commit is contained in:
jacoobes
2022-02-07 23:55:16 -06:00
parent 07018b6d09
commit 09bf5a1bfe
5 changed files with 55 additions and 13 deletions

31
src/handler/logger.ts Normal file
View File

@@ -0,0 +1,31 @@
export class DefaultLogger implements Logger<DefaultEvent> {
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<T> {
clear () : void;
log(message : string, e: T) : void;
}

View File

@@ -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<unknown>;
/**
* @constructor
* @param {Wrapper} wrapper Some data that is required to run sern handler
*/
constructor(
wrapper: Wrapper,
logger? : Logger<unknown>,
) {
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<unknown> | undefined, message: Message, args: string): Promise<possibleOutput | undefined> {
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<T = void> {
visibility: Visibility,
type: CommandType,
delegate: (eventParams: Context, args: Ok<T>) => Awaitable<Result<possibleOutput, string> | void>
parse?: (ctx: Context, args: ParseType<Arg>) => Utils.ArgType<T>
parse?: (ctx: Context, args: Arg) => Utils.ArgType<T>
}
/**
* @enum { number };

View File

@@ -33,7 +33,7 @@ export async function registerModules(handler: Sern.Handler): Promise<void> {
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;

View File

@@ -4,9 +4,8 @@ import type * as Sern from "../handler/sern"
export type Visibility = "private" | "public"
//Anything that can be sent in a `<TextChannel>#send` or `<CommandInteraction>#reply`
export type possibleOutput = string | MessagePayload & MessageOptions;
export type possibleOutput<T = string> = T | MessagePayload & MessageOptions;
export type Nullable<T> = T | null;
export type delegate = Sern.Module<unknown>["delegate"]
/// Thanks @cursorsdottsx
@@ -16,12 +15,9 @@ export type ParseType<T> = {
// A Sern.Module["delegate"] will carry a Context Parameter
export type Context = {
text: Option<Message>,
slash: Option<CommandInteraction>
}
export interface Arg {
text: string;
slash: SlashOptions
message: Option<Message>,
interaction: Option<CommandInteraction>
}
export type Arg = ParseType<{text : string, slash : SlashOptions}>
// TypeAlias for interaction.options
export type SlashOptions = Omit<CommandInteractionOptionResolver, "getMessage" | "getFocused">;

View File

@@ -1,5 +1,6 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"resolveJsonModule": true,
"target": "esnext",
"module": "commonjs",