feat: Adding docs to some data structures, moving to default from export files

This commit is contained in:
Jacob Nguyen
2022-05-25 17:17:55 -05:00
parent 9eef9dd318
commit 0ae541daba
8 changed files with 38 additions and 33 deletions

View File

@@ -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';

View File

@@ -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 {

View File

@@ -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;
}

View File

@@ -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,

View File

@@ -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<T extends keyof SernEventsMapping>(
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<T extends keyof SernEventsMapping>(
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<T extends keyof SernEventsMapping>(
eventName: T,
...args: SernEventsMapping[T]

View File

@@ -24,9 +24,8 @@ function firstSome<T>(...args: Option<T>[]): Nullish<T> {
//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();

View File

@@ -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 = <Module>require(absPath).module;
const mod = <Module>require(absPath).default;
return { mod, absPath };
}),
);

View File

@@ -1,6 +1,5 @@
import type { Awaitable, ClientEvents, CommandInteractionOptionResolver } from 'discord.js';
import type { EventEmitter } from 'events';
// Anything that can be sent in a `<TextChannel>#send` or `<CommandInteraction>#reply`
export type Nullish<T> = T | undefined | null;
// Thanks @cursorsdottsx
export type ParseType<T> = {