Skip to content

Commit

Permalink
once on eventModules
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobes committed May 25, 2024
1 parent af0f909 commit 12a8f0c
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/core/module-loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as Id from './id'
import { Module } from '../types/core-modules';

export const parseCallsite = (site: string) => {
const pathobj = path.posix.parse(site.replace(/file:\\?/, "")
const pathobj = path.parse(site.replace(/file:\\?/, "")
.split(path.sep)
.join(path.posix.sep))
return { name: pathobj.name,
Expand Down
16 changes: 10 additions & 6 deletions src/handlers/event-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Interaction, Message, BaseInteraction } from 'discord.js';
import {
EMPTY, type Observable, concatMap, filter,
throwError, fromEvent, map, type OperatorFunction,
catchError, finalize, pipe, from,
catchError, finalize, pipe, from, take,
} from 'rxjs';
import * as Id from '../core/id'
import type { Emitter } from '../core/interfaces';
Expand Down Expand Up @@ -43,10 +43,15 @@ export function eventDispatcher(deps: Dependencies, module: Module, source: unkn
concatMap(async args => {
if(args) return Reflect.apply(module.execute, null, args);
});

//@ts-ignore
let ev = fromEvent(source ,module.name!);
//@ts-ignore
return fromEvent(source, module.name!)
.pipe(intoPayload(module, deps),
execute);
if(module['once']) {
ev = ev.pipe(take(1))
}
return ev.pipe(intoPayload(module, deps),
execute);
}

interface DispatchPayload {
Expand Down Expand Up @@ -108,8 +113,7 @@ export function createInteractionHandler<T extends Interaction>(
const possibleIds = Id.reconstruct(event);
let modules = possibleIds
.map(({ id, params }) => ({ module: mg.get(id), params }))
.filter((id) => id !== undefined);

.filter(({ module }) => module !== undefined);
if(modules.length == 0) {
return Err.EMPTY;
}
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function (deps: UnpackedDependencies, defaultPrefix?: string) {
return msgCommands$.pipe(
filterTap(e => emitter.emit('warning', resultPayload(PayloadType.Warning, undefined, e))),
concatMap(intoTask(module => {
const result = resultPayload(PayloadType.Failure, module, SernError.PluginFailure);
const result = resultPayload('failure', module, SernError.PluginFailure);
emitter.emit('module.activate', result);
})),
mergeMap(payload => {
Expand Down
1 change: 0 additions & 1 deletion src/handlers/ready.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as Files from '../core/module-loading'
import { once } from 'node:events';
import { resultPayload } from '../core/functions';
import { PayloadType } from '..';
import { CommandType } from '../core/structures/enums';
import { Module } from '../types/core-modules';
import { UnpackedDependencies } from '../types/utility';
Expand Down
2 changes: 1 addition & 1 deletion src/types/core-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ type CommandModuleNoPlugins = {
[T in CommandType]: Omit<CommandModuleDefs[T], 'plugins' | 'onEvent' | 'meta'>;
};
type EventModulesNoPlugins = {
[T in EventType]: Omit<EventModuleDefs[T], 'plugins' | 'onEvent' | 'meta'>;
[T in EventType]: Omit<EventModuleDefs[T], 'plugins' | 'onEvent' | 'meta'> & { once?: boolean };
};

export type InputEvent = {
Expand Down
2 changes: 1 addition & 1 deletion src/types/core-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type {
SDT,
} from './core-modules';
import type { Awaitable } from './utility';
import type { CommandType, EventType, PluginType } from '../core/structures/enums'
import type { CommandType, PluginType } from '../core/structures/enums'
import type { Context } from '../core/structures/context'
import type {
ButtonInteraction,
Expand Down

0 comments on commit 12a8f0c

Please sign in to comment.