mirror of
https://github.com/sern-handler/handler
synced 2026-06-15 04:12:17 +00:00
feat: adding refactoring for repetitive event plugin processing
This commit is contained in:
@@ -12,7 +12,7 @@ import { match } from 'ts-pattern';
|
||||
import { SernError } from '../structures/errors';
|
||||
import Context from '../structures/context';
|
||||
import { controller } from '../sern';
|
||||
import type { AutocompleteCommand, Module, SlashCommand } from '../structures/module';
|
||||
import type { Module } from '../structures/module';
|
||||
import {
|
||||
isButton,
|
||||
isChatInputCommand,
|
||||
|
||||
@@ -21,7 +21,7 @@ export const onMessageCreate = (wrapper: Wrapper) => {
|
||||
map(message => {
|
||||
const [prefix, ...rest] = fmt(message, defaultPrefix);
|
||||
return {
|
||||
ctx: Context.wrap(message), //TODO : check for BothCommand
|
||||
ctx: Context.wrap(message),
|
||||
args: <Args>['text', rest],
|
||||
mod:
|
||||
Files.TextCommands.text.get(prefix) ??
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
import type { Message } from 'discord.js';
|
||||
import type { Awaitable, Message } from 'discord.js';
|
||||
import { Observable, throwError } from 'rxjs';
|
||||
import { SernError } from '../structures/errors';
|
||||
import type { InteractionDefs, Module, ModuleDefs } from '../structures/module';
|
||||
import { correctModuleType } from '../utilities/predicates';
|
||||
import type { CommandType } from '../structures/enums';
|
||||
import type { UnionToIntersection } from '../../types/handler';
|
||||
import { controller } from '../sern';
|
||||
import type { Result } from 'ts-results';
|
||||
import type { SelectMenuInteraction } from 'discord.js';
|
||||
|
||||
export function filterCorrectModule<T extends keyof ModuleDefs>(cmdType: T) {
|
||||
return (src: Observable<Module | undefined>) =>
|
||||
@@ -45,13 +49,20 @@ export function ignoreNonBot(prefix: string) {
|
||||
});
|
||||
}
|
||||
|
||||
export function processOnEvents<T extends CommandType>(interaction: InteractionDefs[T]) {
|
||||
return (src: Observable<ModuleDefs[CommandType]>) =>
|
||||
new Observable<Message>(subscriber => {
|
||||
return src.subscribe({
|
||||
next(m) {},
|
||||
error: e => subscriber.error(e),
|
||||
complete: () => subscriber.complete(),
|
||||
});
|
||||
});
|
||||
}
|
||||
// export function processOnEvents<T extends CommandType>(ty: T, interaction: InteractionDefs[T]) {
|
||||
// return (src: Observable<ModuleDefs[T]>) =>
|
||||
// new Observable<Awaitable<Result<void, void>>>(subscriber => {
|
||||
// return src.subscribe({
|
||||
// next(m) {
|
||||
// subscriber.next(m.onEvent?.map(e => {
|
||||
// return (<UnionToIntersection<typeof e>>e).execute(
|
||||
// [interaction as SelectMenuInteraction], //This is just to satisfy compiler
|
||||
// controller,
|
||||
// );
|
||||
// })) ;
|
||||
// },
|
||||
// error: e => subscriber.error(e),
|
||||
// complete: () => subscriber.complete(),
|
||||
// });
|
||||
// });
|
||||
// }
|
||||
|
||||
@@ -45,16 +45,29 @@ export type CommandPlugin = Override<
|
||||
|
||||
//TODO: rn adding the modType check a little hackish. Find better way to determine the
|
||||
// module type of the event plugin
|
||||
export type EventPlugin<T extends keyof ModuleDefs> = Override<
|
||||
BasePlugin,
|
||||
{
|
||||
type: PluginType.Event;
|
||||
execute: (
|
||||
event: Parameters<ModuleDefs[T]['execute']>,
|
||||
controller: Controller,
|
||||
) => Awaitable<Result<void, void>>;
|
||||
}
|
||||
>;
|
||||
// export type EventPlugin<T extends keyof ModuleDefs> = Override<
|
||||
// BasePlugin,
|
||||
// {
|
||||
// type: PluginType.Event;
|
||||
// execute: (
|
||||
// event: Parameters<ModuleDefs[T]['execute']>,
|
||||
// controller: Controller,
|
||||
// ) => Awaitable<Result<void, void>>;
|
||||
// }
|
||||
// >;
|
||||
|
||||
export type EventPlugin<T extends keyof ModuleDefs = keyof ModuleDefs> = {
|
||||
[K in T]: Override<
|
||||
BasePlugin,
|
||||
{
|
||||
type: PluginType.Event;
|
||||
execute: (
|
||||
event: Parameters<ModuleDefs[K]['execute']>,
|
||||
controller: Controller,
|
||||
) => Awaitable<Result<void, void>>;
|
||||
}
|
||||
>;
|
||||
}[T];
|
||||
|
||||
export function plugins(...plug: CommandPlugin[]): CommandPlugin[];
|
||||
export function plugins<T extends keyof ModuleDefs>(...plug: EventPlugin<T>[]): EventPlugin<T>[];
|
||||
|
||||
@@ -21,7 +21,7 @@ import type Context from './context';
|
||||
import { CommandType, PluginType } from './enums';
|
||||
import type { AutocompleteInteraction } from 'discord.js';
|
||||
import type { ApplicationCommandOptionType } from 'discord.js';
|
||||
import { ChatInputCommandInteraction, Message } from 'discord.js';
|
||||
import { ChatInputCommandInteraction, Message, User } from 'discord.js';
|
||||
|
||||
export interface BaseModule {
|
||||
type: CommandType | PluginType;
|
||||
@@ -119,8 +119,8 @@ export type ModalSubmitCommand = Override<
|
||||
export type AutocompleteCommand = Override<
|
||||
BaseModule,
|
||||
{
|
||||
name: string;
|
||||
type: CommandType.Autocomplete;
|
||||
name: string;
|
||||
onEvent?: EventPlugin<CommandType.Autocomplete>[];
|
||||
execute: (ctx: AutocompleteInteraction) => Awaitable<void>;
|
||||
}
|
||||
@@ -150,7 +150,6 @@ export type ModuleDefs = {
|
||||
[CommandType.Modal]: ModalSubmitCommand;
|
||||
[CommandType.Autocomplete]: AutocompleteCommand;
|
||||
};
|
||||
|
||||
export type InteractionDefs = {
|
||||
[CommandType.Text]: Context;
|
||||
[CommandType.Slash]: Context;
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import type { UnionToIntersection } from './types/handler';
|
||||
import type { Module } from './handler/structures/module';
|
||||
|
||||
export * as Sern from './handler/sern';
|
||||
export * from './types/handler';
|
||||
export * from './handler/structures/structxports';
|
||||
export * from './handler/plugins/plugin';
|
||||
let p: UnionToIntersection<Module>;
|
||||
|
||||
@@ -23,10 +23,15 @@ export type SlashOptions = Omit<CommandInteractionOptionResolver, 'getMessage' |
|
||||
export type Override<T1, T2> = Omit<T1, keyof T2> & T2;
|
||||
|
||||
export type DefinitelyDefined<T, K> = T & Override<T, K>;
|
||||
export type Expand<T> = T extends object ? { [K in keyof T]: Expand<T[K]> } : T;
|
||||
|
||||
type Reconstruct<T> = T extends Omit<infer O, infer _> ? O & Reconstruct<O> : T;
|
||||
|
||||
type IsOptional<T> = {
|
||||
[K in keyof T]-?: T[K] extends Required<T>[K] ? false : true;
|
||||
};
|
||||
|
||||
export type UnionToIntersection<T> = (T extends unknown ? (x: T) => unknown : never) extends (
|
||||
x: infer R,
|
||||
) => unknown
|
||||
? R
|
||||
: never;
|
||||
|
||||
Reference in New Issue
Block a user