From c0ffdeff3498f801d0cf1e571c014cc30bfbdb40 Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 16:39:47 +0300 Subject: [PATCH] feat(decorators): TelegrafCommand added --- lib/decorators/index.ts | 1 + lib/decorators/telegraf-command.decorator.ts | 12 ++++++++++++ lib/telegraf-metadata.accessor.ts | 11 +++++++++++ lib/telegraf.constants.ts | 1 + lib/telegraf.explorer.ts | 14 ++++++++++++++ 5 files changed, 39 insertions(+) create mode 100644 lib/decorators/telegraf-command.decorator.ts diff --git a/lib/decorators/index.ts b/lib/decorators/index.ts index 42c00a1..c94e0a3 100644 --- a/lib/decorators/index.ts +++ b/lib/decorators/index.ts @@ -1,4 +1,5 @@ export * from './telegraf-use.decorator'; export * from './telegraf-on.decorator'; export * from './telegraf-hears.decorator'; +export * from './telegraf-command.decorator'; export * from './telegraf-start.decorator'; diff --git a/lib/decorators/telegraf-command.decorator.ts b/lib/decorators/telegraf-command.decorator.ts new file mode 100644 index 0000000..bb20866 --- /dev/null +++ b/lib/decorators/telegraf-command.decorator.ts @@ -0,0 +1,12 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; + +/** + * Command handling. + * @param commands Commands + * + * https://telegraf.js.org/#/?id=command + */ +export function TelegrafCommand(commands: string | string[]): MethodDecorator { + return SetMetadata(DECORATORS.COMMAND, { commands }); +} diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index cad0d9b..d419ece 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -42,4 +42,15 @@ export class TelegrafMetadataAccessor { getTelegrafHearsMetadata(target: Type | Function) { return this.reflector.get(DECORATORS.HEARS, target); } + + isTelegrafCommand(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.COMMAND, target); + } + + getTelegrafCommandMetadata(target: Type | Function) { + return this.reflector.get(DECORATORS.COMMAND, target); + } } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index d68f1aa..114821e 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -6,5 +6,6 @@ export const DECORATORS = { USE: `${DECORATORS_PREFIX}/USE`, ON: `${DECORATORS_PREFIX}/ON`, HEARS: `${DECORATORS_PREFIX}/HEARS`, + COMMAND: `${DECORATORS_PREFIX}/COMMAND`, START: `${DECORATORS_PREFIX}/START`, }; diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index feda0f0..c2f12cc 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -53,6 +53,11 @@ export class TelegrafExplorer implements OnModuleInit { instance[key], ); this.handleTelegrafHears(instance, key, telegraf, metadata); + } else if (this.metadataAccessor.isTelegrafCommand(instance[key])) { + const metadata = this.metadataAccessor.getTelegrafCommandMetadata( + instance[key], + ); + this.handleTelegrafCommand(instance, key, telegraf, metadata); } }, ); @@ -92,4 +97,13 @@ export class TelegrafExplorer implements OnModuleInit { ) { telegraf.hears(metadata.triggers, instance[key].bind(instance)); } + + handleTelegrafCommand( + instance: object, + key: string, + telegraf: Telegraf, + metadata: any, + ) { + telegraf.command(metadata.commands, instance[key].bind(instance)); + } }