diff --git a/src/core/id.ts b/src/core/id.ts index 17cf579d..9591b3e0 100644 --- a/src/core/id.ts +++ b/src/core/id.ts @@ -20,7 +20,6 @@ export function reconstruct(event: T) { case InteractionType.MessageComponent: { let id = event.customId; const data = parseParams(event, id, `_C${event.componentType}`) - console.log(data) return [data]; } case InteractionType.ApplicationCommand: diff --git a/src/core/ioc/base.ts b/src/core/ioc/base.ts index 3ac05c3e..7c0c4809 100644 --- a/src/core/ioc/base.ts +++ b/src/core/ioc/base.ts @@ -65,7 +65,7 @@ async function composeRoot( conf: DependencyConfiguration, ) { //container should have no client or logger yet. - const hasLogger = conf.exclude?.has('@sern/logger'); + const hasLogger = container.hasKey('@sern/logger'); if (!hasLogger) { __add_container('@sern/logger', new __Services.DefaultLogging()); } diff --git a/src/handlers/event-utils.ts b/src/handlers/event-utils.ts index 6d51fe3e..1a876a15 100644 --- a/src/handlers/event-utils.ts +++ b/src/handlers/event-utils.ts @@ -21,7 +21,8 @@ import { resultPayload, isAutocomplete, treeSearch } from '../core/functions' interface ExecutePayload { module: Module; args: unknown[]; - deps: Dependencies + deps: Dependencies; + params?: string; [key: string]: unknown } @@ -52,9 +53,10 @@ interface DispatchPayload { module: Processed; event: BaseInteraction; defaultPrefix?: string; - deps: Dependencies + deps: Dependencies; + params?: string }; -export function createDispatcher({ module, event, defaultPrefix, deps }: DispatchPayload): ExecutePayload { +export function createDispatcher({ module, event, defaultPrefix, deps, params }: DispatchPayload): ExecutePayload { assert.ok(CommandType.Text !== module.type, SernError.MismatchEvent + 'Found text command in interaction stream'); @@ -68,13 +70,12 @@ export function createDispatcher({ module, event, defaultPrefix, deps }: Dispatc args: [event], deps }; } - switch (module.type) { case CommandType.Slash: case CommandType.Both: { return { module, args: [Context.wrap(event, defaultPrefix)], deps }; } - default: return { module, args: [event], deps }; + default: return { module, args: [event], deps, params }; } } function createGenericHandler( @@ -113,24 +114,22 @@ export function createInteractionHandler( deps: Dependencies, defaultPrefix?: string ) { - const mg = deps['@sern/modules'] + const mg = deps['@sern/modules']; return createGenericHandler, void>>( source, async event => { const possibleIds = Id.reconstruct(event); let modules = possibleIds - .map(({ id }) => mg.get(id)) - .filter((id): id is Module => id !== undefined); + .map(({ id, params }) => ({ module: mg.get(id), params })) + .filter((id) => id !== undefined); if(modules.length == 0) { return Err.EMPTY; } - const [ module ] = modules; + const [{module, params}] = modules; return Ok(createDispatcher({ module: module as Processed, - event, - defaultPrefix, - deps + event, defaultPrefix, deps, params })); }); } @@ -218,10 +217,10 @@ export async function callInitPlugins(module: Module, deps: Dependencies, sEmitt return _module } -async function callPlugins({ args, module, deps }: ExecutePayload) { +async function callPlugins({ args, module, deps, params }: ExecutePayload) { let state = {}; for(const plugin of module.onEvent??[]) { - const result = await plugin.execute(...args, { state, deps, type: module.type === CommandType.Text?'text':'slash' }); + const result = await plugin.execute(...args, { state, deps, params, type: module.type }); if(result.isErr()) { return result; } @@ -236,9 +235,9 @@ async function callPlugins({ args, module, deps }: ExecutePayload) { * @param onStop emits a failure response to the SernEmitter */ export function intoTask(onStop: (m: Module) => unknown) { - const onNext = ({ args, module, deps }: ExecutePayload, state: Record) => ({ + const onNext = ({ args, module, deps, params }: ExecutePayload, state: Record) => ({ module, - args: [...args, { state, deps, type: module.type === CommandType.Text?'text':'slash' }], + args: [...args, { state, deps, params, type: module.type }], deps }); return createResultResolver({ onStop, onNext }); diff --git a/src/handlers/ready.ts b/src/handlers/ready.ts index c8fa11bc..4dbbdee2 100644 --- a/src/handlers/ready.ts +++ b/src/handlers/ready.ts @@ -1,5 +1,5 @@ import * as Files from '../core/module-loading' -import { once } from 'events'; +import { once } from 'node:events'; import { resultPayload } from '../core/functions'; import { PayloadType } from '..'; import { CommandType } from '../core/structures/enums'; @@ -25,7 +25,6 @@ export default async function(dir: string, deps : UnpackedDependencies) { throw Error(`Found ${module.name} at ${module.meta.absPath}, which has incorrect \`type\``); } const resultModule = await callInitPlugins(module, deps, sEmitter); - console.log(resultModule) // FREEZE! no more writing!! commands.set(resultModule.meta.id, Object.freeze(resultModule)); sEmitter.emit('module.register', resultPayload(PayloadType.Success, resultModule)); diff --git a/src/types/core-modules.ts b/src/types/core-modules.ts index 87327456..911e5828 100644 --- a/src/types/core-modules.ts +++ b/src/types/core-modules.ts @@ -24,7 +24,8 @@ import { Awaitable, SernEventsMapping } from './utility'; export type SDT = { state: Record; deps: Dependencies; - type: 'slash' | 'text' + type: 'slash' | 'text', + params?: string }; export type Processed = T & { name: string; description: string }; diff --git a/src/types/core-plugin.ts b/src/types/core-plugin.ts index 60c0b03d..ce3ac059 100644 --- a/src/types/core-plugin.ts +++ b/src/types/core-plugin.ts @@ -13,12 +13,13 @@ import type { Err, Ok, Result } from 'ts-results-es'; import type { + CommandModuleDefs, Module, Processed, SDT, } from './core-modules'; import type { Awaitable } from './utility'; -import type { CommandType, PluginType } from '../core/structures/enums' +import type { CommandType, EventType, PluginType } from '../core/structures/enums' import type { Context } from '../core/structures/context' import type { ButtonInteraction, diff --git a/src/types/ioc.ts b/src/types/ioc.ts index d3aa6711..3803a644 100644 --- a/src/types/ioc.ts +++ b/src/types/ioc.ts @@ -21,11 +21,13 @@ export type IntoDependencies = { /** * @deprecated This old signature will be incompatible with future versions of sern. + * ```ts + * To switch your old code: + await makeDependencies(({ add }) => { + add('@sern/client', new Client()) + }) + * ``` */ export interface DependencyConfiguration { - /* - * @deprecated. Loggers will be opt-in the future - */ - exclude?: Set<'@sern/logger'>; build: (root: Container) => Container; }