mirror of
https://github.com/sern-handler/handler
synced 2026-06-19 14:22:13 +00:00
cleanup tests, codegen, and importing handler
This commit is contained in:
@@ -1,40 +1,4 @@
|
||||
import type { Interaction } from 'discord.js';
|
||||
import { mergeMap, merge, concatMap } from 'rxjs';
|
||||
import { PayloadType } from './core/structures/enums';
|
||||
import { shouldHandle } from './core/module-loading'
|
||||
import {
|
||||
isAutocomplete,
|
||||
isCommand,
|
||||
isMessageComponent,
|
||||
isModal,
|
||||
sharedEventStream,
|
||||
SernError,
|
||||
filterTap,
|
||||
resultPayload,
|
||||
type _Module,
|
||||
} from './core/_internal';
|
||||
import { createInteractionHandler, executeModule, makeModuleExecutor } from './handlers/event-utils';
|
||||
import type { Emitter, ErrorHandling, Logging } from './core/interfaces'
|
||||
|
||||
function interactionHandler(client: Emitter,
|
||||
emitter: Emitter,
|
||||
log: Logging,
|
||||
err: ErrorHandling,
|
||||
modules: Map<string, _Module>) {
|
||||
const interactionStream$ = sharedEventStream<Interaction>(client, 'interactionCreate');
|
||||
const handle = createInteractionHandler(interactionStream$, modules);
|
||||
|
||||
const interactionHandler$ = merge(handle(isMessageComponent),
|
||||
handle(isAutocomplete),
|
||||
handle(isCommand),
|
||||
handle(isModal));
|
||||
return interactionHandler$
|
||||
.pipe(filterTap(e => emitter.emit('warning', resultPayload(PayloadType.Warning, undefined, e))),
|
||||
concatMap(makeModuleExecutor(module =>
|
||||
emitter.emit('module.activate', resultPayload(PayloadType.Failure, module, SernError.PluginFailure)))),
|
||||
mergeMap(payload => executeModule(emitter, log, err, payload)));
|
||||
}
|
||||
|
||||
export const __start = (entryPoint: string, wrapper: { defaultPrefix?: string }) => {
|
||||
|
||||
}
|
||||
|
||||
@@ -3,10 +3,10 @@ import assert from 'assert';
|
||||
import { existsSync } from 'fs';
|
||||
|
||||
|
||||
export const parseCallsite = (fpath: string) => {
|
||||
const pathobj = path.parse(fpath.replace(/file:\\?/, "")
|
||||
.split(path.sep)
|
||||
.join(path.posix.sep))
|
||||
export const parseCallsite = (site: string) => {
|
||||
const pathobj = path.parse(site.replace(/file:\\?/, "")
|
||||
.split(path.sep)
|
||||
.join(path.posix.sep))
|
||||
return { name: pathobj.name,
|
||||
absPath : path.posix.format(pathobj) }
|
||||
}
|
||||
@@ -16,7 +16,7 @@ export const shouldHandle = (pth: string, filenam: string) => {
|
||||
let newPath = path.join(path.dirname(pth), file_name)
|
||||
.replace(/file:\\?/, "");
|
||||
return { exists: existsSync(newPath),
|
||||
path: 'file:///'+newPath };
|
||||
path: 'file://'+newPath };
|
||||
}
|
||||
|
||||
|
||||
|
||||
21
src/sern.ts
21
src/sern.ts
@@ -1,8 +1,7 @@
|
||||
import callsites from 'callsites';
|
||||
import * as Files from './core/module-loading';
|
||||
import { Services } from './core/ioc';
|
||||
|
||||
|
||||
import type { DependencyList } from './types/ioc';
|
||||
|
||||
interface Wrapper {
|
||||
commands?: string;
|
||||
@@ -10,6 +9,16 @@ interface Wrapper {
|
||||
events?: string;
|
||||
}
|
||||
|
||||
const __start = (entryPoint: string,
|
||||
wrapper: { defaultPrefix?: string },
|
||||
dependencies: DependencyList) => {
|
||||
console.log(entryPoint)
|
||||
import(entryPoint)
|
||||
.then(({ __commands, __events=new Map() }) => {
|
||||
console.log(__commands, __events)
|
||||
})
|
||||
.catch(err => dependencies[2]?.error({ message: err }));
|
||||
}
|
||||
/**
|
||||
* @since 1.0.0
|
||||
* @param wrapper Options to pass into sern.
|
||||
@@ -28,10 +37,10 @@ export function init(wrapper: Wrapper) {
|
||||
|
||||
const initCallsite = callsites()[1].getFileName();
|
||||
const handlerModule = Files.shouldHandle(initCallsite!, "handler");
|
||||
console.log(handlerModule)
|
||||
if(!handlerModule.exists) {
|
||||
throw Error("Cannot locate handler file. Did you run sern build?");
|
||||
throw Error("Could not find handler module, did you run sern build?")
|
||||
}
|
||||
import(handlerModule.path)
|
||||
.then(({ start }) => start(initCallsite, wrapper))
|
||||
.catch(err => dependencies[2].error({ message: err }))
|
||||
__start(handlerModule.path, wrapper, dependencies);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user