mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
refactor: remove ts-results-es (#366)
* remove tsresultses * remove test since it uses external api * opt in for simpler * add more debug information Signed-off-by: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> * add more debug information Signed-off-by: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> * clean up if else --------- Signed-off-by: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com>
This commit is contained in:
@@ -39,8 +39,7 @@
|
|||||||
"callsites": "^3.1.0",
|
"callsites": "^3.1.0",
|
||||||
"cron": "^3.1.7",
|
"cron": "^3.1.7",
|
||||||
"deepmerge": "^4.3.1",
|
"deepmerge": "^4.3.1",
|
||||||
"rxjs": "^7.8.0",
|
"rxjs": "^7.8.0"
|
||||||
"ts-results-es": "^4.1.0"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@faker-js/faker": "^8.0.1",
|
"@faker-js/faker": "^8.0.1",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { CommandType, PluginType } from './structures/enums';
|
import { CommandType, PluginType } from './structures/enums';
|
||||||
import type { Plugin, PluginResult, CommandArgs, InitArgs } from '../types/core-plugin';
|
import type { Plugin, PluginResult, CommandArgs, InitArgs } from '../types/core-plugin';
|
||||||
import { Err, Ok } from 'ts-results-es';
|
import { Err, Ok } from './structures/result';
|
||||||
|
|
||||||
export function makePlugin<V extends unknown[]>(
|
export function makePlugin<V extends unknown[]>(
|
||||||
type: PluginType,
|
type: PluginType,
|
||||||
|
|||||||
@@ -8,8 +8,7 @@ import type {
|
|||||||
Snowflake,
|
Snowflake,
|
||||||
User,
|
User,
|
||||||
} from 'discord.js';
|
} from 'discord.js';
|
||||||
import { CoreContext } from '../structures/core-context';
|
import { Result, Ok, Err, val } from './result';
|
||||||
import { Result, Ok, Err } from 'ts-results-es';
|
|
||||||
import * as assert from 'assert';
|
import * as assert from 'assert';
|
||||||
import type { ReplyOptions } from '../../types/utility';
|
import type { ReplyOptions } from '../../types/utility';
|
||||||
import { fmt } from '../functions'
|
import { fmt } from '../functions'
|
||||||
@@ -21,39 +20,32 @@ import { SernError } from './enums';
|
|||||||
* Provides values shared between
|
* Provides values shared between
|
||||||
* Message and ChatInputCommandInteraction
|
* Message and ChatInputCommandInteraction
|
||||||
*/
|
*/
|
||||||
export class Context extends CoreContext<Message, ChatInputCommandInteraction> {
|
export class Context {
|
||||||
|
|
||||||
get options() {
|
get options() {
|
||||||
if(this.isMessage()) {
|
if(this.isMessage()) {
|
||||||
const [, ...rest] = fmt(this.message.content, this.prefix);
|
const [, ...rest] = fmt(this.message.content, this.prefix);
|
||||||
return rest;
|
return rest;
|
||||||
} else {
|
}
|
||||||
return this.interaction.options;
|
return this.interaction.options;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected constructor(protected ctx: Result<Message, ChatInputCommandInteraction>,
|
protected constructor(protected ctx: Result<Message, ChatInputCommandInteraction>,
|
||||||
private __prefix?: string) {
|
private __prefix?: string) { }
|
||||||
super(ctx);
|
|
||||||
}
|
|
||||||
public get prefix() {
|
public get prefix() {
|
||||||
return this.__prefix;
|
return this.__prefix;
|
||||||
}
|
}
|
||||||
public get id(): Snowflake {
|
public get id(): Snowflake {
|
||||||
return safeUnwrap(this.ctx
|
return val(this.ctx).id
|
||||||
.map(m => m.id)
|
|
||||||
.mapErr(i => i.id));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public get channel() {
|
public get channel() {
|
||||||
return safeUnwrap(this.ctx.map(m => m.channel).mapErr(i => i.channel));
|
return val(this.ctx).channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
public get channelId(): Snowflake {
|
public get channelId(): Snowflake {
|
||||||
return safeUnwrap(this.ctx
|
return val(this.ctx).channelId;
|
||||||
.map(m => m.channelId)
|
|
||||||
.mapErr(i => i.channelId));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -61,9 +53,11 @@ export class Context extends CoreContext<Message, ChatInputCommandInteraction> {
|
|||||||
* else, interaction.user
|
* else, interaction.user
|
||||||
*/
|
*/
|
||||||
public get user(): User {
|
public get user(): User {
|
||||||
return safeUnwrap(this.ctx
|
if(this.ctx.ok) {
|
||||||
.map(m => m.author)
|
return this.ctx.value.author;
|
||||||
.mapErr(i => i.user));
|
}
|
||||||
|
return this.ctx.error.user;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public get userId(): Snowflake {
|
public get userId(): Snowflake {
|
||||||
@@ -71,59 +65,60 @@ export class Context extends CoreContext<Message, ChatInputCommandInteraction> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public get createdTimestamp(): number {
|
public get createdTimestamp(): number {
|
||||||
return safeUnwrap(this.ctx
|
return val(this.ctx).createdTimestamp;
|
||||||
.map(m => m.createdTimestamp)
|
|
||||||
.mapErr(i => i.createdTimestamp));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public get guild() {
|
public get guild() {
|
||||||
return safeUnwrap(this.ctx
|
return val(this.ctx).guild;
|
||||||
.map(m => m.guild)
|
|
||||||
.mapErr(i => i.guild));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public get guildId() {
|
public get guildId() {
|
||||||
return safeUnwrap(this.ctx
|
return val(this.ctx).guildId;
|
||||||
.map(m => m.guildId)
|
|
||||||
.mapErr(i => i.guildId));
|
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* interactions can return APIGuildMember if the guild it is emitted from is not cached
|
* interactions can return APIGuildMember if the guild it is emitted from is not cached
|
||||||
*/
|
*/
|
||||||
public get member() {
|
public get member() {
|
||||||
return safeUnwrap(this.ctx
|
return val(this.ctx).member;
|
||||||
.map(m => m.member)
|
|
||||||
.mapErr(i => i.member));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get message(): Message {
|
get message(): Message {
|
||||||
return this.ctx.expect(SernError.MismatchEvent);
|
if(this.ctx.ok) {
|
||||||
|
return this.ctx.value;
|
||||||
|
}
|
||||||
|
throw Error(SernError.MismatchEvent);
|
||||||
|
}
|
||||||
|
public isMessage(): this is Context & { ctx: Result<Message, never> } {
|
||||||
|
return this.ctx.ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
public isSlash(): this is Context & { ctx: Result<never, ChatInputCommandInteraction> } {
|
||||||
|
return !this.isMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
get interaction(): ChatInputCommandInteraction {
|
get interaction(): ChatInputCommandInteraction {
|
||||||
return this.ctx.expectErr(SernError.MismatchEvent);
|
if(!this.ctx.ok) {
|
||||||
|
return this.ctx.error;
|
||||||
|
}
|
||||||
|
throw Error(SernError.MismatchEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public get client(): Client {
|
public get client(): Client {
|
||||||
return safeUnwrap(this.ctx
|
return val(this.ctx).client;
|
||||||
.map(m => m.client)
|
|
||||||
.mapErr(i => i.client));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public get inGuild(): boolean {
|
public get inGuild(): boolean {
|
||||||
return safeUnwrap(this.ctx
|
return val(this.ctx).inGuild()
|
||||||
.map(m => m.inGuild())
|
|
||||||
.mapErr(i => i.inGuild()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async reply(content: ReplyOptions) {
|
public async reply(content: ReplyOptions) {
|
||||||
return safeUnwrap(
|
if(this.ctx.ok) {
|
||||||
this.ctx
|
return this.ctx.value.reply(content as MessageReplyOptions)
|
||||||
.map(m => m.reply(content as MessageReplyOptions))
|
}
|
||||||
.mapErr(i =>
|
interface FetchReply { fetchReply: true };
|
||||||
i.reply(content as InteractionReplyOptions).then(() => i.fetchReply())),
|
return this.ctx.error.reply(content as InteractionReplyOptions & FetchReply)
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static wrap(wrappable: BaseInteraction | Message, prefix?: string): Context {
|
static wrap(wrappable: BaseInteraction | Message, prefix?: string): Context {
|
||||||
@@ -134,10 +129,3 @@ export class Context extends CoreContext<Message, ChatInputCommandInteraction> {
|
|||||||
return new Context(Err(wrappable), prefix);
|
return new Context(Err(wrappable), prefix);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function safeUnwrap<T>(res: Result<T, T>) {
|
|
||||||
if(res.isOk()) {
|
|
||||||
return res.expect("Tried unwrapping message field: " + res)
|
|
||||||
}
|
|
||||||
return res.expectErr("Tried unwrapping interaction field" + res)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
import { Result as Either } from 'ts-results-es';
|
|
||||||
import * as assert from 'node:assert';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @since 3.0.0
|
|
||||||
*/
|
|
||||||
export abstract class CoreContext<M, I> {
|
|
||||||
protected constructor(protected ctx: Either<M, I>) {
|
|
||||||
assert.ok(typeof ctx === 'object' && ctx != null, "Context was nonobject or null");
|
|
||||||
}
|
|
||||||
public isMessage(): this is CoreContext<M, never> {
|
|
||||||
return this.ctx.isOk();
|
|
||||||
}
|
|
||||||
|
|
||||||
public isSlash(): this is CoreContext<never, I> {
|
|
||||||
return !this.isMessage();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
20
src/core/structures/result.ts
Normal file
20
src/core/structures/result.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
export type Result<Ok, Err> =
|
||||||
|
| { ok: true; value: Ok }
|
||||||
|
| { ok: false; error: 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 val = <O, E>(r: Result<O, E>) => r.ok ? r.value : r.error;
|
||||||
|
export const EMPTY_ERR = Err(undefined);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrap an async operation that may throw an Error (`try-catch` style) into checked exception style
|
||||||
|
* @param op The operation function
|
||||||
|
*/
|
||||||
|
export async function wrapAsync<T, E = unknown>(op: () => Promise<T>): Promise<Result<T, E>> {
|
||||||
|
try { return op()
|
||||||
|
.then(Ok)
|
||||||
|
.catch(Err); }
|
||||||
|
catch (e) { return Promise.resolve(Err(e as E)); }
|
||||||
|
}
|
||||||
@@ -8,7 +8,7 @@ import {
|
|||||||
import * as Id from '../core/id'
|
import * as Id from '../core/id'
|
||||||
import type { Emitter, ErrorHandling, Logging } from '../core/interfaces';
|
import type { Emitter, ErrorHandling, Logging } from '../core/interfaces';
|
||||||
import { SernError } from '../core/structures/enums'
|
import { SernError } from '../core/structures/enums'
|
||||||
import { Err, Ok, Result } from 'ts-results-es';
|
import { EMPTY_ERR, Err, Ok, Result, wrapAsync } from '../core/structures/result';
|
||||||
import type { UnpackedDependencies } from '../types/utility';
|
import type { UnpackedDependencies } from '../types/utility';
|
||||||
import type { CommandModule, Module, Processed } from '../types/core-modules';
|
import type { CommandModule, Module, Processed } from '../types/core-modules';
|
||||||
import * as assert from 'node:assert';
|
import * as assert from 'node:assert';
|
||||||
@@ -17,6 +17,7 @@ import { CommandType } from '../core/structures/enums'
|
|||||||
import { inspect } from 'node:util'
|
import { inspect } from 'node:util'
|
||||||
import { disposeAll } from '../core/ioc';
|
import { disposeAll } from '../core/ioc';
|
||||||
import { resultPayload, isAutocomplete, treeSearch, fmt } from '../core/functions'
|
import { resultPayload, isAutocomplete, treeSearch, fmt } from '../core/functions'
|
||||||
|
|
||||||
import merge from 'deepmerge'
|
import merge from 'deepmerge'
|
||||||
|
|
||||||
function handleError<C>(crashHandler: ErrorHandling, emitter: Emitter, logging?: Logging) {
|
function handleError<C>(crashHandler: ErrorHandling, emitter: Emitter, logging?: Logging) {
|
||||||
@@ -43,7 +44,7 @@ interface ExecutePayload {
|
|||||||
|
|
||||||
export const filterTap = <K, R>(onErr: (e: R) => void): OperatorFunction<Result<K, R>, K> =>
|
export const filterTap = <K, R>(onErr: (e: R) => void): OperatorFunction<Result<K, R>, K> =>
|
||||||
concatMap(result => {
|
concatMap(result => {
|
||||||
if(result.isOk()) {
|
if(result.ok){
|
||||||
return of(result.value)
|
return of(result.value)
|
||||||
}
|
}
|
||||||
onErr(result.error);
|
onErr(result.error);
|
||||||
@@ -142,7 +143,7 @@ export function createInteractionHandler<T extends Interaction>(
|
|||||||
.map(({ id, params }) => ({ module: mg.get(id), params }))
|
.map(({ id, params }) => ({ module: mg.get(id), params }))
|
||||||
.filter(({ module }) => module !== undefined);
|
.filter(({ module }) => module !== undefined);
|
||||||
if(modules.length == 0) {
|
if(modules.length == 0) {
|
||||||
return Err.EMPTY;
|
return EMPTY_ERR;
|
||||||
}
|
}
|
||||||
const [{module, params}] = modules;
|
const [{module, params}] = modules;
|
||||||
return Ok(createDispatcher({
|
return Ok(createDispatcher({
|
||||||
@@ -179,9 +180,9 @@ export function createMessageHandler(
|
|||||||
* @param task the deferred execution which will be called
|
* @param task the deferred execution which will be called
|
||||||
*/
|
*/
|
||||||
export function executeModule(emitter: Emitter, { module, args }: ExecutePayload) {
|
export function executeModule(emitter: Emitter, { module, args }: ExecutePayload) {
|
||||||
return from(Result.wrapAsync(async () => module.execute(...args)))
|
return from(wrapAsync(async () => module.execute(...args)))
|
||||||
.pipe(concatMap(result => {
|
.pipe(concatMap(result => {
|
||||||
if (result.isOk()) {
|
if (result.ok){
|
||||||
emitter.emit('module.activate', resultPayload('success', module));
|
emitter.emit('module.activate', resultPayload('success', module));
|
||||||
return EMPTY;
|
return EMPTY;
|
||||||
}
|
}
|
||||||
@@ -206,10 +207,10 @@ export function createResultResolver<Output>(config: {
|
|||||||
return async (payload: ExecutePayload) => {
|
return async (payload: ExecutePayload) => {
|
||||||
const task = await callPlugins(payload);
|
const task = await callPlugins(payload);
|
||||||
if (!task) throw Error("Plugin did not return anything.");
|
if (!task) throw Error("Plugin did not return anything.");
|
||||||
if(task.isOk()) {
|
if(!task.ok) {
|
||||||
return onNext(payload, task.value) as Output;
|
|
||||||
} else {
|
|
||||||
onStop?.(payload.module, String(task.error));
|
onStop?.(payload.module, String(task.error));
|
||||||
|
} else {
|
||||||
|
return onNext(payload, task.value) as Output;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -225,12 +226,13 @@ export async function callInitPlugins(_module: Module, deps: Dependencies, emit?
|
|||||||
for(const plugin of module.plugins ?? []) {
|
for(const plugin of module.plugins ?? []) {
|
||||||
const result = await plugin.execute({ module, absPath: module.meta.absPath, deps });
|
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 (!result) throw Error("Plugin did not return anything. " + inspect(plugin, false, Infinity, true));
|
||||||
if(result.isErr()) {
|
if(!result.ok) {
|
||||||
if(emit) {
|
if(emit) {
|
||||||
emitter?.emit('module.register',
|
emitter?.emit('module.register',
|
||||||
resultPayload('failure', module, result.error ?? SernError.PluginFailure));
|
resultPayload('failure', module, result.error ?? SernError.PluginFailure));
|
||||||
}
|
}
|
||||||
throw Error(result.error ?? SernError.PluginFailure);
|
throw Error((result.error ?? SernError.PluginFailure) +
|
||||||
|
'on module ' + module.name + " " + module.meta.absPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return module
|
return module
|
||||||
@@ -240,7 +242,7 @@ export async function callPlugins({ args, module, deps, params }: ExecutePayload
|
|||||||
let state = {};
|
let state = {};
|
||||||
for(const plugin of module.onEvent??[]) {
|
for(const plugin of module.onEvent??[]) {
|
||||||
const result = await plugin.execute(...args, { state, deps, params, type: module.type });
|
const result = await plugin.execute(...args, { state, deps, params, type: module.type });
|
||||||
if(result.isErr()) {
|
if(!result.ok) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
if(isObject(result.value)) {
|
if(isObject(result.value)) {
|
||||||
|
|||||||
@@ -11,7 +11,6 @@
|
|||||||
* Plugins are reminiscent of middleware in express.
|
* Plugins are reminiscent of middleware in express.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import type { Result } from 'ts-results-es';
|
|
||||||
import type {
|
import type {
|
||||||
Module,
|
Module,
|
||||||
Processed,
|
Processed,
|
||||||
@@ -31,6 +30,7 @@ import type {
|
|||||||
UserContextMenuCommandInteraction,
|
UserContextMenuCommandInteraction,
|
||||||
UserSelectMenuInteraction,
|
UserSelectMenuInteraction,
|
||||||
} from 'discord.js';
|
} from 'discord.js';
|
||||||
|
import { Result } from '../core/structures/result';
|
||||||
|
|
||||||
export type PluginResult = Awaitable<Result<Record<string,unknown>|undefined, string|undefined>>;
|
export type PluginResult = Awaitable<Result<Record<string,unknown>|undefined, string|undefined>>;
|
||||||
export interface InitArgs<T extends Processed<Module> = Processed<Module>> {
|
export interface InitArgs<T extends Processed<Module> = Processed<Module>> {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { InteractionReplyOptions, MessageReplyOptions } from 'discord.js';
|
import type { InteractionReplyOptions, MessageReplyOptions } from 'discord.js';
|
||||||
import type { Module } from './core-modules';
|
import type { Module } from './core-modules';
|
||||||
import type { Result } from 'ts-results-es';
|
import type { Result } from '../core/structures/result';
|
||||||
|
|
||||||
export type Awaitable<T> = PromiseLike<T> | T;
|
export type Awaitable<T> = PromiseLike<T> | T;
|
||||||
|
|
||||||
|
|||||||
@@ -120,13 +120,6 @@ test('init plugins replace array', async () => {
|
|||||||
expect(['a']).deep.equal(s.opts)
|
expect(['a']).deep.equal(s.opts)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('call control plugin ', async () => {
|
|
||||||
const plugin = CommandControlPlugin<CommandType.Slash>((ctx,sdt) => {
|
|
||||||
return controller.next();
|
|
||||||
});
|
|
||||||
const res = await plugin.execute(new ChatInputCommandInteraction(), {})
|
|
||||||
expect(res.isOk()).toBe(true)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('form sdt', async () => {
|
test('form sdt', async () => {
|
||||||
|
|
||||||
|
|||||||
@@ -557,7 +557,6 @@ __metadata:
|
|||||||
discord.js: ^14.15.3
|
discord.js: ^14.15.3
|
||||||
eslint: 8.39.0
|
eslint: 8.39.0
|
||||||
rxjs: ^7.8.0
|
rxjs: ^7.8.0
|
||||||
ts-results-es: ^4.1.0
|
|
||||||
typescript: 5.0.2
|
typescript: 5.0.2
|
||||||
vitest: ^1.6.0
|
vitest: ^1.6.0
|
||||||
languageName: unknown
|
languageName: unknown
|
||||||
@@ -2959,13 +2958,6 @@ __metadata:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
"ts-results-es@npm:^4.1.0":
|
|
||||||
version: 4.2.0
|
|
||||||
resolution: "ts-results-es@npm:4.2.0"
|
|
||||||
checksum: ff475c2f6d44377e0204211e6eafdbcabddf3ad09d40540ad5dee3d817eefbd48c07a21f5ad86864ef82cd8a5542a266af9dd8dd4d58d4766fdd6e79370519bb
|
|
||||||
languageName: node
|
|
||||||
linkType: hard
|
|
||||||
|
|
||||||
"tslib@npm:2.6.2":
|
"tslib@npm:2.6.2":
|
||||||
version: 2.6.2
|
version: 2.6.2
|
||||||
resolution: "tslib@npm:2.6.2"
|
resolution: "tslib@npm:2.6.2"
|
||||||
|
|||||||
Reference in New Issue
Block a user