feat: use regex in customId component command (#345)

This commit is contained in:
Free 公園 2025-06-13 21:08:47 -05:00 committed by GitHub
parent 61413181bb
commit b84d462ce2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -14,13 +14,18 @@ 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;
customId?: string; customId?: string | RegExp;
filter?(context: ComponentContext<typeof this.componentType>): Promise<boolean> | boolean; 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 */ /** @internal */
_filter(context: ComponentContext) { _filter(context: ComponentContext) {
if (this.customId && this.customId !== context.customId) return false; if (this.customId) {
const isString = typeof this.customId === 'string';
const matches = isString ? this.customId === context.customId : (this.customId as RegExp).test(context.customId);
if (!matches) return false;
}
if (this.filter) return this.filter(context); if (this.filter) return this.filter(context);
return true; return true;
} }