mirror of
https://github.com/sern-handler/handler
synced 2026-06-16 04:42:16 +00:00
feat(observableHandling) : making match function a type predicate
assertion
This commit is contained in:
@@ -18,7 +18,7 @@ export const onMessageCreate = (wrapper : Wrapper) => {
|
||||
const posMod = Files.Commands.get(prefix) ?? Files.Alias.get(prefix);
|
||||
const ctx = Context.wrap(m);
|
||||
return of( posMod )
|
||||
.pipe (
|
||||
.pipe(
|
||||
filterCorrectModule(CommandType.Text),
|
||||
filterTap(CommandType.Text, async (mod,plugins) => {
|
||||
const res = await Promise.all(
|
||||
|
||||
@@ -7,19 +7,22 @@ import { isNotFromBot } from '../utilities/messageHelpers';
|
||||
import type { PluggedModule } from '../structures/modules/module';
|
||||
import type { EventPlugin, SernPlugin } from '../plugins/plugin';
|
||||
|
||||
export function match(plug: PluggedModule | undefined, type : CommandType) : boolean {
|
||||
|
||||
export function match<T extends keyof ModuleDefs>(
|
||||
plug: PluggedModule | undefined, type : T
|
||||
) : plug is { mod: ModuleDefs[T], plugins : SernPlugin[] } {
|
||||
return plug !== undefined && (plug.mod.type & type) != 0;
|
||||
}
|
||||
|
||||
export function filterCorrectModule<T extends keyof ModuleDefs>(cmdType : T) {
|
||||
return (src : Observable<PluggedModule|undefined>) =>
|
||||
new Observable<PluggedModule>( subscriber => {
|
||||
new Observable<{cmdType : T, plug : PluggedModule}>( subscriber => {
|
||||
return src.subscribe({
|
||||
next(modul) {
|
||||
if(match(modul, cmdType)) {
|
||||
subscriber.next(modul);
|
||||
next(plug) {
|
||||
if(match(plug, cmdType)) {
|
||||
subscriber.next({cmdType, plug});
|
||||
} else {
|
||||
if (modul === undefined) {
|
||||
if (plug === undefined) {
|
||||
return throwError(() => SernError.UndefinedModule);
|
||||
}
|
||||
return throwError(() => SernError.MismatchModule);
|
||||
@@ -28,7 +31,7 @@ export function filterCorrectModule<T extends keyof ModuleDefs>(cmdType : T) {
|
||||
error: (e) => subscriber.error(e),
|
||||
complete: () => subscriber.complete()
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function filterTap<T extends keyof ModuleDefs>(
|
||||
|
||||
@@ -8,7 +8,6 @@ import type { CommandPlugin, SernPlugin } from '../plugins/plugin';
|
||||
import { partition } from './observableHandling';
|
||||
import { Err, Ok, Result } from 'ts-results';
|
||||
import type { PluggedModule } from '../structures/modules/module';
|
||||
import type { Awaitable } from 'discord.js';
|
||||
|
||||
export const onReady = ( wrapper : Wrapper ) => {
|
||||
const { client, commands } = wrapper;
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
import type { Client } from "discord.js";
|
||||
import { Module, Sern } from "../..";
|
||||
import { CommandPlugin, Controller, PluginType } from "./plugin";
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ export type EventPlugin<T extends CommandType = 1> = {
|
||||
modTy : T
|
||||
} & Override<BasePlugin, {
|
||||
execute : ( event : Parameters<ModuleDefs[T]['execute']>, controller: Controller ) => Awaitable<Result<void,void>>
|
||||
} >;
|
||||
}>;
|
||||
|
||||
export type SernPlugin =
|
||||
CommandPlugin
|
||||
|
||||
Reference in New Issue
Block a user