diff --git a/src/core/contracts/index.ts b/src/core/contracts/index.ts index ae2e677..d417ce1 100644 --- a/src/core/contracts/index.ts +++ b/src/core/contracts/index.ts @@ -1,4 +1,5 @@ export * from './error-handling'; export * from './logging'; export * from './module-manager'; -export * from './module-store' +export * from './module-store'; +export * from './init'; diff --git a/src/core/contracts/init.ts b/src/core/contracts/init.ts new file mode 100644 index 0000000..3d8810c --- /dev/null +++ b/src/core/contracts/init.ts @@ -0,0 +1,9 @@ +import { Awaitable } from '../../shared'; + +/** + * Represents an initialization contract. + * Let dependencies implement this to initiate some logic. + */ +export interface Init { + init() : Awaitable +} diff --git a/src/core/contracts/module-manager.ts b/src/core/contracts/module-manager.ts index d856c2a..70cb359 100644 --- a/src/core/contracts/module-manager.ts +++ b/src/core/contracts/module-manager.ts @@ -1,4 +1,4 @@ -import { CommandModule } from "../types/modules"; +import { CommandModule } from '../types/modules'; /** * @since 2.0.0 diff --git a/src/core/contracts/module-store.ts b/src/core/contracts/module-store.ts index bd689db..c008b9a 100644 --- a/src/core/contracts/module-store.ts +++ b/src/core/contracts/module-store.ts @@ -1,4 +1,6 @@ - +/** + * Represents a core module store that stores IDs mapped to file paths. + */ export interface CoreModuleStore { commands : Map; } diff --git a/src/core/ioc/base.ts b/src/core/ioc/base.ts index 1f67926..069febb 100644 --- a/src/core/ioc/base.ts +++ b/src/core/ioc/base.ts @@ -1,7 +1,7 @@ -import * as assert from "assert"; -import { composeRoot, useContainer } from "./dependency-injection"; -import { DependencyConfiguration } from "./types"; -import { CoreContainer } from "../structures/container"; +import * as assert from 'assert'; +import { composeRoot, useContainer } from './dependency-injection'; +import { Dependencies, DependencyConfiguration } from './types'; +import { CoreContainer } from '../structures/container'; //SIDE EFFECT: GLOBAL DI @@ -28,7 +28,7 @@ export async function makeDependencies( ) { //Until there are more optional dependencies, just check if the logger exists //SIDE EFFECT - containerSubject = new CoreContainer() + containerSubject = new CoreContainer(); await composeRoot(containerSubject, conf); return useContainer(); diff --git a/src/core/ioc/dependency-injection.ts b/src/core/ioc/dependency-injection.ts index dde7a66..007593e 100644 --- a/src/core/ioc/dependency-injection.ts +++ b/src/core/ioc/dependency-injection.ts @@ -1,4 +1,4 @@ -import type { DependencyConfiguration, MapDeps, IntoDependencies } from './types'; +import type { DependencyConfiguration, MapDeps, IntoDependencies, Dependencies, CoreDependencies } from './types'; import { DefaultLogging } from '../structures'; import { SernError } from '../structures/errors'; import { useContainerRaw } from './base'; @@ -24,14 +24,14 @@ export function single(cb: () => T) { export function transient(cb: () => () => T) { return cb; } -export function Service(key: string) : unknown -export function Service(key: T) { - return useContainerRaw().get(key)! + +export function Service(key: T) { + return useContainerRaw().get(key)!; } export function Services(...keys: [...T]) { const container = useContainerRaw(); - return keys.map(k => container.get(k)!) as IntoDependencies + return keys.map(k => container.get(k)!) as IntoDependencies; } /** @@ -56,13 +56,13 @@ export async function composeRoot( try { container.get('@sern/client'); } catch { - throw new Error(SernError.MissingRequired + " No client was provided") + throw new Error(SernError.MissingRequired + ' No client was provided'); } if (!hasLogger) { container.get('@sern/logger')?.info({ message: 'All dependencies loaded successfully.' }); } - + container.ready(); } diff --git a/src/core/ioc/index.ts b/src/core/ioc/index.ts index 1d0c582..747385d 100644 --- a/src/core/ioc/index.ts +++ b/src/core/ioc/index.ts @@ -1,3 +1,3 @@ export { useContainerRaw, makeDependencies } from './base'; export { Service, Services, single, transient } from './dependency-injection'; -export type { Singleton, Transient } from './types' +export type { Singleton, Transient } from './types'; diff --git a/src/core/ioc/ioc.d.ts b/src/core/ioc/ioc.d.ts deleted file mode 100644 index 166bc5e..0000000 --- a/src/core/ioc/ioc.d.ts +++ /dev/null @@ -1,16 +0,0 @@ - -type Singleton = () => T; -type Transient = () => () => T; - -interface CoreDependencies { - '@sern/logger'?: Singleton; - '@sern/emitter': Singleton; - '@sern/store': Singleton; - '@sern/modules': Singleton; - '@sern/errors': Singleton; -} - -interface Dependencies extends CoreDependencies { - '@sern/client': Singleton; -} - diff --git a/src/core/ioc/types.ts b/src/core/ioc/types.ts index 3f58295..a231427 100644 --- a/src/core/ioc/types.ts +++ b/src/core/ioc/types.ts @@ -1,12 +1,20 @@ -import { Container, UnpackFunction } from "iti"; - +import { Container, UnpackFunction } from 'iti'; +import * as Contract from '../contracts'; export type Singleton = () => T; export type Transient = () => () => T; +export interface CoreDependencies { + '@sern/logger'?: Singleton; + '@sern/emitter': Singleton; + '@sern/store': Singleton; + '@sern/modules': Singleton; + '@sern/errors': Singleton; +} - - +export interface Dependencies extends CoreDependencies { + '@sern/client': Singleton; +} export type DependencyFromKey = Dependencies[T]; export type IntoDependencies = { @@ -31,4 +39,3 @@ export type MapDeps = T ] : [never]; - diff --git a/src/core/module-loading.ts b/src/core/module-loading.ts index 2a0fa8b..a1a66f9 100644 --- a/src/core/module-loading.ts +++ b/src/core/module-loading.ts @@ -5,7 +5,7 @@ import { type Observable, from, mergeMap, ObservableInput } from 'rxjs'; import { readdir, stat } from 'fs/promises'; import { basename, join, resolve } from 'path'; import { ImportPayload } from '../handler/types'; -import * as assert from 'node:assert' +import * as assert from 'node:assert'; import { sernMeta } from '../handler/commands'; export type ModuleResult = Promise, SernError>>; @@ -21,8 +21,8 @@ export async function defaultModuleLoader(absPath: string): Mo if (module === undefined) { return Err(SernError.UndefinedModule); } - - assert.ok(module.type > 0 && module.type < 1<<10, "Found a module that does not have a valid type"); + //todo readd class modules + assert.ok(module.type > 0 && module.type < 1<<10, 'Found a module that does not have a valid type'); assert.ok(module[sernMeta], "Found a module that isn't marked with sernMeta"); return Ok({ module, absPath }); diff --git a/src/core/operators.ts b/src/core/operators.ts index 5ebdc86..adf397e 100644 --- a/src/core/operators.ts +++ b/src/core/operators.ts @@ -20,7 +20,7 @@ import { import { Result } from 'ts-results-es'; import { EventEmitter } from 'node:events'; import { ErrorHandling, Logging } from './contracts'; -import util from 'node:util' +import util from 'node:util'; import { Awaitable } from '../shared'; import { PluginResult, VoidResult } from './types/plugins'; /** diff --git a/src/core/structures/container.ts b/src/core/structures/container.ts index 45fbf04..0a7120e 100644 --- a/src/core/structures/container.ts +++ b/src/core/structures/container.ts @@ -1,10 +1,10 @@ -import { Container } from "iti"; -import { DefaultErrorHandling, DefaultModuleManager, SernEmitter } from "../"; -import { isAsyncFunction} from "node:util/types"; -import * as assert from 'node:assert' -import { Subject } from "rxjs"; -import { ModuleStore } from "./module-store"; -import { Dependencies } from "../ioc/types"; +import { Container } from 'iti'; +import { DefaultErrorHandling, DefaultModuleManager, SernEmitter } from '../'; +import { isAsyncFunction} from 'node:util/types'; +import * as assert from 'node:assert'; +import { Subject } from 'rxjs'; +import { ModuleStore } from './module-store'; +import { Dependencies } from '../ioc/types'; /** * Provides all the defaults for sern to function properly. @@ -23,13 +23,13 @@ export class CoreContainer> extends Container new SernEmitter(), '@sern/store': () => new ModuleStore(), }).add(ctx => { - return { '@sern/modules': () => new DefaultModuleManager(ctx["@sern/store"]) }; - }) + return { '@sern/modules': () => new DefaultModuleManager(ctx['@sern/store']) }; + }); } private listenForInsertions() { - assert.notEqual(this.isReady(), "listening for init functions should only occur prior to sern being ready."); - const unsubscriber = this.on('containerUpserted', e => this.callInitHooks(e)); + assert.ok(!this.isReady(), 'listening for init functions should only occur prior to sern being ready.'); + const unsubscriber = this.on('containerUpserted', this.callInitHooks); this.ready$.subscribe({ complete: unsubscriber diff --git a/src/core/structures/context.ts b/src/core/structures/context.ts index 1acaf7f..9a40d8e 100644 --- a/src/core/structures/context.ts +++ b/src/core/structures/context.ts @@ -19,6 +19,9 @@ import { ReplyOptions } from '../../shared'; * Message and ChatInputCommandInteraction */ export class Context extends CoreContext { + /* + * @Experimental + */ get options() { return this.interaction.options; } @@ -81,7 +84,7 @@ export class Context extends CoreContext { if ('interaction' in wrappable) { return new Context(Ok(wrappable)); } - assert.ok(wrappable.isChatInputCommand()) + assert.ok(wrappable.isChatInputCommand()); return new Context(Err(wrappable)); } } diff --git a/src/core/structures/index.ts b/src/core/structures/index.ts index 391c18e..08fbfba 100644 --- a/src/core/structures/index.ts +++ b/src/core/structures/index.ts @@ -1,5 +1,5 @@ export * from './enums'; export * from './context'; export * from './sern-emitter'; -export * from './services' -export * from './module-store' +export * from './services'; +export * from './module-store'; diff --git a/src/core/structures/module-store.ts b/src/core/structures/module-store.ts index bc81fe9..18c56a4 100644 --- a/src/core/structures/module-store.ts +++ b/src/core/structures/module-store.ts @@ -1,4 +1,4 @@ -import { CoreModuleStore } from "../contracts"; +import { CoreModuleStore } from '../contracts'; /* * @internal diff --git a/src/core/structures/sern-emitter.ts b/src/core/structures/sern-emitter.ts index 3acb54b..44bff6f 100644 --- a/src/core/structures/sern-emitter.ts +++ b/src/core/structures/sern-emitter.ts @@ -9,7 +9,7 @@ import { Module } from '../types/modules'; export class SernEmitter extends EventEmitter { constructor() { - super({ captureRejections: true }) + super({ captureRejections: true }); } /** * Listening to sern events with on. This event stays on until a crash or a normal exit diff --git a/src/core/structures/services/error-handling.ts b/src/core/structures/services/error-handling.ts index 8e8ffcf..9827de2 100644 --- a/src/core/structures/services/error-handling.ts +++ b/src/core/structures/services/error-handling.ts @@ -1,4 +1,4 @@ -import { ErrorHandling } from "../../contracts"; +import { ErrorHandling } from '../../contracts'; /** * @internal diff --git a/src/core/structures/services/logger.ts b/src/core/structures/services/logger.ts index f0781be..5de19c7 100644 --- a/src/core/structures/services/logger.ts +++ b/src/core/structures/services/logger.ts @@ -1,4 +1,4 @@ -import { LogPayload, Logging } from "../../contracts"; +import { LogPayload, Logging } from '../../contracts'; /** * @internal diff --git a/src/core/structures/services/module-manager.ts b/src/core/structures/services/module-manager.ts index b7af14a..55cdad0 100644 --- a/src/core/structures/services/module-manager.ts +++ b/src/core/structures/services/module-manager.ts @@ -1,6 +1,6 @@ -import { CoreModuleStore, ModuleManager } from "../../contracts"; -import { importModule } from "../../module-loading"; -import { CommandModule } from "../../types/modules"; +import { CoreModuleStore, ModuleManager } from '../../contracts'; +import { importModule } from '../../module-loading'; +import { CommandModule } from '../../types/modules'; /** * @internal diff --git a/src/handler/commands.ts b/src/handler/commands.ts index 3259033..689a3a7 100644 --- a/src/handler/commands.ts +++ b/src/handler/commands.ts @@ -5,8 +5,8 @@ import { CommandModule, EventModule, InputCommand, InputEvent } from '../core/ty import { partition } from '../core/functions'; import { Awaitable } from '../shared'; export const sernMeta = Symbol('@sern/meta'); -export const UNREGISTERED = "meow meow meow"; -export const EMPTY_PATH = "purr purr purr"; +export const UNREGISTERED = 'meow meow meow'; +export const EMPTY_PATH = 'purr purr purr'; /** * @since 1.0.0 The wrapper function to define command modules for sern * @param mod diff --git a/src/handler/events/dispatchers.ts b/src/handler/events/dispatchers.ts index 611eeba..cc59fdb 100644 --- a/src/handler/events/dispatchers.ts +++ b/src/handler/events/dispatchers.ts @@ -29,7 +29,7 @@ export function dispatchMessage(module: Processed, args: [Context return { module, args - } + }; } export function dispatchAutocomplete(payload: { module: Processed, event: AutocompleteInteraction }) { diff --git a/src/handler/events/generic.ts b/src/handler/events/generic.ts index de41c29..b73f152 100644 --- a/src/handler/events/generic.ts +++ b/src/handler/events/generic.ts @@ -1,10 +1,9 @@ import { Interaction, - InteractionType, Message, } from 'discord.js'; import { EMPTY, Observable, concatMap, filter, from, of, throwError, tap, MonoTypeOperatorFunction } from 'rxjs'; -import { CommandType, EventType, 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'; @@ -66,8 +65,8 @@ export function createMessageHandler( return defaultModuleLoader>(fullPath) .then(result => { const args = contextArgs(event, rest); - return result.map(payload => dispatchMessage(payload.module, args)) - }) + return result.map(payload => dispatchMessage(payload.module, args)); + }); }); } /** @@ -78,11 +77,11 @@ function assignDefaults(): MonoTypeOperatorFunction { module.name ??= Files.filename(absPath); - module.description ??= "..."; + module.description ??= '...'; module[sernMeta].fullPath = absPath; - module[sernMeta].id = `${module.name}_${uniqueId(module.type)}` + module[sernMeta].id = `${module.name}_${uniqueId(module.type)}`; } - ) + ); } export function buildModules( diff --git a/src/handler/events/messages.ts b/src/handler/events/messages.ts index a3e683e..bbb0c67 100644 --- a/src/handler/events/messages.ts +++ b/src/handler/events/messages.ts @@ -25,7 +25,7 @@ export function makeMessageHandler( defaultPrefix: string | undefined, ) { if (!defaultPrefix) { - log?.debug({ message: 'No prefix found. message handler shut down' }); + log?.debug({ message: 'No prefix found. message handler shutting down' }); return EMPTY; } const messageStream$ = sharedObservable(client, 'messageCreate'); diff --git a/src/handler/events/user-defined.ts b/src/handler/events/user-defined.ts index 437aec3..03feea2 100644 --- a/src/handler/events/user-defined.ts +++ b/src/handler/events/user-defined.ts @@ -8,6 +8,7 @@ import { buildModules, callInitPlugins } from './generic'; import { handleError } from '../../core/operators'; import { Service, useContainerRaw } from '../../core/ioc'; import { DependencyList, Processed } from '../types'; +import { Dependencies } from '../../core/ioc/types'; @@ -24,7 +25,7 @@ export function makeEventsHandler( case EventType.Discord: return eventDispatcher(e, client); case EventType.External: - return eventDispatcher(e, Service(e.emitter)); + return eventDispatcher(e, Service(e.emitter as keyof Dependencies)); default: return err.crash( Error(SernError.InvalidModuleType + ' while creating event handler'), diff --git a/src/handler/id.ts b/src/handler/id.ts index 2ba752d..92a50ee 100644 --- a/src/handler/id.ts +++ b/src/handler/id.ts @@ -1,5 +1,5 @@ -import { Interaction, InteractionType } from "discord.js"; -import { CommandType, EventType } from "../core"; +import { Interaction, InteractionType } from 'discord.js'; +import { CommandType, EventType } from '../core'; /** * Creates a unique ID for a given interaction object. diff --git a/src/handler/types.ts b/src/handler/types.ts index eda3d83..2cee378 100644 --- a/src/handler/types.ts +++ b/src/handler/types.ts @@ -1,6 +1,6 @@ -import { ErrorHandling, Logging, ModuleManager, SernEmitter } from "../core"; -import EventEmitter from "node:events"; -import { Module } from "../core/types/modules"; +import { ErrorHandling, Logging, ModuleManager, SernEmitter } from '../core'; +import EventEmitter from 'node:events'; +import { Module } from '../core/types/modules'; export type Processed = T & { name: string; description: string }; diff --git a/src/index.ts b/src/index.ts index 61624f7..db36141 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ export * as Sern from './handler/sern'; export * from './core'; -export { commandModule, eventModule, discordEvent } from './handler/commands' +export { commandModule, eventModule, discordEvent } from './handler/commands'; export { controller } from './handler/sern'; -export type { Wrapper, Args } from './shared' +export type { Wrapper, Args } from './shared'; diff --git a/src/shared.ts b/src/shared.ts index 6e89a12..2b259ca 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -5,6 +5,7 @@ import type { } from 'discord.js'; import { PayloadType } from './core'; import { AnyModule } from './core/types/modules'; +import { Dependencies } from './core/ioc/types'; export type ReplyOptions = | string diff --git a/tsup.config.js b/tsup.config.js index 6806f4e..e1d7160 100644 --- a/tsup.config.js +++ b/tsup.config.js @@ -54,9 +54,9 @@ export default defineConfig([ }, { dts: { - only: true, - entry: 'src/index.ts' + only: true }, + entry: ['src/index.ts'], outDir: 'dist' } ]);