mirror of
https://github.com/sern-handler/handler
synced 2026-06-22 07:42:14 +00:00
feat: change typings of sern emitter
This commit is contained in:
@@ -216,10 +216,10 @@ export function onInteractionCreate(wrapper: Wrapper) {
|
||||
}
|
||||
if (ePlugArr.every(e => e.ok)) {
|
||||
await execute();
|
||||
wrapper.sernEmitter?.emit('module.activate', { success: true, module: mod! });
|
||||
wrapper.sernEmitter?.emit('module.activate', { type: 'success', module: mod! });
|
||||
} else {
|
||||
wrapper.sernEmitter?.emit('module.activate', {
|
||||
success: false,
|
||||
type: 'failure',
|
||||
module: mod!,
|
||||
reason: SernError.PluginFailure,
|
||||
});
|
||||
|
||||
@@ -53,11 +53,11 @@ export const onMessageCreate = (wrapper: Wrapper) => {
|
||||
next({ mod, ctx, args, res }) {
|
||||
if (res.every(pl => pl.ok)) {
|
||||
Promise.resolve(mod.execute(ctx, args)).then(() => {
|
||||
wrapper.sernEmitter?.emit('module.activate', { success: true, module: mod! });
|
||||
wrapper.sernEmitter?.emit('module.activate', { type: 'success', module: mod! });
|
||||
});
|
||||
} else {
|
||||
wrapper.sernEmitter?.emit('module.activate', {
|
||||
success: false,
|
||||
type: 'failure',
|
||||
module: mod!,
|
||||
reason: SernError.PluginFailure,
|
||||
});
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Observable, throwError } from 'rxjs';
|
||||
import { SernError } from '../structures/errors';
|
||||
import type { Module, ModuleDefs } from '../structures/module';
|
||||
import { correctModuleType } from '../utilities/predicates';
|
||||
import type { Result } from 'ts-results';
|
||||
export function filterCorrectModule<T extends keyof ModuleDefs>(cmdType: T) {
|
||||
return (src: Observable<Module | undefined>) =>
|
||||
new Observable<ModuleDefs[T]>(subscriber => {
|
||||
@@ -42,3 +43,24 @@ export function ignoreNonBot(prefix: string) {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* If the current value in Result stream is an error, calls callback.
|
||||
* @param cb
|
||||
*/
|
||||
export function errTap(cb: (err: SernError) => void) {
|
||||
return (src: Observable<Result<{ mod: Module; absPath: string }, SernError>>) =>
|
||||
new Observable<{ mod: Module; absPath: string }>(subscriber => {
|
||||
return src.subscribe({
|
||||
next(value) {
|
||||
if (value.err) {
|
||||
cb(value.val);
|
||||
} else {
|
||||
subscriber.next(value.val);
|
||||
}
|
||||
},
|
||||
error: e => subscriber.error(e),
|
||||
complete: () => subscriber.complete(),
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ import { match } from 'ts-pattern';
|
||||
import { SernError } from '../structures/errors';
|
||||
import type { DefinitelyDefined } from '../../types/handler';
|
||||
import { CommandType, PluginType } from '../structures/enums';
|
||||
import { elseMap, elseMapTo, resultMap } from 'ts-results/rxjs-operators';
|
||||
import { errTap } from './observableHandling';
|
||||
|
||||
export const onReady = (wrapper: Wrapper) => {
|
||||
|
||||
@@ -2,9 +2,8 @@ import { EventEmitter } from 'events';
|
||||
import type { Module } from './structures/module';
|
||||
|
||||
type Payload =
|
||||
| { success: true; module: Module }
|
||||
| { success: false; module: Module | undefined; reason: string | Error };
|
||||
|
||||
| { type: 'success'; module: Module }
|
||||
| { type: 'failure'; module: Module | undefined; reason: string | Error };
|
||||
export type SernEventsMapping = {
|
||||
['module.register']: [Payload];
|
||||
['module.activate']: [Payload];
|
||||
|
||||
Reference in New Issue
Block a user