diff --git a/src/handler/events/observableHandling.ts b/src/handler/events/observableHandling.ts index eb9b73c..ba3d552 100644 --- a/src/handler/events/observableHandling.ts +++ b/src/handler/events/observableHandling.ts @@ -1,7 +1,6 @@ import type { Message } from 'discord.js'; import { Observable, throwError } from 'rxjs'; import { SernError } from '../structures/errors'; -import { isNotFromBot } from '../utilities/messageHelpers'; import type { Module, ModuleDefs } from '../structures/module'; import { correctModuleType } from '../utilities/predicates'; diff --git a/src/handler/events/readyEvent.ts b/src/handler/events/readyEvent.ts index 026fddb..224a2ae 100644 --- a/src/handler/events/readyEvent.ts +++ b/src/handler/events/readyEvent.ts @@ -82,7 +82,11 @@ export const onReady = (wrapper: Wrapper) => { if (loadedPluginsCorrectly) { const res = registerModule(mod); if (res.err) { - throw Error(SernError.NonValidModuleType); + throw Error( + SernError.NonValidModuleType + + ', or loading modules was handled incorrectly. ' + + 'Check commands path and command files!', + ); } wrapper.sernEmitter?.emit('module.register', { success: true, module: mod }); } else { diff --git a/src/handler/plugins/plugin.ts b/src/handler/plugins/plugin.ts index 4f9bc55..342a7ba 100644 --- a/src/handler/plugins/plugin.ts +++ b/src/handler/plugins/plugin.ts @@ -1,8 +1,8 @@ // // Plugins can be inserted on all commands and are emitted // -// 1.) on ready event, where all commands are loaded. -// 2.) on corresponding observable (command triggers) +// 1. on ready event, where all commands are loaded. +// 2. on corresponding observable (command triggers) // // The goal of plugins is to organize commands and // provide extensions to repetitive patterns @@ -68,10 +68,11 @@ type ModuleNoPlugins = ValueOf<{ //TODO: I WANT BETTER TYPINGS AHHHHHHHHHHHHHHH export function sernModule(plugins: CommandPlugin[], mod: ModuleNoPlugins): Module { - if (mod.type !== CommandType.Autocomplete) + if (mod.type === CommandType.Autocomplete) { + return mod; + } else return { plugins, ...mod, }; - else return mod; } diff --git a/src/handler/sern.ts b/src/handler/sern.ts index 1f49ae6..dbe0af9 100644 --- a/src/handler/sern.ts +++ b/src/handler/sern.ts @@ -1,17 +1,13 @@ import type { DiscordEvent, EventEmitterRegister } from '../types/handler'; -import { ApplicationCommandType, Client } from 'discord.js'; - import type Wrapper from './structures/wrapper'; import { fromEvent } from 'rxjs'; -import { SernError } from './structures/errors'; import { onReady } from './events/readyEvent'; import { onMessageCreate } from './events/messageEvent'; import { onInteractionCreate } from './events/interactionCreate'; -import { match, P } from 'ts-pattern'; import { Err, Ok } from 'ts-results'; -import { CommandType } from './structures/enums'; import { isDiscordEvent } from './utilities/predicates'; +import type { Client } from 'discord.js'; export function init(wrapper: Wrapper) { const { events, client } = wrapper; @@ -33,18 +29,6 @@ function eventObserver(client: Client, events: (DiscordEvent | EventEmitterRegis }); } -export function cmdTypeToDjs(ty: CommandType) { - return match(ty) - .with(CommandType.Slash, () => ApplicationCommandType.ChatInput) - .with(CommandType.MenuUser, () => ApplicationCommandType.User) - .with(CommandType.MenuMsg, () => ApplicationCommandType.Message) - .with(CommandType.Both, () => ApplicationCommandType.ChatInput) - .with(P._, () => { - throw new Error(SernError.NonValidModuleType); - }) - .exhaustive(); -} - export const controller = { next: () => Ok.EMPTY, stop: () => Err.EMPTY, diff --git a/src/handler/sernEmitter.ts b/src/handler/sernEmitter.ts index e4074d5..a628241 100644 --- a/src/handler/sernEmitter.ts +++ b/src/handler/sernEmitter.ts @@ -11,24 +11,34 @@ type SernEventsMapping = { ['error']: [Error | string]; }; -/** - * - */ export default class SernEmitter extends EventEmitter { + /** + * Listening to sern events with on. This event stays on until a crash or a normal exit + * @param eventName + * @param listener what to do with the data + */ public override on( eventName: T, listener: (...args: SernEventsMapping[T][]) => void, ): this { return super.on(eventName, listener); } - + /** + * Listening to sern events with on. This event stays on until a crash or a normal exit + * @param eventName + * @param listener what to do with the data + */ public override once( eventName: T, listener: (...args: SernEventsMapping[T][]) => void, ): this { return super.once(eventName, listener); } - + /** + * Listening to sern events with on. This event stays on until a crash or a normal exit + * @param eventName + * @param args the arguments for emitting the { eventName } + */ public override emit( eventName: T, ...args: SernEventsMapping[T] diff --git a/src/handler/structures/context.ts b/src/handler/structures/context.ts index 4eb3d1b..918c794 100644 --- a/src/handler/structures/context.ts +++ b/src/handler/structures/context.ts @@ -24,9 +24,8 @@ function firstSome(...args: Option[]): Nullish { //Could I refactor with Either monad? /** - * The Context class will provide values that are shared between + * Provides values shared between * Message and ChatInputCommandInteraction - * */ export default class Context { private constructor( @@ -37,11 +36,20 @@ export default class Context { this.oInterac = oInterac; } + /** + * Getting the Message object. Crashes if module type is + * CommandType.Slash or the event fired in a Both command was + * ChatInputCommandInteraction + */ @ExternallyUsed public get message() { return this.oMsg.unwrap(); } - + /** + * Getting the ChatInputCommandInteraction object. Crashes if module type is + * CommandType.Text or the event fired in a Both command was + * Message + */ @ExternallyUsed public get interaction() { return this.oInterac.unwrap(); diff --git a/src/handler/utilities/readFile.ts b/src/handler/utilities/readFile.ts index c41e584..47dbdff 100644 --- a/src/handler/utilities/readFile.ts +++ b/src/handler/utilities/readFile.ts @@ -52,7 +52,7 @@ export function buildData(commandDir: string): Observable<{ return from( getCommands(commandDir).map(absPath => { // eslint-disable-next-line @typescript-eslint/no-var-requires - const mod = require(absPath).module; + const mod = require(absPath).default; return { mod, absPath }; }), ); diff --git a/src/types/handler.ts b/src/types/handler.ts index dd6eb50..fb42a53 100644 --- a/src/types/handler.ts +++ b/src/types/handler.ts @@ -1,6 +1,5 @@ import type { Awaitable, ClientEvents, CommandInteractionOptionResolver } from 'discord.js'; import type { EventEmitter } from 'events'; -// Anything that can be sent in a `#send` or `#reply` export type Nullish = T | undefined | null; // Thanks @cursorsdottsx export type ParseType = {