Presence namespaced types removed

This commit is contained in:
Jacob Nguyen
2024-04-28 23:37:58 -05:00
parent 071d5eac49
commit e6fba9d8b5
11 changed files with 114 additions and 113 deletions

View File

@@ -4,13 +4,19 @@
"version": "3.3.4",
"description": "A complete, customizable, typesafe, & reactive framework for discord bots.",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.mjs",
"import": "./dist/index.js",
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"./internal": {
"import": "./dist/_internal.js",
"require": "./dist/_internal.js",
"types": "./dist/_internal.d.ts"
}
},
"scripts": {

34
src/_internal.ts Normal file
View File

@@ -0,0 +1,34 @@
import type { Interaction } from 'discord.js';
import { mergeMap, merge, concatMap } from 'rxjs';
import { PayloadType } from './core';
import {
isAutocomplete,
isCommand,
isMessageComponent,
isModal,
sharedEventStream,
SernError,
filterTap,
resultPayload,
} from './core/_internal';
import { createInteractionHandler, executeModule, makeModuleExecutor } from './handlers/event-utils';
import type { DependencyList } from './types/ioc';
export function interactionHandler([emitter, err, log, modules, client]: DependencyList) {
const interactionStream$ = sharedEventStream<Interaction>(client, 'interactionCreate');
const handle = createInteractionHandler(interactionStream$, modules);
const interactionHandler$ = merge(
handle(isMessageComponent),
handle(isAutocomplete),
handle(isCommand),
handle(isModal),
);
return interactionHandler$
.pipe(
filterTap(e => emitter.emit('warning', resultPayload(PayloadType.Warning, undefined, e))),
concatMap(makeModuleExecutor(module =>
emitter.emit('module.activate', resultPayload(PayloadType.Failure, module, SernError.PluginFailure)))),
mergeMap(payload => executeModule(emitter, log, err, payload)));
}

View File

@@ -1,9 +1,6 @@
import path from 'node:path';
import assert from 'assert';
import { createRequire } from 'node:module';
import type { Wrapper } from '../types/core';
import { existsSync } from 'fs';
import type { Logging } from './interfaces';
export const parseCallsite = (fpath: string) => {
@@ -54,32 +51,3 @@ export const fmtFileName = (fileName: string) => path.parse(fileName).name;
export const filename = (p: string) => fmtFileName(path.basename(p));
const requir = createRequire(import.meta.url);
export function loadConfig(wrapper: Wrapper | 'file', log: Logging | undefined): Wrapper {
if (wrapper !== 'file') {
return wrapper;
}
log?.info({ message: 'Experimental loading of sern.config.json'});
const config = requir(path.resolve('sern.config.json'));
const makePath = (dir: PropertyKey) =>
config.language === 'typescript'
? path.join('dist', config.paths[dir]!)
: path.join(config.paths[dir]!);
log?.info({ message: 'Loading config: ' + JSON.stringify(config, null, 4) });
const commandsPath = makePath('commands');
log?.info({ message: `Commands path is set to ${commandsPath}` });
let eventsPath: string | undefined;
if (config.paths.events) {
eventsPath = makePath('events');
log?.info({ message: `Events path is set to ${eventsPath} `});
}
return { defaultPrefix: config.defaultPrefix,
commands: commandsPath,
events: eventsPath };
}

View File

@@ -67,9 +67,6 @@ export function discordEvent<T extends keyof ClientEvents>(mod: {
plugins?: AnyEventPlugin[];
execute: (...args: ClientEvents[T]) => Awaitable<unknown>;
}) {
return eventModule({
type: EventType.Discord,
...mod,
});
return eventModule({ type: EventType.Discord, ...mod, });
}

View File

@@ -3,65 +3,63 @@ import type { IntoDependencies } from "../types/ioc";
import type { Emitter } from "./interfaces";
type Status = 'online' | 'idle' | 'invisible' | 'dnd'
type PresenceReduce = (previous: Result) => Result;
type PresenceReduce = (previous: PresenceResult) => PresenceResult;
export interface Result {
export interface PresenceResult {
status?: Status;
afk?: boolean;
activities?: ActivitiesOptions[];
shardId?: number[];
repeat?: number | [Emitter, string];
onRepeat?: (previous: Result) => Result;
onRepeat?: (previous: PresenceResult) => PresenceResult;
}
export type Config <T extends (keyof Dependencies)[]> =
{
export const Presence = {
/**
* A small wrapper to provide type inference.
* Create a Presence module which **MUST** be put in a file called presence.(language-extension)
* adjacent to the file where **Sern.init** is CALLED.
*/
module : <T extends (keyof Dependencies)[]>(conf: PresenceConfig<T>) => conf,
/**
* Create a Presence body which can be either:
* - once, the presence is activated only once.
* - repeated, per cycle or event, the presence can be changed.
*/
of : (root: Omit<PresenceResult, 'repeat' | 'onRepeat'>) => {
return {
/**
* @example
* Presence
* .of({
* activities: [{ name: "deez nuts" }]
* }) //starts the presence with "deez nuts".
* .repeated(prev => {
* return {
* afk: true,
* activities: prev.activities?.map(s => ({ ...s, name: s.name+"s" }))
* };
* }, 10000)) //every 10 s, the callback sets the presence to the returned one.
*/
repeated: (onRepeat: PresenceReduce, repeat: number | [Emitter, string]) => {
return { repeat, onRepeat, ...root }
},
/**
* @example
* Presence
* .of({
* activities: [
* { name: "Chilling out" }
* ]
* })
* .once() // Sets the presence once, with what's provided in '.of()'
*/
once: () => root
};
}
}
export type PresenceConfig <T extends (keyof Dependencies)[]> = {
inject?: [...T]
execute: (...v: IntoDependencies<T>) => Result;
execute: (...v: IntoDependencies<T>) => PresenceResult;
};
/**
* A small wrapper to provide type inference.
* Create a Presence module which **MUST** be put in a file called presence.(language-extension)
* adjacent to the file where **Sern.init** is CALLED.
*/
export const module = <T extends (keyof Dependencies)[]>(conf: Config<T>) => conf;
/**
* Create a Presence body which can be either:
* - once, the presence is activated only once.
* - repeated, per cycle or event, the presence can be changed.
*/
export function of(root: Omit<Result, 'repeat' | 'onRepeat'>) {
return {
/**
* @example
* Presence
* .of({
* activities: [{ name: "deez nuts" }]
* }) //starts the presence with "deez nuts".
* .repeated(prev => {
* return {
* afk: true,
* activities: prev.activities?.map(s => ({ ...s, name: s.name+"s" }))
* };
* }, 10000)) //every 10 s, the callback sets the presence to the returned one.
*/
repeated: (onRepeat: PresenceReduce, repeat: number | [Emitter, string]) => {
return { repeat, onRepeat, ...root }
},
/**
* @example
* Presence
* .of({
* activities: [
* { name: "Chilling out" }
* ]
* })
* .once() // Sets the presence once, with what's provided in '.of()'
*/
once: () => root
};
}

View File

@@ -8,12 +8,10 @@ export class DefaultErrorHandling implements ErrorHandling {
crash(err: Error): never {
throw err;
}
#keepAlive = 1;
keepAlive = 1;
updateAlive(err: Error) {
this.#keepAlive--;
if (this.#keepAlive === 0) {
this.keepAlive--;
if (this.keepAlive === 0) {
throw err;
}
}

View File

@@ -1,12 +1,12 @@
import { concatMap, from, interval, of, map, scan, startWith, fromEvent, take } from "rxjs"
import { Files } from "../core/_internal";
import * as Presence from "../core/presences";
import { PresenceConfig, PresenceResult } from "../core/presences";
import { Services } from "../core/ioc";
import assert from "node:assert";
type SetPresence = (conf: Presence.Result) => Promise<unknown>
type SetPresence = (conf: PresenceResult) => Promise<unknown>
const parseConfig = async (conf: Promise<Presence.Result>) => {
const parseConfig = async (conf: Promise<PresenceResult>) => {
return conf.then(s => {
if('repeat' in s) {
const { onRepeat, repeat } = s;
@@ -25,7 +25,7 @@ const parseConfig = async (conf: Promise<Presence.Result>) => {
export const presenceHandler = (path: string, setPresence: SetPresence) => {
interface PresenceModule {
module: Presence.Config<(keyof Dependencies)[]>
module: PresenceConfig<(keyof Dependencies)[]>
}
const presence = Files
.importModule<PresenceModule>(path)

View File

@@ -50,6 +50,5 @@ export {
discordEvent,
} from './core/modules';
export * as Presence from './core/presences'
export * from './core/presences'

View File

@@ -1,17 +1,6 @@
import { handleCrash } from './handlers/event-utils';
import callsites from 'callsites';
import { Files } from './core/_internal';
import path from 'node:path'
import { merge } from 'rxjs';
import { Services } from './core/ioc';
import { eventsHandler } from './handlers/user-defined-events';
import { readyHandler } from './handlers/ready-event';
import { messageHandler } from './handlers/message-event';
import { interactionHandler } from './handlers/interaction-event';
import { presenceHandler } from './handlers/presence';
import type { Client } from 'discord.js';
import { type Wrapper } from './';
/**
* @since 1.0.0

View File

@@ -11,7 +11,8 @@
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"module": "esnext",
"outDir": "dist",
"module": "commonjs",
"target": "esnext"
},
"exclude": ["node_modules", "dist"],

View File

@@ -1,4 +1,5 @@
import { defineConfig } from 'tsup';
const shared = {
entry: ['src/index.ts'],
external: ['discord.js', 'iti'],
@@ -12,6 +13,16 @@ const shared = {
},
};
export default defineConfig([
{
...shared,
format: ['esm', 'cjs'],
target: 'node18',
tsconfig: './tsconfig.json',
outDir: './dist',
minify: false,
dts: true,
entry: ['src/_internal.ts'],
},
{
format: ['esm', 'cjs'],
target: 'node18',