diff --git a/src/core/structures/context.ts b/src/core/structures/context.ts index 0adb4698..8d244a7b 100644 --- a/src/core/structures/context.ts +++ b/src/core/structures/context.ts @@ -8,7 +8,7 @@ import type { Snowflake, User, } from 'discord.js'; -import { Result, Ok, Err, val, isOk } from './result'; +import { Result, Ok, Err, val } from './result'; import * as assert from 'assert'; import type { ReplyOptions } from '../../types/utility'; import { fmt } from '../functions' @@ -54,7 +54,7 @@ export class Context { * else, interaction.user */ public get user(): User { - if(isOk(this.ctx)) { + if(this.ctx.ok) { return this.ctx.value.author; } else { return this.ctx.error.user; @@ -84,13 +84,13 @@ export class Context { } get message(): Message { - if(isOk(this.ctx)) { + if(this.ctx.ok) { return this.ctx.value; } throw Error(SernError.MismatchEvent); } public isMessage(): this is Context & { ctx: Result } { - return isOk(this.ctx); + return this.ctx.ok; } public isSlash(): this is Context & { ctx: Result } { @@ -98,7 +98,7 @@ export class Context { } get interaction(): ChatInputCommandInteraction { - if(!isOk(this.ctx)) { + if(!this.ctx.ok) { return this.ctx.error; } throw Error(SernError.MismatchEvent); @@ -114,7 +114,7 @@ export class Context { } public async reply(content: ReplyOptions) { - if(isOk(this.ctx)) { + if(this.ctx.ok) { return this.ctx.value.reply(content as MessageReplyOptions) } else { interface FetchReply { fetchReply: true }; diff --git a/src/core/structures/result.ts b/src/core/structures/result.ts index 9b36b80f..06d579f4 100644 --- a/src/core/structures/result.ts +++ b/src/core/structures/result.ts @@ -5,14 +5,7 @@ export type Result = export const Ok = (value: Ok) => ({ ok: true, value } as const); export const Err = (error: Err) => ({ ok: false, error } as const); -export const isOk = (r: Result): r is Result & { ok: true; value: O } => { - return r.ok; -} -export const isErr = (r: Result) : r is Result & { ok: false; error: E } => { - return !isOk(r); -} - -export const val = (r: Result) => isOk(r) ? r.value : r.error; +export const val = (r: Result) => r.ok ? r.value : r.error; export const EMPTY_ERR = Err(undefined); /** diff --git a/src/handlers/event-utils.ts b/src/handlers/event-utils.ts index b093c700..18492ac2 100644 --- a/src/handlers/event-utils.ts +++ b/src/handlers/event-utils.ts @@ -8,7 +8,7 @@ import { import * as Id from '../core/id' import type { Emitter, ErrorHandling, Logging } from '../core/interfaces'; import { SernError } from '../core/structures/enums' -import { EMPTY_ERR, Err, Ok, Result, isErr, isOk, wrapAsync } from '../core/structures/result'; +import { EMPTY_ERR, Err, Ok, Result, wrapAsync } from '../core/structures/result'; import type { UnpackedDependencies } from '../types/utility'; import type { CommandModule, Module, Processed } from '../types/core-modules'; import * as assert from 'node:assert'; @@ -44,7 +44,7 @@ interface ExecutePayload { export const filterTap = (onErr: (e: R) => void): OperatorFunction, K> => concatMap(result => { - if(isOk(result)) { + if(result.ok){ return of(result.value) } onErr(result.error); @@ -182,7 +182,7 @@ export function createMessageHandler( export function executeModule(emitter: Emitter, { module, args }: ExecutePayload) { return from(wrapAsync(async () => module.execute(...args))) .pipe(concatMap(result => { - if (isOk(result)) { + if (result.ok){ emitter.emit('module.activate', resultPayload('success', module)); return EMPTY; } @@ -207,7 +207,7 @@ export function createResultResolver(config: { return async (payload: ExecutePayload) => { const task = await callPlugins(payload); if (!task) throw Error("Plugin did not return anything."); - if(isErr(task)) { + if(!task.ok) { onStop?.(payload.module, String(task.error)); } else { return onNext(payload, task.value) as Output; @@ -226,7 +226,7 @@ export async function callInitPlugins(_module: Module, deps: Dependencies, emit? for(const plugin of module.plugins ?? []) { const result = await plugin.execute({ module, absPath: module.meta.absPath, deps }); if (!result) throw Error("Plugin did not return anything. " + inspect(plugin, false, Infinity, true)); - if(isErr(result)) { + if(!result.ok) { if(emit) { emitter?.emit('module.register', resultPayload('failure', module, result.error ?? SernError.PluginFailure)); @@ -241,7 +241,7 @@ export async function callPlugins({ args, module, deps, params }: ExecutePayload let state = {}; for(const plugin of module.onEvent??[]) { const result = await plugin.execute(...args, { state, deps, params, type: module.type }); - if(isErr(result)) { + if(!result.ok) { return result; } if(isObject(result.value)) {