Skip to content

Commit 220a60e

Browse files
authored
feat: moduleinfo-in-eventplugins (#373)
1 parent 55715d5 commit 220a60e

File tree

6 files changed

+247
-18
lines changed

6 files changed

+247
-18
lines changed

src/core/modules.ts

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,39 @@ import { partitionPlugins } from './functions'
1010
import type { Awaitable } from '../types/utility';
1111

1212
/**
13-
* @since 1.0.0 The wrapper function to define command modules for sern
14-
* @param mod
13+
* Creates a command module with standardized structure and plugin support.
14+
*
15+
* @since 1.0.0
16+
* @param {InputCommand} mod - Command module configuration
17+
* @returns {Module} Processed command module ready for registration
18+
*
19+
* @example
20+
* // Basic slash command
21+
* export default commandModule({
22+
* type: CommandType.Slash,
23+
* description: "Ping command",
24+
* execute: async (ctx) => {
25+
* await ctx.reply("Pong! 🏓");
26+
* }
27+
* });
28+
*
29+
* @example
30+
* // Command with component interaction
31+
* export default commandModule({
32+
* type: CommandType.Slash,
33+
* description: "Interactive command",
34+
* execute: async (ctx) => {
35+
* const button = new ButtonBuilder({
36+
* customId: "btn/someData",
37+
* label: "Click me",
38+
* style: ButtonStyle.Primary
39+
* });
40+
* await ctx.reply({
41+
* content: "Interactive message",
42+
* components: [new ActionRowBuilder().addComponents(button)]
43+
* });
44+
* }
45+
* });
1546
*/
1647
export function commandModule(mod: InputCommand): Module {
1748
const [onEvent, plugins] = partitionPlugins(mod.plugins);
@@ -21,10 +52,33 @@ export function commandModule(mod: InputCommand): Module {
2152
locals: {} } as Module;
2253
}
2354

55+
2456
/**
57+
* Creates an event module for handling Discord.js or custom events.
58+
*
2559
* @since 1.0.0
26-
* The wrapper function to define event modules for sern
27-
* @param mod
60+
* @template T - Event name from ClientEvents
61+
* @param {InputEvent<T>} mod - Event module configuration
62+
* @returns {Module} Processed event module ready for registration
63+
* @throws {Error} If ControlPlugins are used in event modules
64+
*
65+
* @example
66+
* // Discord event listener
67+
* export default eventModule({
68+
* type: EventType.Discord,
69+
* execute: async (message) => {
70+
* console.log(`${message.author.tag}: ${message.content}`);
71+
* }
72+
* });
73+
*
74+
* @example
75+
* // Custom sern event
76+
* export default eventModule({
77+
* type: EventType.Sern,
78+
* execute: async (eventData) => {
79+
* // Handle sern-specific event
80+
* }
81+
* });
2882
*/
2983
export function eventModule<T extends keyof ClientEvents = keyof ClientEvents>(mod: InputEvent<T>): Module {
3084
const [onEvent, plugins] = partitionPlugins(mod.plugins);

src/core/plugin.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { CommandType, PluginType } from './structures/enums';
22
import type { Plugin, PluginResult, CommandArgs, InitArgs } from '../types/core-plugin';
33
import { Err, Ok } from './structures/result';
4+
import type { Dictionary } from '../types/utility';
45

56
export function makePlugin<V extends unknown[]>(
67
type: PluginType,
@@ -37,7 +38,7 @@ export function CommandControlPlugin<I extends CommandType>(
3738
* The object passed into every plugin to control a command's behavior
3839
*/
3940
export const controller = {
40-
next: (val?: Record<string,unknown>) => Ok(val),
41+
next: (val?: Dictionary) => Ok(val),
4142
stop: (val?: string) => Err(val),
4243
};
4344

src/handlers/event-utils.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,17 @@ export async function callInitPlugins(_module: Module, deps: Dependencies, emit?
241241
export async function callPlugins({ args, module, deps, params }: ExecutePayload) {
242242
let state = {};
243243
for(const plugin of module.onEvent??[]) {
244-
const result = await plugin.execute(...args, { state, deps, params, type: module.type });
244+
const executionContext = {
245+
state,
246+
deps,
247+
params,
248+
type: module.type,
249+
module: { name: module.name,
250+
description: module.description,
251+
locals: module.locals,
252+
meta: module.meta }
253+
};
254+
const result = await plugin.execute(...args, executionContext);
245255
if(!result.ok) {
246256
return result;
247257
}
@@ -253,14 +263,26 @@ export async function callPlugins({ args, module, deps, params }: ExecutePayload
253263
}
254264
/**
255265
* Creates an executable task ( execute the command ) if all control plugins are successful
266+
* this needs to go
256267
* @param onStop emits a failure response to the SernEmitter
257268
*/
258269
export function intoTask(onStop: (m: Module) => unknown) {
259-
const onNext = ({ args, module, deps, params }: ExecutePayload, state: Record<string, unknown>) => ({
260-
module,
261-
args: [...args, { state, deps, params, type: module.type }],
262-
deps
263-
});
270+
const onNext = ({ args, module, deps, params }: ExecutePayload, state: Record<string, unknown>) => {
271+
272+
return {
273+
module,
274+
args: [...args, { state,
275+
deps,
276+
params,
277+
type: module.type,
278+
module: { name: module.name,
279+
description: module.description,
280+
locals: module.locals,
281+
meta: module.meta } }],
282+
deps
283+
}
284+
285+
};
264286
return createResultResolver({ onStop, onNext });
265287
}
266288

src/handlers/presence.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { concatMap, from, interval, of, map, scan, startWith, fromEvent, take, mergeScan } from "rxjs"
1+
import { concatMap, from, interval, of, map, startWith, fromEvent, take, mergeScan } from "rxjs"
22
import { Presence } from "../core/presences";
33
import { Services } from "../core/ioc";
44
import assert from "node:assert";

src/types/core-modules.ts

Lines changed: 157 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,97 @@ import type {
1919
import type { CommandType, EventType } from '../core/structures/enums';
2020
import { Context } from '../core/structures/context'
2121
import { ControlPlugin, InitPlugin, Plugin } from './core-plugin';
22-
import { Awaitable, SernEventsMapping, UnpackedDependencies } from './utility';
22+
import { Awaitable, SernEventsMapping, UnpackedDependencies, Dictionary } from './utility';
2323

24-
//state, deps, type (very original)
24+
/**
25+
* SDT (State, Dependencies, Type) interface represents the core data structure
26+
* passed through the plugin pipeline to command modules.
27+
*
28+
* @interface SDT
29+
* @template TState - Type parameter for the state object's structure
30+
* @template TDeps - Type parameter for dependencies interface
31+
*
32+
* @property {Record<string, unknown>} state - Accumulated state data passed between plugins
33+
* @property {TDeps} deps - Instance of application dependencies
34+
* @property {CommandType} type - Command type identifier
35+
* @property {string} [params] - Optional parameters passed to the command
36+
*
37+
* @example
38+
* // Example of a plugin using SDT
39+
* const loggingPlugin = CommandControlPlugin((ctx, sdt: SDT) => {
40+
* console.log(`User ${ctx.user.id} executed command`);
41+
* return controller.next({ 'logging/timestamp': Date.now() });
42+
* });
43+
*
44+
* @example
45+
* // Example of state accumulation through multiple plugins
46+
* const plugin1 = CommandControlPlugin((ctx, sdt: SDT) => {
47+
* return controller.next({ 'plugin1/data': 'value1' });
48+
* });
49+
*
50+
* const plugin2 = CommandControlPlugin((ctx, sdt: SDT) => {
51+
* // Access previous state
52+
* const prevData = sdt.state['plugin1/data'];
53+
* return controller.next({ 'plugin2/data': 'value2' });
54+
* });
55+
*
56+
* @remarks
57+
* - State is immutable and accumulated through the plugin chain
58+
* - Keys in state should be namespaced to avoid collisions
59+
* - Dependencies are injected and available throughout the pipeline
60+
* - Type information helps plugins make type-safe decisions
61+
*
62+
* @see {@link CommandControlPlugin} for plugin implementation
63+
* @see {@link CommandType} for available command types
64+
* @see {@link Dependencies} for dependency injection interface
65+
*/
2566
export type SDT = {
26-
state: Record<string,unknown>;
67+
/**
68+
* Accumulated state passed between plugins in the pipeline.
69+
* Each plugin can add to or modify this state using controller.next().
70+
*
71+
* @type {Record<string, unknown>}
72+
* @example
73+
* // Good: Namespaced state key
74+
* { 'myPlugin/userData': { id: '123', name: 'User' } }
75+
*
76+
* // Avoid: Non-namespaced keys that might collide
77+
* { userData: { id: '123' } }
78+
*/
79+
state: Record<string, unknown>;
80+
81+
/**
82+
* Application dependencies available to plugins and command modules.
83+
* Typically includes services, configurations, and utilities.
84+
*
85+
* @type {Dependencies}
86+
*/
2787
deps: Dependencies;
28-
type: CommandType,
29-
params?: string
88+
89+
/**
90+
* Identifies the type of command being processed.
91+
* Used by plugins to apply type-specific logic.
92+
*
93+
* @type {CommandType}
94+
*/
95+
type: CommandType;
96+
97+
/**
98+
* Optional parameters passed to the command.
99+
* May contain additional configuration or runtime data.
100+
*
101+
* @type {string}
102+
* @optional
103+
*/
104+
params?: string;
105+
106+
/**
107+
* A copy of the current module that the plugin is running in.
108+
*/
109+
module: { name: string;
110+
description: string;
111+
meta: Dictionary;
112+
locals: Dictionary; }
30113
};
31114

32115
export type Processed<T> = T & { name: string; description: string };
@@ -41,7 +124,75 @@ export interface Module {
41124
id: string;
42125
absPath: string;
43126
}
44-
locals: Record<string,unknown>
127+
128+
/**
129+
* Custom data storage object for module-specific information.
130+
* Plugins and module code can use this to store and retrieve metadata,
131+
* configuration, or any other module-specific information.
132+
*
133+
* @type {Dictionary}
134+
* @description A key-value store that allows plugins and module code to persist
135+
* data at the module level. This is especially useful for InitPlugins that need
136+
* to attach metadata or configuration to modules.
137+
*
138+
* @example
139+
* // In a plugin
140+
* module.locals.registrationDate = Date.now();
141+
* module.locals.version = "1.0.0";
142+
* module.locals.permissions = ["ADMIN", "MODERATE"];
143+
*
144+
* @example
145+
* // In module execution
146+
* console.log(`Command registered on: ${new Date(module.locals.registrationDate)}`);
147+
*
148+
* @example
149+
* // Storing localization data
150+
* module.locals.translations = {
151+
* en: "Hello",
152+
* es: "Hola",
153+
* fr: "Bonjour"
154+
* };
155+
*
156+
* @example
157+
* // Storing command metadata
158+
* module.locals.metadata = {
159+
* category: "admin",
160+
* cooldown: 5000,
161+
* requiresPermissions: true
162+
* };
163+
*
164+
* @remarks
165+
* - The locals object is initialized as an empty object ({}) by default
166+
* - Keys should be namespaced to avoid collisions between plugins
167+
* - Values can be of any type
168+
* - Data persists for the lifetime of the module
169+
* - Commonly used by InitPlugins during module initialization
170+
*
171+
* @best-practices
172+
* 1. Namespace your keys to avoid conflicts:
173+
* ```typescript
174+
* module.locals['myPlugin:data'] = value;
175+
* ```
176+
*
177+
* 2. Document the data structure you're storing:
178+
* ```typescript
179+
* interface MyPluginData {
180+
* version: string;
181+
* timestamp: number;
182+
* }
183+
* module.locals['myPlugin:data'] = {
184+
* version: '1.0.0',
185+
* timestamp: Date.now()
186+
* } as MyPluginData;
187+
* ```
188+
*
189+
* 3. Use type-safe accessors when possible:
190+
* ```typescript
191+
* const getPluginData = (module: Module): MyPluginData =>
192+
* module.locals['myPlugin:data'];
193+
* ```
194+
*/
195+
locals: Dictionary;
45196
execute(...args: any[]): Awaitable<any>;
46197
}
47198

src/types/utility.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Module } from './core-modules';
33
import type { Result } from '../core/structures/result';
44

55
export type Awaitable<T> = PromiseLike<T> | T;
6+
export type Dictionary = Record<string, unknown>
67

78
export type VoidResult = Result<void, void>;
89
export type AnyFunction = (...args: any[]) => unknown;

0 commit comments

Comments
 (0)