mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
consolidate interfaces in single file
This commit is contained in:
@@ -1,9 +0,0 @@
|
||||
//i deleted it, hmm so how should we allow users to enable localization?
|
||||
// a
|
||||
import type { AnyFunction } from '../../types/utility';
|
||||
|
||||
export interface Emitter {
|
||||
addListener(eventName: string | symbol, listener: AnyFunction): this;
|
||||
removeListener(eventName: string | symbol, listener: AnyFunction): this;
|
||||
emit(eventName: string | symbol, ...payload: any[]): boolean;
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
/**
|
||||
* @since 2.0.0
|
||||
*/
|
||||
export interface ErrorHandling {
|
||||
/**
|
||||
* @deprecated
|
||||
* Version 4 will remove this method
|
||||
*/
|
||||
crash(err: Error): never;
|
||||
/**
|
||||
* A function that is called on every throw.
|
||||
* @param error
|
||||
*/
|
||||
updateAlive(error: Error): void;
|
||||
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
|
||||
/**
|
||||
* Represents an initialization contract.
|
||||
* Let dependencies implement this to initiate some logic.
|
||||
*/
|
||||
export interface Init {
|
||||
init(): unknown;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a Disposable contract.
|
||||
* Let dependencies implement this to dispose and cleanup.
|
||||
*/
|
||||
export interface Disposable {
|
||||
dispose(): unknown;
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
export * from './error-handling';
|
||||
export * from './logging';
|
||||
export * from './hooks';
|
||||
export * from './emitter';
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
/**
|
||||
* @since 2.0.0
|
||||
*/
|
||||
export interface Logging<T = unknown> {
|
||||
error(payload: LogPayload<T>): void;
|
||||
warning(payload: LogPayload<T>): void;
|
||||
info(payload: LogPayload<T>): void;
|
||||
debug(payload: LogPayload<T>): void;
|
||||
}
|
||||
|
||||
export type LogPayload<T = unknown> = { message: T };
|
||||
@@ -8,17 +8,12 @@ import { CommandType, EventType } from './structures';
|
||||
*/
|
||||
export function reconstruct<T extends Interaction>(event: T) {
|
||||
switch (event.type) {
|
||||
case InteractionType.MessageComponent: {
|
||||
return [`${event.customId}_C${event.componentType}`];
|
||||
}
|
||||
case InteractionType.MessageComponent: return [`${event.customId}_C${event.componentType}`];
|
||||
case InteractionType.ApplicationCommand:
|
||||
case InteractionType.ApplicationCommandAutocomplete: {
|
||||
case InteractionType.ApplicationCommandAutocomplete:
|
||||
return [`${event.commandName}_A${event.commandType}`, `${event.commandName}_B`];
|
||||
}
|
||||
//Modal interactions are classified as components for sern
|
||||
case InteractionType.ModalSubmit: {
|
||||
return [`${event.customId}_M`];
|
||||
}
|
||||
case InteractionType.ModalSubmit: return [`${event.customId}_M`];
|
||||
}
|
||||
}
|
||||
/**
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export * from './contracts';
|
||||
export * from './interfaces';
|
||||
export * from './create-plugins';
|
||||
export * from './structures';
|
||||
export * from './ioc';
|
||||
|
||||
54
src/core/interfaces.ts
Normal file
54
src/core/interfaces.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import type { AnyFunction } from '../types/utility';
|
||||
|
||||
|
||||
/**
|
||||
* Represents an initialization contract.
|
||||
* Let dependencies implement this to initiate some logic.
|
||||
*/
|
||||
export interface Init {
|
||||
init(): unknown;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a Disposable contract.
|
||||
* Let dependencies implement this to dispose and cleanup.
|
||||
*/
|
||||
export interface Disposable {
|
||||
dispose(): unknown;
|
||||
}
|
||||
|
||||
export interface Emitter {
|
||||
addListener(eventName: string | symbol, listener: AnyFunction): this;
|
||||
removeListener(eventName: string | symbol, listener: AnyFunction): this;
|
||||
emit(eventName: string | symbol, ...payload: any[]): boolean;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @since 2.0.0
|
||||
*/
|
||||
export interface ErrorHandling {
|
||||
/**
|
||||
* @deprecated
|
||||
* Version 4 will remove this method
|
||||
*/
|
||||
crash(err: Error): never;
|
||||
/**
|
||||
* A function that is called on every throw.
|
||||
* @param error
|
||||
*/
|
||||
updateAlive(error: Error): void;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 2.0.0
|
||||
*/
|
||||
export interface Logging<T = unknown> {
|
||||
error(payload: LogPayload<T>): void;
|
||||
warning(payload: LogPayload<T>): void;
|
||||
info(payload: LogPayload<T>): void;
|
||||
debug(payload: LogPayload<T>): void;
|
||||
}
|
||||
|
||||
export type LogPayload<T = unknown> = { message: T };
|
||||
@@ -5,7 +5,7 @@ import { CoreContainer } from './container';
|
||||
import { Result } from 'ts-results-es';
|
||||
import { __Services } from '../_internal';
|
||||
import { AnyFunction } from '../../types/utility';
|
||||
import type { Logging } from '../contracts/logging';
|
||||
import type { Logging } from '../interfaces';
|
||||
import type { UnpackFunction } from 'iti';
|
||||
//SIDE EFFECT: GLOBAL DI
|
||||
let containerSubject: CoreContainer<Partial<Dependencies>>;
|
||||
|
||||
@@ -6,7 +6,7 @@ import { createRequire } from 'node:module';
|
||||
import type { ImportPayload, Wrapper } from '../types/core';
|
||||
import type { Module } from '../types/core-modules';
|
||||
import { existsSync } from 'fs';
|
||||
import type { Logging } from './contracts/logging';
|
||||
import type { Logging } from './interfaces';
|
||||
|
||||
|
||||
export const parseCallsite = (fpath: string) => {
|
||||
@@ -76,7 +76,7 @@ export const getFullPathTree = (dir: string) => readPaths(path.resolve(dir));
|
||||
|
||||
export const filename = (p: string) => fmtFileName(path.basename(p));
|
||||
|
||||
const validExtensions = ['.js', '.cjs', '.mts', '.mjs', '.cts', '.ts', ''];
|
||||
const validExtensions = ['.js', '.ts', ''];
|
||||
const isSkippable = (filename: string) => {
|
||||
//empty string is for non extension files (directories)
|
||||
return filename[0] === '!' || !validExtensions.includes(path.extname(filename));
|
||||
@@ -90,23 +90,19 @@ async function deriveFileInfo(dir: string, file: string) {
|
||||
}
|
||||
|
||||
async function* readPaths(dir: string): AsyncGenerator<string> {
|
||||
try {
|
||||
const files = await readdir(dir);
|
||||
for (const file of files) {
|
||||
const { fullPath, fileStats, base } = await deriveFileInfo(dir, file);
|
||||
if (fileStats.isDirectory()) {
|
||||
//Todo: refactor so that i dont repeat myself for files (line 71)
|
||||
if (!isSkippable(base)) {
|
||||
yield* readPaths(fullPath);
|
||||
}
|
||||
} else {
|
||||
if (!isSkippable(base)) {
|
||||
yield 'file:///' + fullPath;
|
||||
}
|
||||
const files = await readdir(dir);
|
||||
for (const file of files) {
|
||||
const { fullPath, fileStats, base } = await deriveFileInfo(dir, file);
|
||||
if (fileStats.isDirectory()) {
|
||||
//Todo: refactor so that i dont repeat myself for files (line 71)
|
||||
if (!isSkippable(base)) {
|
||||
yield* readPaths(fullPath);
|
||||
}
|
||||
} else {
|
||||
if (!isSkippable(base)) {
|
||||
yield 'file:///' + fullPath;
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,6 @@ import { ClientEvents } from 'discord.js';
|
||||
import { EventType } from '../core/structures';
|
||||
import type { AnyEventPlugin, } from '../types/core-plugin';
|
||||
import type {
|
||||
CommandModule,
|
||||
EventModule,
|
||||
InputCommand,
|
||||
InputEvent,
|
||||
} from '../types/core-modules';
|
||||
|
||||
@@ -16,7 +16,7 @@ import {
|
||||
pipe,
|
||||
share,
|
||||
} from 'rxjs';
|
||||
import { Emitter, ErrorHandling, Logging } from './contracts';
|
||||
import { Emitter, ErrorHandling, Logging } from './interfaces';
|
||||
import util from 'node:util';
|
||||
import type { PluginResult } from '../types/core-plugin';
|
||||
import type { Result } from 'ts-results-es'
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { ActivitiesOptions } from "discord.js";
|
||||
import type { IntoDependencies } from "../types/ioc";
|
||||
import type { Emitter } from "./contracts/emitter";
|
||||
import type { Emitter } from "./interfaces";
|
||||
|
||||
type Status = 'online' | 'idle' | 'invisible' | 'dnd'
|
||||
type PresenceReduce = (previous: Result) => Result;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ErrorHandling } from '../../contracts';
|
||||
import { ErrorHandling } from '../../interfaces';
|
||||
|
||||
/**
|
||||
* @internal
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { LogPayload, Logging } from '../../contracts';
|
||||
import { LogPayload, Logging } from '../../interfaces';
|
||||
|
||||
/**
|
||||
* @internal
|
||||
|
||||
@@ -61,10 +61,8 @@ export function createDispatcher(payload: {
|
||||
module: Processed<CommandModule>;
|
||||
event: BaseInteraction;
|
||||
}) {
|
||||
assert.ok(
|
||||
CommandType.Text !== payload.module.type,
|
||||
SernError.MismatchEvent + 'Found text command in interaction stream',
|
||||
);
|
||||
assert.ok(CommandType.Text !== payload.module.type,
|
||||
SernError.MismatchEvent + 'Found text command in interaction stream');
|
||||
switch (payload.module.type) {
|
||||
case CommandType.Slash:
|
||||
case CommandType.Both: {
|
||||
|
||||
@@ -85,7 +85,7 @@ export function createInteractionHandler<T extends Interaction>(
|
||||
export function createMessageHandler(
|
||||
source: Observable<Message>,
|
||||
defaultPrefix: string,
|
||||
mg: any, //TODO
|
||||
mg: any,
|
||||
) {
|
||||
return createGenericHandler(source, async event => {
|
||||
const [prefix, ...rest] = fmt(event.content, defaultPrefix);
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
import { ObservableInput, concat, first, fromEvent, ignoreElements, pipe, tap } from 'rxjs';
|
||||
import { _Module } from '../core/_internal';
|
||||
import { Logging, } from '../core/contracts';
|
||||
import { Logging, } from '../core/interfaces';
|
||||
import type { DependencyList } from '../types/ioc';
|
||||
|
||||
const once = (log: Logging | undefined) => pipe(
|
||||
tap(() => { log?.info({ message: "Waiting on discord client to be ready..." }) }),
|
||||
first(),
|
||||
ignoreElements())
|
||||
|
||||
export function readyHandler(
|
||||
[sEmitter, , log ,, client]: DependencyList,
|
||||
allPaths: ObservableInput<string>,
|
||||
@@ -17,7 +22,4 @@ export function readyHandler(
|
||||
// `Found ${module.name} at ${module.meta.fullPath}, which does not have a valid type`);
|
||||
}
|
||||
|
||||
const once = (log: Logging | undefined) => pipe(
|
||||
tap(() => { log?.info({ message: "Waiting on discord client to be ready..." }) }),
|
||||
first(),
|
||||
ignoreElements())
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ export type { Wrapper } from './types/core';
|
||||
|
||||
export type { Args, SlashOptions, Payload, SernEventsMapping } from './types/utility';
|
||||
|
||||
export type { Singleton, Transient, CoreDependencies, Initializable } from './types/ioc';
|
||||
export type { Singleton, Transient, CoreDependencies } from './types/ioc';
|
||||
|
||||
export {
|
||||
commandModule,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Container, UnpackFunction } from 'iti';
|
||||
import * as Contracts from '../core/contracts';
|
||||
import * as Contracts from '../core/interfaces';
|
||||
/**
|
||||
* Type to annotate that something is a singleton.
|
||||
* T is created once and lazily.
|
||||
|
||||
Reference in New Issue
Block a user