mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
refactor allat
This commit is contained in:
@@ -20,22 +20,21 @@ export function reconstruct<T extends Interaction>(event: T) {
|
||||
*
|
||||
* A magic number to represent any commandtype that is an ApplicationCommand.
|
||||
*/
|
||||
const appBitField = 0b000000001111;
|
||||
const PUBLISHABLE = 0b000000001111;
|
||||
|
||||
|
||||
const TypeMap = new Map<number, number>([
|
||||
[CommandType.Text, 0],
|
||||
[CommandType.Both, 0],
|
||||
[CommandType.Slash, ApplicationCommandType.ChatInput],
|
||||
[CommandType.CtxUser, ApplicationCommandType.User],
|
||||
[CommandType.CtxMsg, ApplicationCommandType.Message],
|
||||
[CommandType.Button, ComponentType.Button],
|
||||
[CommandType.Modal, InteractionType.ModalSubmit],
|
||||
[CommandType.StringSelect, ComponentType.StringSelect],
|
||||
[CommandType.UserSelect, ComponentType.UserSelect],
|
||||
[CommandType.MentionableSelect, ComponentType.MentionableSelect],
|
||||
[CommandType.RoleSelect, ComponentType.RoleSelect],
|
||||
[CommandType.ChannelSelect, ComponentType.ChannelSelect]]);
|
||||
const TypeMap = new Map<number, number>([[CommandType.Text, 0],
|
||||
[CommandType.Both, 0],
|
||||
[CommandType.Slash, ApplicationCommandType.ChatInput],
|
||||
[CommandType.CtxUser, ApplicationCommandType.User],
|
||||
[CommandType.CtxMsg, ApplicationCommandType.Message],
|
||||
[CommandType.Button, ComponentType.Button],
|
||||
[CommandType.Modal, InteractionType.ModalSubmit],
|
||||
[CommandType.StringSelect, ComponentType.StringSelect],
|
||||
[CommandType.UserSelect, ComponentType.UserSelect],
|
||||
[CommandType.MentionableSelect, ComponentType.MentionableSelect],
|
||||
[CommandType.RoleSelect, ComponentType.RoleSelect],
|
||||
[CommandType.ChannelSelect, ComponentType.ChannelSelect]]);
|
||||
|
||||
/*
|
||||
* Generates an id based on name and CommandType.
|
||||
@@ -52,7 +51,7 @@ export function create(name: string, type: CommandType | EventType) {
|
||||
if(type == CommandType.Modal) {
|
||||
return `${name}_M`;
|
||||
}
|
||||
const am = (appBitField & type) !== 0 ? 'A' : 'C';
|
||||
const am = (PUBLISHABLE & type) !== 0 ? 'A' : 'C';
|
||||
return `${name}_${am}${TypeMap.get(type)!}`
|
||||
}
|
||||
|
||||
|
||||
@@ -3,14 +3,11 @@ import type { IntoDependencies } from '../../types/ioc';
|
||||
import { useContainerRaw } from './base';
|
||||
|
||||
/**
|
||||
* @__PURE__
|
||||
* @since 2.0.0.
|
||||
* Creates a singleton object.
|
||||
* @param cb
|
||||
*/
|
||||
export function single<T>(cb: () => T) {
|
||||
return cb;
|
||||
}
|
||||
export function single<T>(cb: () => T) { return cb; }
|
||||
|
||||
/**
|
||||
* @__PURE__
|
||||
@@ -18,9 +15,7 @@ export function single<T>(cb: () => T) {
|
||||
* Creates a transient object
|
||||
* @param cb
|
||||
*/
|
||||
export function transient<T>(cb: () => () => T) {
|
||||
return cb;
|
||||
}
|
||||
export function transient<T>(cb: () => () => T) { return cb; }
|
||||
/**
|
||||
* The new Service api, a cleaner alternative to useContainer
|
||||
* To obtain intellisense, ensure a .d.ts file exists in the root of compilation.
|
||||
|
||||
@@ -9,7 +9,6 @@ import {
|
||||
EMPTY,
|
||||
every,
|
||||
fromEvent,
|
||||
map,
|
||||
Observable,
|
||||
of,
|
||||
OperatorFunction,
|
||||
@@ -46,7 +45,7 @@ export function callPlugin(args: unknown): OperatorFunction<PluginExecutable, Vo
|
||||
});
|
||||
}
|
||||
|
||||
export const arrayifySource = map(src => (Array.isArray(src) ? (src as unknown[]) : [src]));
|
||||
export const arrayifySource = <T>(src: T) => (Array.isArray(src) ? (src as unknown[]) : [src]);
|
||||
|
||||
/**
|
||||
* Checks if the stream of results is all ok.
|
||||
|
||||
@@ -105,8 +105,7 @@ export class Context extends CoreContext<Message, ChatInputCommandInteraction> {
|
||||
this.ctx
|
||||
.map(m => m.reply(content as MessageReplyOptions))
|
||||
.mapErr(i =>
|
||||
i.reply(content as InteractionReplyOptions).then(() => i.fetchReply()),
|
||||
),
|
||||
i.reply(content as InteractionReplyOptions).then(() => i.fetchReply())),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ import { PayloadType } from '../core/structures/enums'
|
||||
import { Err, Ok, Result } from 'ts-results-es';
|
||||
import type { Awaitable } from '../types/utility';
|
||||
import type { ControlPlugin } from '../types/core-plugin';
|
||||
import type { AnyModule, CommandMeta, CommandModule, Module, Processed } from '../types/core-modules';
|
||||
import type { CommandMeta, CommandModule, Module, Processed } from '../types/core-modules';
|
||||
import { EventEmitter } from 'node:events';
|
||||
import * as assert from 'node:assert';
|
||||
import { Context } from '../core/structures/context';
|
||||
@@ -50,7 +50,7 @@ function contextArgs(wrappable: Message | BaseInteraction, messageArgs?: string[
|
||||
|
||||
|
||||
function intoPayload(module: Processed<Module>, ) {
|
||||
return pipe(arrayifySource,
|
||||
return pipe(map(arrayifySource),
|
||||
map(args => ({ module, args })));
|
||||
}
|
||||
|
||||
@@ -244,7 +244,7 @@ export function createResultResolver<
|
||||
* Calls a module's init plugins and checks for Err. If so, call { onStop } and
|
||||
* ignore the module
|
||||
*/
|
||||
export function callInitPlugins<T extends Processed<AnyModule>>(sernEmitter: Emitter) {
|
||||
export function callInitPlugins<T extends Processed<Module>>(sernEmitter: Emitter) {
|
||||
return concatMap(
|
||||
createResultResolver({
|
||||
createStream: args => from(args.module.plugins).pipe(callPlugin(args)),
|
||||
|
||||
@@ -21,7 +21,7 @@ function hasPrefix(prefix: string, content: string) {
|
||||
}
|
||||
|
||||
export function messageHandler(
|
||||
[emitter, err, log, modules, client]: DependencyList,
|
||||
[emitter, err, log, client]: DependencyList,
|
||||
defaultPrefix: string | undefined,
|
||||
) {
|
||||
if (!defaultPrefix) {
|
||||
@@ -31,15 +31,15 @@ export function messageHandler(
|
||||
return EMPTY;
|
||||
}
|
||||
const messageStream$ = sharedEventStream<Message>(client, 'messageCreate');
|
||||
const handle = createMessageHandler(messageStream$, defaultPrefix, modules);
|
||||
|
||||
const msgCommands$ = handle(isNonBot(defaultPrefix));
|
||||
|
||||
return msgCommands$.pipe(
|
||||
filterTap((e) => emitter.emit('warning', resultPayload(PayloadType.Warning, undefined, e))),
|
||||
concatMap(makeModuleExecutor(module => {
|
||||
const result = resultPayload(PayloadType.Failure, module, SernError.PluginFailure);
|
||||
emitter.emit('module.activate', result);
|
||||
})),
|
||||
mergeMap(payload => executeModule(emitter, log, err, payload)));
|
||||
// const handle = createMessageHandler(messageStream$, defaultPrefix, modules);
|
||||
//
|
||||
// const msgCommands$ = handle(isNonBot(defaultPrefix));
|
||||
//
|
||||
// return msgCommands$.pipe(
|
||||
// filterTap((e) => emitter.emit('warning', resultPayload(PayloadType.Warning, undefined, e))),
|
||||
// concatMap(makeModuleExecutor(module => {
|
||||
// const result = resultPayload(PayloadType.Failure, module, SernError.PluginFailure);
|
||||
// emitter.emit('module.activate', result);
|
||||
// })),
|
||||
// mergeMap(payload => executeModule(emitter, log, err, payload)));
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ const once = (log: Logging | undefined) => pipe(
|
||||
ignoreElements())
|
||||
|
||||
export function readyHandler(
|
||||
[sEmitter, , log ,, client]: DependencyList,
|
||||
[sEmitter, , log, client]: DependencyList,
|
||||
allPaths: ObservableInput<string>,
|
||||
) {
|
||||
//Todo: add module manager on on ready
|
||||
|
||||
@@ -7,7 +7,7 @@ import type { DependencyList } from '../types/ioc';
|
||||
import type { EventModule, Processed } from '../types/core-modules';
|
||||
|
||||
export function eventsHandler(
|
||||
[emitter, err, log, moduleManager, client]: DependencyList,
|
||||
[emitter, err, log, client]: DependencyList,
|
||||
allPaths: ObservableInput<string>,
|
||||
) {
|
||||
//code smell
|
||||
|
||||
@@ -142,7 +142,6 @@ export type CommandModule =
|
||||
| RoleSelectCommand
|
||||
| ModalSubmitCommand;
|
||||
|
||||
export type AnyModule = CommandModule | EventModule;
|
||||
//https://stackoverflow.com/questions/64092736/alternative-to-switch-statement-for-typescript-discriminated-union
|
||||
// Explicit Module Definitions for mapping
|
||||
export interface CommandModuleDefs {
|
||||
|
||||
@@ -48,7 +48,7 @@ import type {
|
||||
UserContextMenuCommandInteraction,
|
||||
UserSelectMenuInteraction,
|
||||
} from 'discord.js';
|
||||
import { VoidResult } from '../core/_internal';
|
||||
import type { VoidResult } from '../core/_internal';
|
||||
|
||||
export type PluginResult = Awaitable<VoidResult>;
|
||||
|
||||
|
||||
4
src/types/dependencies.d.ts
vendored
4
src/types/dependencies.d.ts
vendored
@@ -10,3 +10,7 @@ declare global {
|
||||
interface Dependencies extends CoreDependencies {}
|
||||
|
||||
}
|
||||
|
||||
declare module './handler.js' {
|
||||
declare const commands : Map<string, any>
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@ export type DependencyList = [
|
||||
Contracts.Emitter,
|
||||
Contracts.ErrorHandling,
|
||||
Contracts.Logging | undefined,
|
||||
null,
|
||||
Contracts.Emitter,
|
||||
];
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { CommandInteractionOptionResolver, InteractionReplyOptions, MessageReplyOptions } from 'discord.js';
|
||||
import type { PayloadType } from '../core/structures/enums';
|
||||
import type { AnyModule } from './core-modules';
|
||||
import type { Module } from './core-modules';
|
||||
|
||||
export type Awaitable<T> = PromiseLike<T> | T;
|
||||
|
||||
@@ -18,14 +18,14 @@ export type Args = ParseType<{ text: string[]; slash: SlashOptions }>;
|
||||
export interface SernEventsMapping {
|
||||
'module.register': [Payload];
|
||||
'module.activate': [Payload];
|
||||
error: [{ type: PayloadType.Failure; module?: AnyModule; reason: string | Error }];
|
||||
error: [{ type: PayloadType.Failure; module?: Module; reason: string | Error }];
|
||||
warning: [Payload];
|
||||
modulesLoaded: [never?];
|
||||
}
|
||||
|
||||
export type Payload =
|
||||
| { type: PayloadType.Success; module: AnyModule }
|
||||
| { type: PayloadType.Failure; module?: AnyModule; reason: string | Error }
|
||||
| { type: PayloadType.Success; module: Module }
|
||||
| { type: PayloadType.Failure; module?: Module; reason: string | Error }
|
||||
| { type: PayloadType.Warning; module: undefined; reason: string };
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user