mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
Compare commits
1 Commits
v4.1.0
...
Murtatrxx-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7cc16ff791 |
27
CHANGELOG.md
27
CHANGELOG.md
@@ -1,32 +1,5 @@
|
||||
# Changelog
|
||||
|
||||
## [4.1.0](https://github.com/sern-handler/handler/compare/v4.0.3...v4.1.0) (2025-01-06)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* moduleinfo-in-eventplugins ([#373](https://github.com/sern-handler/handler/issues/373)) ([220a60e](https://github.com/sern-handler/handler/commit/220a60ecf853df8d288de2533c669562a430c3f9))
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* update github username ([#371](https://github.com/sern-handler/handler/issues/371)) ([55715d5](https://github.com/sern-handler/handler/commit/55715d565990fe686159f3c1eda3754d1262c72c))
|
||||
|
||||
## [4.0.3](https://github.com/sern-handler/handler/compare/v4.0.2...v4.0.3) (2024-10-06)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* async presence ([#369](https://github.com/sern-handler/handler/issues/369)) ([eabfb81](https://github.com/sern-handler/handler/commit/eabfb81819b53a4656d8eac6e21cfb488b724a42))
|
||||
* fix eventModule typing for Discord events ([#368](https://github.com/sern-handler/handler/issues/368)) ([1789ccb](https://github.com/sern-handler/handler/commit/1789ccb2f22f502f87538fecdb07106ff7110434))
|
||||
|
||||
## [4.0.2](https://github.com/sern-handler/handler/compare/v4.0.1...v4.0.2) (2024-08-13)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* type issue ([2106cdc](https://github.com/sern-handler/handler/commit/2106cdc1d033f88b6ee4ccca6754fe7a595a9328))
|
||||
|
||||
## [4.0.1](https://github.com/sern-handler/handler/compare/v4.0.0...v4.0.1) (2024-07-19)
|
||||
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ export default commandModule({
|
||||
- [Vinci](https://github.com/SrIzan10/vinci), the bot for Mara Turing.
|
||||
- [Bask](https://github.com/baskbotml/bask), Listen your favorite artists on Discord.
|
||||
- [Murayama](https://github.com/murayamabot/murayama), :pepega:
|
||||
- [Protector](https://github.com/GlitchApotamus/Protector), Just a simple bot to help enhance a private minecraft server.
|
||||
- [Protector (WIP)](https://github.com/needhamgary/Protector), Just a simple bot to help enhance a private minecraft server.
|
||||
- [SmokinWeed 💨](https://github.com/Peter-MJ-Parker/sern-bud), A fun bot for a small - but growing - server.
|
||||
- [Man Nomic](https://github.com/jacoobes/man-nomic), A simple information bot to provide information to the nomic-ai discord community.
|
||||
- [Linear-Discord](https://github.com/sern-handler/linear-discord) Display and manage a linear dashboard.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@sern/handler",
|
||||
"packageManager": "yarn@3.5.0",
|
||||
"version": "4.1.0",
|
||||
"version": "4.0.1",
|
||||
"description": "A complete, customizable, typesafe, & reactive framework for discord bots.",
|
||||
"main": "./dist/index.js",
|
||||
"module": "./dist/index.js",
|
||||
@@ -39,7 +39,8 @@
|
||||
"callsites": "^3.1.0",
|
||||
"cron": "^3.1.7",
|
||||
"deepmerge": "^4.3.1",
|
||||
"rxjs": "^7.8.0"
|
||||
"rxjs": "^7.8.0",
|
||||
"ts-results-es": "^4.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@faker-js/faker": "^8.0.1",
|
||||
|
||||
@@ -10,39 +10,8 @@ import { partitionPlugins } from './functions'
|
||||
import type { Awaitable } from '../types/utility';
|
||||
|
||||
/**
|
||||
* Creates a command module with standardized structure and plugin support.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param {InputCommand} mod - Command module configuration
|
||||
* @returns {Module} Processed command module ready for registration
|
||||
*
|
||||
* @example
|
||||
* // Basic slash command
|
||||
* export default commandModule({
|
||||
* type: CommandType.Slash,
|
||||
* description: "Ping command",
|
||||
* execute: async (ctx) => {
|
||||
* await ctx.reply("Pong! 🏓");
|
||||
* }
|
||||
* });
|
||||
*
|
||||
* @example
|
||||
* // Command with component interaction
|
||||
* export default commandModule({
|
||||
* type: CommandType.Slash,
|
||||
* description: "Interactive command",
|
||||
* execute: async (ctx) => {
|
||||
* const button = new ButtonBuilder({
|
||||
* customId: "btn/someData",
|
||||
* label: "Click me",
|
||||
* style: ButtonStyle.Primary
|
||||
* });
|
||||
* await ctx.reply({
|
||||
* content: "Interactive message",
|
||||
* components: [new ActionRowBuilder().addComponents(button)]
|
||||
* });
|
||||
* }
|
||||
* });
|
||||
* @since 1.0.0 The wrapper function to define command modules for sern
|
||||
* @param mod
|
||||
*/
|
||||
export function commandModule(mod: InputCommand): Module {
|
||||
const [onEvent, plugins] = partitionPlugins(mod.plugins);
|
||||
@@ -52,35 +21,12 @@ export function commandModule(mod: InputCommand): Module {
|
||||
locals: {} } as Module;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates an event module for handling Discord.js or custom events.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @template T - Event name from ClientEvents
|
||||
* @param {InputEvent<T>} mod - Event module configuration
|
||||
* @returns {Module} Processed event module ready for registration
|
||||
* @throws {Error} If ControlPlugins are used in event modules
|
||||
*
|
||||
* @example
|
||||
* // Discord event listener
|
||||
* export default eventModule({
|
||||
* type: EventType.Discord,
|
||||
* execute: async (message) => {
|
||||
* console.log(`${message.author.tag}: ${message.content}`);
|
||||
* }
|
||||
* });
|
||||
*
|
||||
* @example
|
||||
* // Custom sern event
|
||||
* export default eventModule({
|
||||
* type: EventType.Sern,
|
||||
* execute: async (eventData) => {
|
||||
* // Handle sern-specific event
|
||||
* }
|
||||
* });
|
||||
* The wrapper function to define event modules for sern
|
||||
* @param mod
|
||||
*/
|
||||
export function eventModule<T extends keyof ClientEvents = keyof ClientEvents>(mod: InputEvent<T>): Module {
|
||||
export function eventModule(mod: InputEvent): Module {
|
||||
const [onEvent, plugins] = partitionPlugins(mod.plugins);
|
||||
if(onEvent.length !== 0) throw Error("Event modules cannot have ControlPlugins");
|
||||
return { ...mod,
|
||||
@@ -89,9 +35,8 @@ export function eventModule<T extends keyof ClientEvents = keyof ClientEvents>(m
|
||||
}
|
||||
|
||||
/** Create event modules from discord.js client events,
|
||||
* This was an {@link eventModule} for discord events,
|
||||
* where typings were bad.
|
||||
* @deprecated Use {@link eventModule} instead
|
||||
* This is an {@link eventModule} for discord events,
|
||||
* where typings can be very bad.
|
||||
* @param mod
|
||||
*/
|
||||
export function discordEvent<T extends keyof ClientEvents>(mod: {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { CommandType, PluginType } from './structures/enums';
|
||||
import type { Plugin, PluginResult, CommandArgs, InitArgs } from '../types/core-plugin';
|
||||
import { Err, Ok } from './structures/result';
|
||||
import type { Dictionary } from '../types/utility';
|
||||
import { Err, Ok } from 'ts-results-es';
|
||||
|
||||
export function makePlugin<V extends unknown[]>(
|
||||
type: PluginType,
|
||||
@@ -38,7 +37,7 @@ export function CommandControlPlugin<I extends CommandType>(
|
||||
* The object passed into every plugin to control a command's behavior
|
||||
*/
|
||||
export const controller = {
|
||||
next: (val?: Dictionary) => Ok(val),
|
||||
next: (val?: Record<string,unknown>) => Ok(val),
|
||||
stop: (val?: string) => Err(val),
|
||||
};
|
||||
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import type { ActivitiesOptions } from "discord.js";
|
||||
import type { IntoDependencies } from "./ioc";
|
||||
import type { Emitter } from "./interfaces";
|
||||
import { Awaitable } from "../types/utility";
|
||||
|
||||
type Status = 'online' | 'idle' | 'invisible' | 'dnd'
|
||||
type PresenceReduce = (previous: Presence.Result) => Awaitable<Presence.Result>;
|
||||
type PresenceReduce = (previous: Presence.Result) => Presence.Result;
|
||||
|
||||
|
||||
|
||||
export const Presence = {
|
||||
/**
|
||||
@@ -49,7 +50,7 @@ export const Presence = {
|
||||
export declare namespace Presence {
|
||||
export type Config<T extends (keyof Dependencies)[]> = {
|
||||
inject?: [...T]
|
||||
execute: (...v: IntoDependencies<T>) => Awaitable<Presence.Result>;
|
||||
execute: (...v: IntoDependencies<T>) => Presence.Result;
|
||||
|
||||
}
|
||||
|
||||
@@ -59,7 +60,7 @@ export declare namespace Presence {
|
||||
activities?: ActivitiesOptions[];
|
||||
shardId?: number[];
|
||||
repeat?: number | [Emitter, string];
|
||||
onRepeat?: PresenceReduce
|
||||
onRepeat?: (previous: Result) => Result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,8 @@ import type {
|
||||
Snowflake,
|
||||
User,
|
||||
} from 'discord.js';
|
||||
import { Result, Ok, Err, val } from './result';
|
||||
import { CoreContext } from '../structures/core-context';
|
||||
import { Result, Ok, Err } from 'ts-results-es';
|
||||
import * as assert from 'assert';
|
||||
import type { ReplyOptions } from '../../types/utility';
|
||||
import { fmt } from '../functions'
|
||||
@@ -20,32 +21,39 @@ import { SernError } from './enums';
|
||||
* Provides values shared between
|
||||
* Message and ChatInputCommandInteraction
|
||||
*/
|
||||
export class Context {
|
||||
export class Context extends CoreContext<Message, ChatInputCommandInteraction> {
|
||||
|
||||
get options() {
|
||||
if(this.isMessage()) {
|
||||
const [, ...rest] = fmt(this.message.content, this.prefix);
|
||||
return rest;
|
||||
}
|
||||
return this.interaction.options;
|
||||
} else {
|
||||
return this.interaction.options;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected constructor(protected ctx: Result<Message, ChatInputCommandInteraction>,
|
||||
private __prefix?: string) { }
|
||||
private __prefix?: string) {
|
||||
super(ctx);
|
||||
}
|
||||
public get prefix() {
|
||||
return this.__prefix;
|
||||
}
|
||||
public get id(): Snowflake {
|
||||
return val(this.ctx).id
|
||||
return safeUnwrap(this.ctx
|
||||
.map(m => m.id)
|
||||
.mapErr(i => i.id));
|
||||
}
|
||||
|
||||
public get channel() {
|
||||
return val(this.ctx).channel;
|
||||
return safeUnwrap(this.ctx.map(m => m.channel).mapErr(i => i.channel));
|
||||
}
|
||||
|
||||
public get channelId(): Snowflake {
|
||||
return val(this.ctx).channelId;
|
||||
return safeUnwrap(this.ctx
|
||||
.map(m => m.channelId)
|
||||
.mapErr(i => i.channelId));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,11 +61,9 @@ export class Context {
|
||||
* else, interaction.user
|
||||
*/
|
||||
public get user(): User {
|
||||
if(this.ctx.ok) {
|
||||
return this.ctx.value.author;
|
||||
}
|
||||
return this.ctx.error.user;
|
||||
|
||||
return safeUnwrap(this.ctx
|
||||
.map(m => m.author)
|
||||
.mapErr(i => i.user));
|
||||
}
|
||||
|
||||
public get userId(): Snowflake {
|
||||
@@ -65,60 +71,59 @@ export class Context {
|
||||
}
|
||||
|
||||
public get createdTimestamp(): number {
|
||||
return val(this.ctx).createdTimestamp;
|
||||
return safeUnwrap(this.ctx
|
||||
.map(m => m.createdTimestamp)
|
||||
.mapErr(i => i.createdTimestamp));
|
||||
}
|
||||
|
||||
public get guild() {
|
||||
return val(this.ctx).guild;
|
||||
return safeUnwrap(this.ctx
|
||||
.map(m => m.guild)
|
||||
.mapErr(i => i.guild));
|
||||
}
|
||||
|
||||
public get guildId() {
|
||||
return val(this.ctx).guildId;
|
||||
return safeUnwrap(this.ctx
|
||||
.map(m => m.guildId)
|
||||
.mapErr(i => i.guildId));
|
||||
}
|
||||
/*
|
||||
* interactions can return APIGuildMember if the guild it is emitted from is not cached
|
||||
*/
|
||||
public get member() {
|
||||
return val(this.ctx).member;
|
||||
return safeUnwrap(this.ctx
|
||||
.map(m => m.member)
|
||||
.mapErr(i => i.member));
|
||||
}
|
||||
|
||||
get message(): Message {
|
||||
if(this.ctx.ok) {
|
||||
return this.ctx.value;
|
||||
}
|
||||
throw Error(SernError.MismatchEvent);
|
||||
}
|
||||
public isMessage(): this is Context & { ctx: Result<Message, never> } {
|
||||
return this.ctx.ok;
|
||||
}
|
||||
|
||||
public isSlash(): this is Context & { ctx: Result<never, ChatInputCommandInteraction> } {
|
||||
return !this.isMessage();
|
||||
return this.ctx.expect(SernError.MismatchEvent);
|
||||
}
|
||||
|
||||
get interaction(): ChatInputCommandInteraction {
|
||||
if(!this.ctx.ok) {
|
||||
return this.ctx.error;
|
||||
}
|
||||
throw Error(SernError.MismatchEvent);
|
||||
return this.ctx.expectErr(SernError.MismatchEvent);
|
||||
}
|
||||
|
||||
|
||||
public get client(): Client {
|
||||
return val(this.ctx).client;
|
||||
return safeUnwrap(this.ctx
|
||||
.map(m => m.client)
|
||||
.mapErr(i => i.client));
|
||||
}
|
||||
|
||||
public get inGuild(): boolean {
|
||||
return val(this.ctx).inGuild()
|
||||
return safeUnwrap(this.ctx
|
||||
.map(m => m.inGuild())
|
||||
.mapErr(i => i.inGuild()));
|
||||
}
|
||||
|
||||
public async reply(content: ReplyOptions) {
|
||||
if(this.ctx.ok) {
|
||||
return this.ctx.value.reply(content as MessageReplyOptions)
|
||||
}
|
||||
interface FetchReply { fetchReply: true };
|
||||
return this.ctx.error.reply(content as InteractionReplyOptions & FetchReply)
|
||||
|
||||
return safeUnwrap(
|
||||
this.ctx
|
||||
.map(m => m.reply(content as MessageReplyOptions))
|
||||
.mapErr(i =>
|
||||
i.reply(content as InteractionReplyOptions).then(() => i.fetchReply())),
|
||||
);
|
||||
}
|
||||
|
||||
static wrap(wrappable: BaseInteraction | Message, prefix?: string): Context {
|
||||
@@ -129,3 +134,10 @@ export class Context {
|
||||
return new Context(Err(wrappable), prefix);
|
||||
}
|
||||
}
|
||||
|
||||
function safeUnwrap<T>(res: Result<T, T>) {
|
||||
if(res.isOk()) {
|
||||
return res.expect("Tried unwrapping message field: " + res)
|
||||
}
|
||||
return res.expectErr("Tried unwrapping interaction field" + res)
|
||||
}
|
||||
|
||||
18
src/core/structures/core-context.ts
Normal file
18
src/core/structures/core-context.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Result as Either } from 'ts-results-es';
|
||||
import * as assert from 'node:assert';
|
||||
|
||||
/**
|
||||
* @since 3.0.0
|
||||
*/
|
||||
export abstract class CoreContext<M, I> {
|
||||
protected constructor(protected ctx: Either<M, I>) {
|
||||
assert.ok(typeof ctx === 'object' && ctx != null, "Context was nonobject or null");
|
||||
}
|
||||
public isMessage(): this is CoreContext<M, never> {
|
||||
return this.ctx.isOk();
|
||||
}
|
||||
|
||||
public isSlash(): this is CoreContext<never, I> {
|
||||
return !this.isMessage();
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
export type Result<Ok, Err> =
|
||||
| { ok: true; value: Ok }
|
||||
| { ok: false; error: Err };
|
||||
|
||||
export const Ok = <Ok>(value: Ok) => ({ ok: true, value } as const);
|
||||
export const Err = <Err>(error: Err) => ({ ok: false, error } as const);
|
||||
|
||||
export const val = <O, E>(r: Result<O, E>) => r.ok ? r.value : r.error;
|
||||
export const EMPTY_ERR = Err(undefined);
|
||||
|
||||
/**
|
||||
* Wrap an async operation that may throw an Error (`try-catch` style) into checked exception style
|
||||
* @param op The operation function
|
||||
*/
|
||||
export async function wrapAsync<T, E = unknown>(op: () => Promise<T>): Promise<Result<T, E>> {
|
||||
try { return op()
|
||||
.then(Ok)
|
||||
.catch(Err); }
|
||||
catch (e) { return Promise.resolve(Err(e as E)); }
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
import * as Id from '../core/id'
|
||||
import type { Emitter, ErrorHandling, Logging } from '../core/interfaces';
|
||||
import { SernError } from '../core/structures/enums'
|
||||
import { EMPTY_ERR, Err, Ok, Result, wrapAsync } from '../core/structures/result';
|
||||
import { Err, Ok, Result } from 'ts-results-es';
|
||||
import type { UnpackedDependencies } from '../types/utility';
|
||||
import type { CommandModule, Module, Processed } from '../types/core-modules';
|
||||
import * as assert from 'node:assert';
|
||||
@@ -17,7 +17,6 @@ import { CommandType } from '../core/structures/enums'
|
||||
import { inspect } from 'node:util'
|
||||
import { disposeAll } from '../core/ioc';
|
||||
import { resultPayload, isAutocomplete, treeSearch, fmt } from '../core/functions'
|
||||
|
||||
import merge from 'deepmerge'
|
||||
|
||||
function handleError<C>(crashHandler: ErrorHandling, emitter: Emitter, logging?: Logging) {
|
||||
@@ -44,7 +43,7 @@ interface ExecutePayload {
|
||||
|
||||
export const filterTap = <K, R>(onErr: (e: R) => void): OperatorFunction<Result<K, R>, K> =>
|
||||
concatMap(result => {
|
||||
if(result.ok){
|
||||
if(result.isOk()) {
|
||||
return of(result.value)
|
||||
}
|
||||
onErr(result.error);
|
||||
@@ -143,7 +142,7 @@ export function createInteractionHandler<T extends Interaction>(
|
||||
.map(({ id, params }) => ({ module: mg.get(id), params }))
|
||||
.filter(({ module }) => module !== undefined);
|
||||
if(modules.length == 0) {
|
||||
return EMPTY_ERR;
|
||||
return Err.EMPTY;
|
||||
}
|
||||
const [{module, params}] = modules;
|
||||
return Ok(createDispatcher({
|
||||
@@ -180,9 +179,9 @@ export function createMessageHandler(
|
||||
* @param task the deferred execution which will be called
|
||||
*/
|
||||
export function executeModule(emitter: Emitter, { module, args }: ExecutePayload) {
|
||||
return from(wrapAsync(async () => module.execute(...args)))
|
||||
return from(Result.wrapAsync(async () => module.execute(...args)))
|
||||
.pipe(concatMap(result => {
|
||||
if (result.ok){
|
||||
if (result.isOk()) {
|
||||
emitter.emit('module.activate', resultPayload('success', module));
|
||||
return EMPTY;
|
||||
}
|
||||
@@ -207,10 +206,10 @@ export function createResultResolver<Output>(config: {
|
||||
return async (payload: ExecutePayload) => {
|
||||
const task = await callPlugins(payload);
|
||||
if (!task) throw Error("Plugin did not return anything.");
|
||||
if(!task.ok) {
|
||||
onStop?.(payload.module, String(task.error));
|
||||
} else {
|
||||
if(task.isOk()) {
|
||||
return onNext(payload, task.value) as Output;
|
||||
} else {
|
||||
onStop?.(payload.module, String(task.error));
|
||||
}
|
||||
};
|
||||
};
|
||||
@@ -226,13 +225,12 @@ export async function callInitPlugins(_module: Module, deps: Dependencies, emit?
|
||||
for(const plugin of module.plugins ?? []) {
|
||||
const result = await plugin.execute({ module, absPath: module.meta.absPath, deps });
|
||||
if (!result) throw Error("Plugin did not return anything. " + inspect(plugin, false, Infinity, true));
|
||||
if(!result.ok) {
|
||||
if(result.isErr()) {
|
||||
if(emit) {
|
||||
emitter?.emit('module.register',
|
||||
resultPayload('failure', module, result.error ?? SernError.PluginFailure));
|
||||
}
|
||||
throw Error((result.error ?? SernError.PluginFailure) +
|
||||
'on module ' + module.name + " " + module.meta.absPath);
|
||||
throw Error(result.error ?? SernError.PluginFailure);
|
||||
}
|
||||
}
|
||||
return module
|
||||
@@ -241,18 +239,8 @@ export async function callInitPlugins(_module: Module, deps: Dependencies, emit?
|
||||
export async function callPlugins({ args, module, deps, params }: ExecutePayload) {
|
||||
let state = {};
|
||||
for(const plugin of module.onEvent??[]) {
|
||||
const executionContext = {
|
||||
state,
|
||||
deps,
|
||||
params,
|
||||
type: module.type,
|
||||
module: { name: module.name,
|
||||
description: module.description,
|
||||
locals: module.locals,
|
||||
meta: module.meta }
|
||||
};
|
||||
const result = await plugin.execute(...args, executionContext);
|
||||
if(!result.ok) {
|
||||
const result = await plugin.execute(...args, { state, deps, params, type: module.type });
|
||||
if(result.isErr()) {
|
||||
return result;
|
||||
}
|
||||
if(isObject(result.value)) {
|
||||
@@ -263,26 +251,14 @@ export async function callPlugins({ args, module, deps, params }: ExecutePayload
|
||||
}
|
||||
/**
|
||||
* Creates an executable task ( execute the command ) if all control plugins are successful
|
||||
* this needs to go
|
||||
* @param onStop emits a failure response to the SernEmitter
|
||||
*/
|
||||
export function intoTask(onStop: (m: Module) => unknown) {
|
||||
const onNext = ({ args, module, deps, params }: ExecutePayload, state: Record<string, unknown>) => {
|
||||
|
||||
return {
|
||||
module,
|
||||
args: [...args, { state,
|
||||
deps,
|
||||
params,
|
||||
type: module.type,
|
||||
module: { name: module.name,
|
||||
description: module.description,
|
||||
locals: module.locals,
|
||||
meta: module.meta } }],
|
||||
deps
|
||||
}
|
||||
|
||||
};
|
||||
const onNext = ({ args, module, deps, params }: ExecutePayload, state: Record<string, unknown>) => ({
|
||||
module,
|
||||
args: [...args, { state, deps, params, type: module.type }],
|
||||
deps
|
||||
});
|
||||
return createResultResolver({ onStop, onNext });
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { concatMap, from, interval, of, map, startWith, fromEvent, take, mergeScan } from "rxjs"
|
||||
import { concatMap, from, interval, of, map, scan, startWith, fromEvent, take } from "rxjs"
|
||||
import { Presence } from "../core/presences";
|
||||
import { Services } from "../core/ioc";
|
||||
import assert from "node:assert";
|
||||
@@ -14,7 +14,7 @@ const parseConfig = async (conf: Promise<Presence.Result>) => {
|
||||
const src$ = typeof repeat === 'number'
|
||||
? interval(repeat)
|
||||
: fromEvent(...repeat);
|
||||
return src$.pipe(mergeScan(async (args) => onRepeat(args), s),
|
||||
return src$.pipe(scan(onRepeat, s),
|
||||
startWith(s));
|
||||
}
|
||||
return of(s).pipe(take(1));
|
||||
|
||||
@@ -19,97 +19,14 @@ import type {
|
||||
import type { CommandType, EventType } from '../core/structures/enums';
|
||||
import { Context } from '../core/structures/context'
|
||||
import { ControlPlugin, InitPlugin, Plugin } from './core-plugin';
|
||||
import { Awaitable, SernEventsMapping, UnpackedDependencies, Dictionary } from './utility';
|
||||
import { Awaitable, SernEventsMapping, UnpackedDependencies } from './utility';
|
||||
|
||||
/**
|
||||
* SDT (State, Dependencies, Type) interface represents the core data structure
|
||||
* passed through the plugin pipeline to command modules.
|
||||
*
|
||||
* @interface SDT
|
||||
* @template TState - Type parameter for the state object's structure
|
||||
* @template TDeps - Type parameter for dependencies interface
|
||||
*
|
||||
* @property {Record<string, unknown>} state - Accumulated state data passed between plugins
|
||||
* @property {TDeps} deps - Instance of application dependencies
|
||||
* @property {CommandType} type - Command type identifier
|
||||
* @property {string} [params] - Optional parameters passed to the command
|
||||
*
|
||||
* @example
|
||||
* // Example of a plugin using SDT
|
||||
* const loggingPlugin = CommandControlPlugin((ctx, sdt: SDT) => {
|
||||
* console.log(`User ${ctx.user.id} executed command`);
|
||||
* return controller.next({ 'logging/timestamp': Date.now() });
|
||||
* });
|
||||
*
|
||||
* @example
|
||||
* // Example of state accumulation through multiple plugins
|
||||
* const plugin1 = CommandControlPlugin((ctx, sdt: SDT) => {
|
||||
* return controller.next({ 'plugin1/data': 'value1' });
|
||||
* });
|
||||
*
|
||||
* const plugin2 = CommandControlPlugin((ctx, sdt: SDT) => {
|
||||
* // Access previous state
|
||||
* const prevData = sdt.state['plugin1/data'];
|
||||
* return controller.next({ 'plugin2/data': 'value2' });
|
||||
* });
|
||||
*
|
||||
* @remarks
|
||||
* - State is immutable and accumulated through the plugin chain
|
||||
* - Keys in state should be namespaced to avoid collisions
|
||||
* - Dependencies are injected and available throughout the pipeline
|
||||
* - Type information helps plugins make type-safe decisions
|
||||
*
|
||||
* @see {@link CommandControlPlugin} for plugin implementation
|
||||
* @see {@link CommandType} for available command types
|
||||
* @see {@link Dependencies} for dependency injection interface
|
||||
*/
|
||||
//state, deps, type (very original)
|
||||
export type SDT = {
|
||||
/**
|
||||
* Accumulated state passed between plugins in the pipeline.
|
||||
* Each plugin can add to or modify this state using controller.next().
|
||||
*
|
||||
* @type {Record<string, unknown>}
|
||||
* @example
|
||||
* // Good: Namespaced state key
|
||||
* { 'myPlugin/userData': { id: '123', name: 'User' } }
|
||||
*
|
||||
* // Avoid: Non-namespaced keys that might collide
|
||||
* { userData: { id: '123' } }
|
||||
*/
|
||||
state: Record<string, unknown>;
|
||||
|
||||
/**
|
||||
* Application dependencies available to plugins and command modules.
|
||||
* Typically includes services, configurations, and utilities.
|
||||
*
|
||||
* @type {Dependencies}
|
||||
*/
|
||||
state: Record<string,unknown>;
|
||||
deps: Dependencies;
|
||||
|
||||
/**
|
||||
* Identifies the type of command being processed.
|
||||
* Used by plugins to apply type-specific logic.
|
||||
*
|
||||
* @type {CommandType}
|
||||
*/
|
||||
type: CommandType;
|
||||
|
||||
/**
|
||||
* Optional parameters passed to the command.
|
||||
* May contain additional configuration or runtime data.
|
||||
*
|
||||
* @type {string}
|
||||
* @optional
|
||||
*/
|
||||
params?: string;
|
||||
|
||||
/**
|
||||
* A copy of the current module that the plugin is running in.
|
||||
*/
|
||||
module: { name: string;
|
||||
description: string;
|
||||
meta: Dictionary;
|
||||
locals: Dictionary; }
|
||||
type: CommandType,
|
||||
params?: string
|
||||
};
|
||||
|
||||
export type Processed<T> = T & { name: string; description: string };
|
||||
@@ -124,75 +41,7 @@ export interface Module {
|
||||
id: string;
|
||||
absPath: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom data storage object for module-specific information.
|
||||
* Plugins and module code can use this to store and retrieve metadata,
|
||||
* configuration, or any other module-specific information.
|
||||
*
|
||||
* @type {Dictionary}
|
||||
* @description A key-value store that allows plugins and module code to persist
|
||||
* data at the module level. This is especially useful for InitPlugins that need
|
||||
* to attach metadata or configuration to modules.
|
||||
*
|
||||
* @example
|
||||
* // In a plugin
|
||||
* module.locals.registrationDate = Date.now();
|
||||
* module.locals.version = "1.0.0";
|
||||
* module.locals.permissions = ["ADMIN", "MODERATE"];
|
||||
*
|
||||
* @example
|
||||
* // In module execution
|
||||
* console.log(`Command registered on: ${new Date(module.locals.registrationDate)}`);
|
||||
*
|
||||
* @example
|
||||
* // Storing localization data
|
||||
* module.locals.translations = {
|
||||
* en: "Hello",
|
||||
* es: "Hola",
|
||||
* fr: "Bonjour"
|
||||
* };
|
||||
*
|
||||
* @example
|
||||
* // Storing command metadata
|
||||
* module.locals.metadata = {
|
||||
* category: "admin",
|
||||
* cooldown: 5000,
|
||||
* requiresPermissions: true
|
||||
* };
|
||||
*
|
||||
* @remarks
|
||||
* - The locals object is initialized as an empty object ({}) by default
|
||||
* - Keys should be namespaced to avoid collisions between plugins
|
||||
* - Values can be of any type
|
||||
* - Data persists for the lifetime of the module
|
||||
* - Commonly used by InitPlugins during module initialization
|
||||
*
|
||||
* @best-practices
|
||||
* 1. Namespace your keys to avoid conflicts:
|
||||
* ```typescript
|
||||
* module.locals['myPlugin:data'] = value;
|
||||
* ```
|
||||
*
|
||||
* 2. Document the data structure you're storing:
|
||||
* ```typescript
|
||||
* interface MyPluginData {
|
||||
* version: string;
|
||||
* timestamp: number;
|
||||
* }
|
||||
* module.locals['myPlugin:data'] = {
|
||||
* version: '1.0.0',
|
||||
* timestamp: Date.now()
|
||||
* } as MyPluginData;
|
||||
* ```
|
||||
*
|
||||
* 3. Use type-safe accessors when possible:
|
||||
* ```typescript
|
||||
* const getPluginData = (module: Module): MyPluginData =>
|
||||
* module.locals['myPlugin:data'];
|
||||
* ```
|
||||
*/
|
||||
locals: Dictionary;
|
||||
locals: Record<string,unknown>
|
||||
execute(...args: any[]): Awaitable<any>;
|
||||
}
|
||||
|
||||
@@ -318,9 +167,9 @@ export interface CommandModuleDefs {
|
||||
[CommandType.Modal]: ModalSubmitCommand;
|
||||
}
|
||||
|
||||
export interface EventModuleDefs<T extends keyof ClientEvents = keyof ClientEvents> {
|
||||
export interface EventModuleDefs {
|
||||
[EventType.Sern]: SernEventCommand;
|
||||
[EventType.Discord]: DiscordEventCommand<T>;
|
||||
[EventType.Discord]: DiscordEventCommand;
|
||||
[EventType.External]: ExternalEventCommand;
|
||||
}
|
||||
|
||||
@@ -337,12 +186,12 @@ export interface SernAutocompleteData
|
||||
type CommandModuleNoPlugins = {
|
||||
[T in CommandType]: Omit<CommandModuleDefs[T], 'plugins' | 'onEvent' | 'meta' | 'locals'>;
|
||||
};
|
||||
type EventModulesNoPlugins<K extends keyof ClientEvents = keyof ClientEvents> = {
|
||||
[T in EventType]: Omit<EventModuleDefs<K>[T], 'plugins' | 'onEvent' | 'meta' | 'locals'> ;
|
||||
type EventModulesNoPlugins = {
|
||||
[T in EventType]: Omit<EventModuleDefs[T], 'plugins' | 'onEvent' | 'meta' | 'locals'> ;
|
||||
};
|
||||
|
||||
export type InputEvent<K extends keyof ClientEvents = keyof ClientEvents> = {
|
||||
[T in EventType]: EventModulesNoPlugins<K>[T] & {
|
||||
export type InputEvent = {
|
||||
[T in EventType]: EventModulesNoPlugins[T] & {
|
||||
once?: boolean;
|
||||
plugins?: InitPlugin[]
|
||||
};
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
* Plugins are reminiscent of middleware in express.
|
||||
*/
|
||||
|
||||
import type { Result } from 'ts-results-es';
|
||||
import type {
|
||||
Module,
|
||||
Processed,
|
||||
@@ -22,7 +23,6 @@ import type { Context } from '../core/structures/context'
|
||||
import type {
|
||||
ButtonInteraction,
|
||||
ChannelSelectMenuInteraction,
|
||||
ChatInputCommandInteraction,
|
||||
MentionableSelectMenuInteraction,
|
||||
MessageContextMenuCommandInteraction,
|
||||
ModalSubmitInteraction,
|
||||
@@ -31,7 +31,6 @@ import type {
|
||||
UserContextMenuCommandInteraction,
|
||||
UserSelectMenuInteraction,
|
||||
} from 'discord.js';
|
||||
import { Result } from '../core/structures/result';
|
||||
|
||||
export type PluginResult = Awaitable<Result<Record<string,unknown>|undefined, string|undefined>>;
|
||||
export interface InitArgs<T extends Processed<Module> = Processed<Module>> {
|
||||
@@ -57,8 +56,8 @@ export type AnyPlugin = ControlPlugin | InitPlugin<[InitArgs<Processed<Module>>]
|
||||
export type CommandArgs<I extends CommandType = CommandType> = CommandArgsMatrix[I]
|
||||
|
||||
interface CommandArgsMatrix {
|
||||
[CommandType.Text]: [Context & { get options(): string[]}, SDT];
|
||||
[CommandType.Slash]: [Context & { get options(): ChatInputCommandInteraction['options']}, SDT];
|
||||
[CommandType.Text]: [Context, SDT];
|
||||
[CommandType.Slash]: [Context, SDT];
|
||||
[CommandType.Both]: [Context, SDT];
|
||||
[CommandType.CtxMsg]: [MessageContextMenuCommandInteraction, SDT];
|
||||
[CommandType.CtxUser]: [UserContextMenuCommandInteraction, SDT];
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import type { InteractionReplyOptions, MessageReplyOptions } from 'discord.js';
|
||||
import type { Module } from './core-modules';
|
||||
import type { Result } from '../core/structures/result';
|
||||
import type { Result } from 'ts-results-es';
|
||||
|
||||
export type Awaitable<T> = PromiseLike<T> | T;
|
||||
export type Dictionary = Record<string, unknown>
|
||||
|
||||
export type VoidResult = Result<void, void>;
|
||||
export type AnyFunction = (...args: any[]) => unknown;
|
||||
|
||||
@@ -120,6 +120,13 @@ test('init plugins replace array', async () => {
|
||||
expect(['a']).deep.equal(s.opts)
|
||||
})
|
||||
|
||||
test('call control plugin ', async () => {
|
||||
const plugin = CommandControlPlugin<CommandType.Slash>((ctx,sdt) => {
|
||||
return controller.next();
|
||||
});
|
||||
const res = await plugin.execute(new ChatInputCommandInteraction(), {})
|
||||
expect(res.isOk()).toBe(true)
|
||||
})
|
||||
|
||||
test('form sdt', async () => {
|
||||
|
||||
|
||||
@@ -557,6 +557,7 @@ __metadata:
|
||||
discord.js: ^14.15.3
|
||||
eslint: 8.39.0
|
||||
rxjs: ^7.8.0
|
||||
ts-results-es: ^4.1.0
|
||||
typescript: 5.0.2
|
||||
vitest: ^1.6.0
|
||||
languageName: unknown
|
||||
@@ -2958,6 +2959,13 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"ts-results-es@npm:^4.1.0":
|
||||
version: 4.2.0
|
||||
resolution: "ts-results-es@npm:4.2.0"
|
||||
checksum: ff475c2f6d44377e0204211e6eafdbcabddf3ad09d40540ad5dee3d817eefbd48c07a21f5ad86864ef82cd8a5542a266af9dd8dd4d58d4766fdd6e79370519bb
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"tslib@npm:2.6.2":
|
||||
version: 2.6.2
|
||||
resolution: "tslib@npm:2.6.2"
|
||||
|
||||
Reference in New Issue
Block a user