mirror of
https://github.com/sern-handler/handler
synced 2026-06-17 21:32:14 +00:00
refactor: switching to when statements and type predicates
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import type {
|
||||
ChatInputCommandInteraction,
|
||||
CommandInteraction,
|
||||
Interaction,
|
||||
MessageComponentInteraction,
|
||||
MessageContextMenuCommandInteraction as MessageCtxInt,
|
||||
UserContextMenuCommandInteraction as UserCtxInt,
|
||||
} from 'discord.js';
|
||||
import type { SelectMenuInteraction } from 'discord.js';
|
||||
import { concatMap, fromEvent, Observable, of, throwError } from 'rxjs';
|
||||
import type Wrapper from '../structures/wrapper';
|
||||
import * as Files from '../utilities/readFile';
|
||||
@@ -14,16 +15,11 @@ import Context from '../structures/context';
|
||||
import type { Result } from 'ts-results';
|
||||
import { CommandType, controller } from '../sern';
|
||||
import type { Args, UnionToTuple } from '../../types/handler';
|
||||
import type { MessageComponentInteraction } from 'discord.js';
|
||||
import { ComponentType } from 'discord.js';
|
||||
import type { Module } from '../structures/module';
|
||||
import type { EventPlugin } from '../plugins/plugin';
|
||||
import { isButton, isChatInputCommand, isSelectMenu } from '../utilities/predicates';
|
||||
|
||||
|
||||
function isChatInputCommand(i: CommandInteraction): i is ChatInputCommandInteraction {
|
||||
return i.isChatInputCommand();
|
||||
}
|
||||
|
||||
function applicationCommandHandler(mod: Module | undefined, interaction: CommandInteraction) {
|
||||
if (mod === undefined) {
|
||||
return throwError(() => SernError.UndefinedModule);
|
||||
@@ -33,13 +29,13 @@ function applicationCommandHandler(mod: Module | undefined, interaction: Command
|
||||
.when(isChatInputCommand, i => {
|
||||
const ctx = Context.wrap(i);
|
||||
const res = eventPlugins.map(e => {
|
||||
return (<EventPlugin<CommandType.Both>>e).execute(
|
||||
return (<EventPlugin<CommandType.Slash>>e).execute(
|
||||
[ctx, <Args>['slash', i.options]]
|
||||
, controller);
|
||||
}) as Awaited<Result<void, void>>[];
|
||||
//Possible unsafe cast
|
||||
// could result in the promises not being resolved
|
||||
return of({ type: mod.type, res, plugged: mod, ctx });
|
||||
return of({ type: CommandType.Slash, res, mod, ctx });
|
||||
},
|
||||
)
|
||||
.when(
|
||||
@@ -51,7 +47,7 @@ function applicationCommandHandler(mod: Module | undefined, interaction: Command
|
||||
[ctx] as UnionToTuple<CommandType.MenuMsg | CommandType.MenuUser>
|
||||
, controller);
|
||||
}) as Awaited<Result<void, void>>[];
|
||||
return of({ type: mod.type, res, plugged: mod, ctx });
|
||||
return of({ type: mod.type, res, mod, ctx });
|
||||
},
|
||||
)
|
||||
.run();
|
||||
@@ -66,13 +62,17 @@ function messageComponentInteractionHandler(
|
||||
}
|
||||
const eventPlugins = mod.onEvent;
|
||||
return match(interaction)
|
||||
.with({
|
||||
componentType: P.union(ComponentType.Button, ComponentType.SelectMenu),
|
||||
}, (ctx) => {
|
||||
.when(isButton, ctx => {
|
||||
const res = eventPlugins.map(e => {
|
||||
return e.execute([ctx] as UnionToTuple<CommandType.Button | CommandType.MenuSelect>, controller);
|
||||
return (<EventPlugin<CommandType.Button>>e).execute([ctx], controller);
|
||||
}) as Awaited<Result<void, void>>[];
|
||||
return of({ type: mod.type, res, plugged: mod, ctx });
|
||||
return of({ type: mod.type, res, mod, ctx });
|
||||
})
|
||||
.when(isSelectMenu, (ctx: SelectMenuInteraction) => {
|
||||
const res = eventPlugins.map(e => {
|
||||
return (<EventPlugin<CommandType.MenuSelect>>e).execute([ctx], controller);
|
||||
}) as Awaited<Result<void, void>>[];
|
||||
return of({ type: mod.type, res, mod, ctx });
|
||||
})
|
||||
.otherwise(() => throwError(() => SernError.NotSupportedInteraction));
|
||||
}
|
||||
@@ -100,7 +100,9 @@ export const onInteractionCreate = (wrapper: Wrapper) => {
|
||||
} else return throwError(() => SernError.NotSupportedInteraction);
|
||||
}),
|
||||
)
|
||||
.subscribe(console.log);
|
||||
.subscribe(m => {
|
||||
m;
|
||||
});
|
||||
|
||||
|
||||
};
|
||||
|
||||
@@ -15,8 +15,8 @@ export const onReady = (wrapper: Wrapper) => {
|
||||
const ready$ = fromEvent(client, 'ready').pipe(take(1), skip(1));
|
||||
const processCommandFiles$ = Files.buildData(commands).pipe(
|
||||
map(({ mod, absPath }) => {
|
||||
const name = mod?.name ?? Files.fmtFileName(basename(absPath));
|
||||
if (mod?.name === undefined) {
|
||||
const name = Files.fmtFileName(basename(absPath));
|
||||
return { name, ...mod };
|
||||
}
|
||||
return mod;
|
||||
|
||||
@@ -50,6 +50,9 @@ export type EventPlugin<T extends CommandType> = {
|
||||
{
|
||||
execute: (event: Parameters<ModuleDefs[T]['execute']>, controller: Controller) => Awaitable<Result<void, void>>;
|
||||
}>;
|
||||
export type EventPluginType = {
|
||||
[K in CommandType] : EventPlugin<K>
|
||||
}
|
||||
|
||||
export function plugins(...plug: CommandPlugin[]): CommandPlugin[];
|
||||
export function plugins<T extends CommandType>(...plug: EventPlugin<T>[]): EventPlugin<T>[];
|
||||
@@ -58,6 +61,6 @@ export function plugins<T extends CommandType>(...plug: CommandPlugin[] | EventP
|
||||
return plug;
|
||||
}
|
||||
|
||||
export function sernModule(mod: Module): Module {
|
||||
export function sernModule<T extends CommandType>(mod: ModuleDefs[T]): Module {
|
||||
return mod;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,25 @@
|
||||
import type { Module, ModuleDefs } from '../structures/module';
|
||||
import type { ChatInputCommandInteraction, CommandInteraction } from 'discord.js';
|
||||
import type { EventPlugin } from '../../../dist';
|
||||
import { CommandType } from '../sern';
|
||||
import type { EventPluginType } from '../plugins/plugin';
|
||||
import type { ButtonInteraction, MessageComponentInteraction, SelectMenuInteraction } from 'discord.js';
|
||||
|
||||
|
||||
export function correctModuleType<T extends keyof ModuleDefs>(
|
||||
plug: Module | undefined,
|
||||
type: T,
|
||||
): plug is ModuleDefs[T] {
|
||||
return plug !== undefined && plug.type === type;
|
||||
}
|
||||
|
||||
export function isChatInputCommand(i: CommandInteraction): i is ChatInputCommandInteraction {
|
||||
return i.isChatInputCommand();
|
||||
}
|
||||
|
||||
export function isButton(i : MessageComponentInteraction) : i is ButtonInteraction {
|
||||
return i.isButton();
|
||||
}
|
||||
export function isSelectMenu(i : MessageComponentInteraction) : i is SelectMenuInteraction {
|
||||
return i.isSelectMenu();
|
||||
}
|
||||
Reference in New Issue
Block a user