mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
refactor: cleaning up code and renaming variables
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import type { Processed } from '../../types/core';
|
||||
import type { BothCommand, CommandModule, Module } from '../../types/module';
|
||||
import type { BothCommand, CommandModule, EventModule, Module } from '../../types/module';
|
||||
import { EventEmitter } from 'node:events';
|
||||
import * as assert from 'node:assert';
|
||||
import { concatMap, from, fromEvent, map, OperatorFunction, pipe } from 'rxjs';
|
||||
@@ -9,7 +9,7 @@ import { AutocompleteInteraction, BaseInteraction, Message } from 'discord.js';
|
||||
import { treeSearch } from '../../core/functions';
|
||||
import { SernError } from '../../core/structures/errors';
|
||||
import { Args } from '../../types/handler';
|
||||
import { CommandType, Context } from '../../core';
|
||||
import { CommandType, Context, EventType } from '../../core';
|
||||
import { isAutocomplete } from '../../core/predicates';
|
||||
|
||||
export function dispatchInteraction<
|
||||
@@ -24,7 +24,7 @@ export function dispatchInteraction<
|
||||
args: createArgs(payload.event),
|
||||
};
|
||||
}
|
||||
|
||||
//TODO: refactor dispatchers so that it implements a strategy for each different type of payload?
|
||||
export function dispatchMessage(module: Processed<CommandModule>, args: [Context, Args]) {
|
||||
return {
|
||||
module,
|
||||
@@ -46,6 +46,8 @@ export function dispatchAutocomplete(payload: { module: Processed<BothCommand>,
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
export function contextArgs(
|
||||
wrappable: Message | BaseInteraction,
|
||||
messageArgs?: string[],
|
||||
@@ -55,6 +57,7 @@ export function contextArgs(
|
||||
return [ctx, args] as [Context, Args];
|
||||
}
|
||||
|
||||
|
||||
export function interactionArg<T extends BaseInteraction>(interaction: T) {
|
||||
return [interaction] as [T];
|
||||
}
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
import {
|
||||
BaseInteraction,
|
||||
Interaction,
|
||||
InteractionType,
|
||||
Message,
|
||||
} from 'discord.js';
|
||||
import { EMPTY, Observable, concatMap, filter, from, map, of, throwError, tap } from 'rxjs';
|
||||
import { CommandType, ModuleManager } from '../../core';
|
||||
import { ModuleManager } from '../../core';
|
||||
import { SernError } from '../../core/structures/errors';
|
||||
import { callPlugin, everyPluginOk, filterMap, filterMapTo } from '../../core/operators';
|
||||
import { defaultModuleLoader } from '../../core/module-loading';
|
||||
import { ImportPayload, Processed } from '../../types/core';
|
||||
import { CommandModule, Module } from '../../types/module';
|
||||
import { contextArgs, createDispatcher, dispatchAutocomplete, dispatchInteraction, dispatchMessage, interactionArg } from './dispatchers';
|
||||
import { isAutocomplete } from '../../core/predicates';
|
||||
import { contextArgs, createDispatcher, dispatchMessage } from './dispatchers';
|
||||
import { ObservableInput, pipe, switchMap } from 'rxjs';
|
||||
import { SernEmitter } from '../../core';
|
||||
import { errTap } from '../../core/operators';
|
||||
|
||||
@@ -10,6 +10,7 @@ import { DependencyList } from '../../types/core';
|
||||
export function makeInteractionHandler([emitter, _, _1, modules, client]: DependencyList ) {
|
||||
const interactionStream$ = sharedObservable<Interaction>(client, 'interactionCreate');
|
||||
const handle = createInteractionHandler(interactionStream$, modules);
|
||||
|
||||
const interactionHandler$ = merge(
|
||||
handle(isMessageComponent),
|
||||
handle(isAutocomplete),
|
||||
|
||||
@@ -31,12 +31,12 @@ export function makeMessageHandler(
|
||||
const messageStream$ = sharedObservable<Message>(client, 'messageCreate');
|
||||
const handler = createMessageHandler(messageStream$, defaultPrefix, modules);
|
||||
|
||||
const messageHandler = handler(isNonBot(defaultPrefix) as (m: Message) => m is Message);
|
||||
return messageHandler.pipe(
|
||||
const prefixedMessages$ = handler(isNonBot(defaultPrefix) as (m: Message) => m is Message);
|
||||
|
||||
return prefixedMessages$.pipe(
|
||||
makeModuleExecutor(module => {
|
||||
emitter.emit('module.activate', SernEmitter.failure(module, SernError.PluginFailure));
|
||||
}),
|
||||
concatMap(payload => executeModule(emitter, payload)),
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
@@ -12,12 +12,12 @@ import { buildModules, callInitPlugins } from './generic';
|
||||
|
||||
export function startReadyEvent(
|
||||
[sEmitter, errorHandler, , moduleManager, client]: DependencyList,
|
||||
input: ObservableInput<string>,
|
||||
allPaths: ObservableInput<string>,
|
||||
) {
|
||||
const ready$ = fromEvent(client!, 'ready').pipe(take(1));
|
||||
return ready$
|
||||
.pipe(
|
||||
buildModules(input, sEmitter),
|
||||
buildModules(allPaths, sEmitter),
|
||||
callInitPlugins({
|
||||
onStop: module => {
|
||||
sEmitter.emit(
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { catchError, finalize, map, mergeAll, of } from 'rxjs';
|
||||
import { ObservableInput, catchError, finalize, map, mergeAll, of } from 'rxjs';
|
||||
import type { Dependencies, Processed, Wrapper } from '../../types/core';
|
||||
import type { CommandModule, EventModule } from '../../types/module';
|
||||
import type { EventEmitter } from 'node:events';
|
||||
@@ -11,9 +11,11 @@ import { handleError } from '../../core/contracts/error-handling';
|
||||
import { useContainerRaw } from '../../core/dependencies';
|
||||
import { buildModules, callInitPlugins } from './generic';
|
||||
|
||||
|
||||
|
||||
export function makeEventsHandler(
|
||||
[s, err, log, client]: [SernEmitter, ErrorHandling, Logging | undefined, EventEmitter],
|
||||
eventsPath: string,
|
||||
allPaths: ObservableInput<string>,
|
||||
containerGetter: Wrapper['containerConfig'],
|
||||
) {
|
||||
const lazy = (k: string) => containerGetter.get(k as keyof Dependencies)[0];
|
||||
@@ -33,7 +35,7 @@ export function makeEventsHandler(
|
||||
};
|
||||
of(null)
|
||||
.pipe(
|
||||
buildModules(eventsPath, s),
|
||||
buildModules(allPaths, s),
|
||||
callInitPlugins({
|
||||
onStop: module =>
|
||||
s.emit('module.register', SernEmitter.failure(module, SernError.PluginFailure)),
|
||||
|
||||
Reference in New Issue
Block a user