mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-01 20:46:08 +00:00
feat: Make Component#filter optional and add customId pass (#330)
* feat: enhance ComponentCommand and ModalCommand with optional customId and filter methods * fix: lol * fix: simplify conditional checks for customId in ComponentHandler * fix: optional chain goes brrr * feat: rework?
This commit is contained in:
parent
008c2da719
commit
f74f29e96a
@ -14,9 +14,17 @@ export interface ComponentCommand {
|
|||||||
export abstract class ComponentCommand {
|
export abstract class ComponentCommand {
|
||||||
type = InteractionCommandType.COMPONENT;
|
type = InteractionCommandType.COMPONENT;
|
||||||
abstract componentType: keyof ContextComponentCommandInteractionMap;
|
abstract componentType: keyof ContextComponentCommandInteractionMap;
|
||||||
abstract filter(context: ComponentContext<typeof this.componentType>): Promise<boolean> | boolean;
|
customId?: string;
|
||||||
|
filter?(context: ComponentContext<typeof this.componentType>): Promise<boolean> | boolean;
|
||||||
abstract run(context: ComponentContext<typeof this.componentType>): any;
|
abstract run(context: ComponentContext<typeof this.componentType>): any;
|
||||||
|
|
||||||
|
/** @internal */
|
||||||
|
async _filter(context: ComponentContext) {
|
||||||
|
const old = (await this.filter?.(context)) ?? true;
|
||||||
|
if (this.customId) return this.customId === context.customId && old;
|
||||||
|
return old;
|
||||||
|
}
|
||||||
|
|
||||||
middlewares: (keyof RegisteredMiddlewares)[] = [];
|
middlewares: (keyof RegisteredMiddlewares)[] = [];
|
||||||
|
|
||||||
props!: ExtraProps;
|
props!: ExtraProps;
|
||||||
|
@ -310,7 +310,7 @@ export class ComponentHandler extends BaseHandler {
|
|||||||
if (
|
if (
|
||||||
i.type === InteractionCommandType.COMPONENT &&
|
i.type === InteractionCommandType.COMPONENT &&
|
||||||
i.cType === context.interaction.componentType &&
|
i.cType === context.interaction.componentType &&
|
||||||
(await i.filter(context))
|
(await i._filter(context))
|
||||||
) {
|
) {
|
||||||
context.command = i;
|
context.command = i;
|
||||||
await this.execute(i, context);
|
await this.execute(i, context);
|
||||||
@ -324,7 +324,7 @@ export class ComponentHandler extends BaseHandler {
|
|||||||
async executeModal(context: ModalContext) {
|
async executeModal(context: ModalContext) {
|
||||||
for (const i of this.commands) {
|
for (const i of this.commands) {
|
||||||
try {
|
try {
|
||||||
if (i.type === InteractionCommandType.MODAL && (await i.filter(context))) {
|
if (i.type === InteractionCommandType.MODAL && (await i._filter(context))) {
|
||||||
context.command = i;
|
context.command = i;
|
||||||
await this.execute(i, context);
|
await this.execute(i, context);
|
||||||
}
|
}
|
||||||
|
@ -8,9 +8,17 @@ export interface ModalCommand {
|
|||||||
|
|
||||||
export abstract class ModalCommand {
|
export abstract class ModalCommand {
|
||||||
type = InteractionCommandType.MODAL;
|
type = InteractionCommandType.MODAL;
|
||||||
abstract filter(context: ModalContext): Promise<boolean> | boolean;
|
filter?(context: ModalContext): Promise<boolean> | boolean;
|
||||||
|
customId?: string;
|
||||||
abstract run(context: ModalContext): any;
|
abstract run(context: ModalContext): any;
|
||||||
|
|
||||||
|
/** @internal */
|
||||||
|
async _filter(context: ModalContext) {
|
||||||
|
const old = (await this.filter?.(context)) ?? true;
|
||||||
|
if (this.customId) return this.customId === context.customId && old;
|
||||||
|
return old;
|
||||||
|
}
|
||||||
|
|
||||||
middlewares: (keyof RegisteredMiddlewares)[] = [];
|
middlewares: (keyof RegisteredMiddlewares)[] = [];
|
||||||
|
|
||||||
props!: ExtraProps;
|
props!: ExtraProps;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user