refactor: Bring CommandType and PluginType to top level

This commit is contained in:
Jacob Nguyen
2022-05-17 17:10:01 -05:00
parent f8987499f6
commit 82f1bad8ff
8 changed files with 32 additions and 32 deletions

View File

@@ -5,7 +5,7 @@ import * as Files from '../utilities/readFile';
import { match } from 'ts-pattern';
import { SernError } from '../structures/errors';
import Context from '../structures/context';
import { CommandType, controller } from '../sern';
import { controller } from '../sern';
import type { Module } from '../structures/module';
import {
isButton,
@@ -15,6 +15,7 @@ import {
isUserContextMenuCmd,
} from '../utilities/predicates';
import { filterCorrectModule } from './observableHandling';
import { CommandType } from '../structures/enums';
function applicationCommandHandler(mod: Module | undefined, interaction: CommandInteraction) {
const mod$ = <T extends CommandType>(cmdTy : T) => of(mod).pipe(

View File

@@ -2,12 +2,13 @@ import type { Message } from 'discord.js';
import { concatMap, filter, from, fromEvent, map, Observable, of } from 'rxjs';
import { Err } from 'ts-results';
import type { Args } from '../..';
import { CommandType, controller } from '../sern';
import { controller } from '../sern';
import Context from '../structures/context';
import type Wrapper from '../structures/wrapper';
import { fmt } from '../utilities/messageHelpers';
import * as Files from '../utilities/readFile';
import { filterCorrectModule, ignoreNonBot } from './observableHandling';
import { CommandType } from '../structures/enums';
export const onMessageCreate = (wrapper: Wrapper) => {
const { client, defaultPrefix } = wrapper;

View File

@@ -2,8 +2,7 @@ import { concat, concatMap, from, fromEvent, map, Observable, of, skip, take } f
import { basename } from 'path';
import * as Files from '../utilities/readFile';
import type Wrapper from '../structures/wrapper';
import { CommandType, controller } from '../sern';
import type { PluginType } from '../plugins/plugin';
import { controller } from '../sern';
import type { Result } from 'ts-results';
import type { Awaitable } from 'discord.js';
import type { Module } from '../structures/module';
@@ -12,6 +11,7 @@ import { ApplicationCommandType, ComponentType } from 'discord.js';
import { Err, Ok } from 'ts-results';
import { SernError } from '../structures/errors';
import type { DefinitelyDefined } from '../../types/handler';
import { CommandType, PluginType } from '../structures/enums';
export const onReady = (wrapper: Wrapper) => {
const { client, commands } = wrapper;

View File

@@ -13,9 +13,10 @@
import type { Awaitable, Client } from 'discord.js';
import type { Err, Ok, Result } from 'ts-results';
import type { Module, Override, Wrapper } from '../..';
import type { CommandType } from '../sern';
import type { Module, Override } from '../..';
import type { BaseModule, ModuleDefs } from '../structures/module';
import type { CommandType } from '../structures/enums';
import { PluginType } from '../structures/enums';
export interface Controller {
@@ -23,11 +24,6 @@ export interface Controller {
stop: () => Err<void>;
}
export enum PluginType {
Command = 0b01,
Event = 0b10,
}
type executeCmdPlugin = (controller: Controller) => Result<void, void> ;
type BasePlugin = Override<BaseModule, {
@@ -56,13 +52,11 @@ export function plugins<T extends CommandType>(...plug: (CommandPlugin | EventPl
export function sernModule<T extends CommandType>(
plugs: (CommandPlugin | EventPlugin<T>)[], mod : ModuleDefs[T]
) : ModuleDefs[T] {
//mod.plugins if defined, warn user to use first parameter
//mod.onEvent if defined, warn user to use first parameter
const plugins = plugs.filter(el => el.type === PluginType.Command);
const onEvent = plugs.filter(el => el.type === PluginType.Event);
return {
plugins,
onEvent,
...mod
} as unknown as ModuleDefs[T];
};
}

View File

@@ -10,6 +10,7 @@ import { onMessageCreate } from './events/messageEvent';
import { onInteractionCreate } from './events/interactionCreate';
import { match, P } from 'ts-pattern';
import { Err, Ok } from 'ts-results';
import { CommandType } from './structures/enums';
export function init(wrapper: Wrapper) {
const { events, client } = wrapper;
@@ -27,19 +28,6 @@ function eventObserver(client: Client, events: DiscordEvent[]) {
});
}
/**
* @enum { number };
*/
export enum CommandType {
Text = 0b0000001,
Slash = 0b0000010,
MenuUser = 0b0000100,
MenuMsg = 0b0001000,
Button = 0b0010000,
MenuSelect = 0b0100000,
Both = 0b0000011,
}
export function cmdTypeToDjs(ty: CommandType) {
return match(ty)
.with(CommandType.Slash, () => ApplicationCommandType.ChatInput)

View File

@@ -0,0 +1,19 @@
/**
* @enum { number };
*/
enum CommandType {
Text = 0b0000001,
Slash = 0b0000010,
MenuUser = 0b0000100,
MenuMsg = 0b0001000,
Button = 0b0010000,
MenuSelect = 0b0100000,
Both = 0b0000011,
}
enum PluginType {
Command = 0b01,
Event = 0b10,
}
export { CommandType, PluginType };

View File

@@ -8,10 +8,9 @@ import type {
} from 'discord.js';
import type { Override } from '../../types/handler';
import type { Args } from '../../types/handler';
import type { CommandType } from '../sern';
import type { CommandPlugin, EventPlugin } from '../plugins/plugin';
import type Context from './context';
import type { PluginType } from '../plugins/plugin';
import { CommandType, PluginType } from './enums';
export interface BaseModule {
type : CommandType | PluginType
@@ -47,7 +46,6 @@ export type ContextMenuUser = Override<BaseModule, {
type: CommandType.MenuUser;
onEvent?: EventPlugin<CommandType.MenuUser>[];
plugins?: (CommandPlugin)[];
description? : string;
execute: (ctx: UserContextMenuCommandInteraction) => Awaitable<void>
}>
@@ -55,7 +53,6 @@ export type ContextMenuMsg = Override<BaseModule, {
type: CommandType.MenuMsg;
onEvent?: EventPlugin<CommandType.MenuMsg>[];
plugins?: CommandPlugin[];
description? : string;
execute: (ctx: MessageContextMenuCommandInteraction) => Awaitable<void>
}>;

View File

@@ -1,5 +1,5 @@
import Context from './context';
import type { BothCommand, Module, SlashCommand, TextCommand } from './module';
import type Wrapper from './wrapper';
export * from './enums';
export { Context, SlashCommand, TextCommand, BothCommand, Module, Wrapper };