Skip to content

Commit

Permalink
opt in for simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobes committed Aug 11, 2024
1 parent cb818ef commit 076f599
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
12 changes: 6 additions & 6 deletions src/core/structures/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -84,21 +84,21 @@ 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<Message, never> } {
return isOk(this.ctx);
return this.ctx.ok;
}

public isSlash(): this is Context & { ctx: Result<never, ChatInputCommandInteraction> } {
return !this.isMessage();
}

get interaction(): ChatInputCommandInteraction {
if(!isOk(this.ctx)) {
if(!this.ctx.ok) {
return this.ctx.error;
}
throw Error(SernError.MismatchEvent);
Expand All @@ -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 };
Expand Down
9 changes: 1 addition & 8 deletions src/core/structures/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ export type Result<Ok, Err> =
export const Ok = <Ok>(value: Ok) => ({ ok: true, value } as const);
export const Err = <Err>(error: Err) => ({ ok: false, error } as const);

export const isOk = <O, E>(r: Result<O,E>): r is Result<O, E> & { ok: true; value: O } => {
return r.ok;
}
export const isErr = <O, E>(r: Result<O, E>) : r is Result<O, E> & { ok: false; error: E } => {
return !isOk(r);
}

export const val = <O, E>(r: Result<O, E>) => isOk(r) ? r.value : r.error;
export const val = <O, E>(r: Result<O, E>) => r.ok ? r.value : r.error;
export const EMPTY_ERR = Err(undefined);

/**
Expand Down
12 changes: 6 additions & 6 deletions src/handlers/event-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -44,7 +44,7 @@ interface ExecutePayload {

export const filterTap = <K, R>(onErr: (e: R) => void): OperatorFunction<Result<K, R>, K> =>
concatMap(result => {
if(isOk(result)) {
if(result.ok){
return of(result.value)
}
onErr(result.error);
Expand Down Expand Up @@ -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;
}
Expand All @@ -207,7 +207,7 @@ export function createResultResolver<Output>(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;
Expand All @@ -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));
Expand All @@ -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)) {
Expand Down

0 comments on commit 076f599

Please sign in to comment.