From fa3a7f52584727ca856786defdd078c585bf76cb Mon Sep 17 00:00:00 2001 From: Aleksandr Bukhalo Date: Thu, 19 Mar 2020 16:44:05 +0300 Subject: [PATCH] feat(decorators): TelegrafHelp added --- lib/decorators/index.ts | 1 + lib/decorators/telegraf-help.decorator.ts | 11 +++++++++ lib/telegraf-metadata.accessor.ts | 21 ++++++++++------ lib/telegraf.constants.ts | 1 + lib/telegraf.explorer.ts | 30 +++++++++++++++-------- 5 files changed, 47 insertions(+), 17 deletions(-) create mode 100644 lib/decorators/telegraf-help.decorator.ts diff --git a/lib/decorators/index.ts b/lib/decorators/index.ts index c94e0a3..63c6636 100644 --- a/lib/decorators/index.ts +++ b/lib/decorators/index.ts @@ -3,3 +3,4 @@ export * from './telegraf-on.decorator'; export * from './telegraf-hears.decorator'; export * from './telegraf-command.decorator'; export * from './telegraf-start.decorator'; +export * from './telegraf-help.decorator'; diff --git a/lib/decorators/telegraf-help.decorator.ts b/lib/decorators/telegraf-help.decorator.ts new file mode 100644 index 0000000..7bade62 --- /dev/null +++ b/lib/decorators/telegraf-help.decorator.ts @@ -0,0 +1,11 @@ +import { SetMetadata } from '@nestjs/common'; +import { DECORATORS } from '../telegraf.constants'; + +/** + * Handler for /help command. + * + * https://telegraf.js.org/#/?id=help + */ +export function TelegrafHelp(): MethodDecorator { + return SetMetadata(DECORATORS.HELP, {}); +} diff --git a/lib/telegraf-metadata.accessor.ts b/lib/telegraf-metadata.accessor.ts index d419ece..30ce062 100644 --- a/lib/telegraf-metadata.accessor.ts +++ b/lib/telegraf-metadata.accessor.ts @@ -14,13 +14,6 @@ export class TelegrafMetadataAccessor { return !!this.reflector.get(DECORATORS.USE, target); } - isTelegrafStart(target: Type | Function): boolean { - if (!target) { - return false; - } - return !!this.reflector.get(DECORATORS.START, target); - } - isTelegrafOn(target: Type | Function): boolean { if (!target) { return false; @@ -53,4 +46,18 @@ export class TelegrafMetadataAccessor { getTelegrafCommandMetadata(target: Type | Function) { return this.reflector.get(DECORATORS.COMMAND, target); } + + isTelegrafStart(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.START, target); + } + + isTelegrafHelp(target: Type | Function): boolean { + if (!target) { + return false; + } + return !!this.reflector.get(DECORATORS.HELP, target); + } } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index 114821e..78ee19a 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -8,4 +8,5 @@ export const DECORATORS = { HEARS: `${DECORATORS_PREFIX}/HEARS`, COMMAND: `${DECORATORS_PREFIX}/COMMAND`, START: `${DECORATORS_PREFIX}/START`, + HELP: `${DECORATORS_PREFIX}/HELP`, }; diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index c2f12cc..2187ffd 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -41,8 +41,6 @@ export class TelegrafExplorer implements OnModuleInit { (key: string) => { if (this.metadataAccessor.isTelegrafUse(instance[key])) { this.handleTelegrafUse(instance, key, telegraf); - } else if (this.metadataAccessor.isTelegrafStart(instance[key])) { - this.handleTelegrafStart(instance, key, telegraf); } else if (this.metadataAccessor.isTelegrafOn(instance[key])) { const metadata = this.metadataAccessor.getTelegrafOnMetadata( instance[key], @@ -58,6 +56,10 @@ export class TelegrafExplorer implements OnModuleInit { instance[key], ); this.handleTelegrafCommand(instance, key, telegraf, metadata); + } else if (this.metadataAccessor.isTelegrafStart(instance[key])) { + this.handleTelegrafStart(instance, key, telegraf); + } else if (this.metadataAccessor.isTelegrafHelp(instance[key])) { + this.handleTelegrafHelp(instance, key, telegraf); } }, ); @@ -81,14 +83,6 @@ export class TelegrafExplorer implements OnModuleInit { telegraf.on(metadata.updateTypes, instance[key].bind(instance)); } - handleTelegrafStart( - instance: object, - key: string, - telegraf: Telegraf, - ) { - telegraf.start(instance[key].bind(instance)); - } - handleTelegrafHears( instance: object, key: string, @@ -106,4 +100,20 @@ export class TelegrafExplorer implements OnModuleInit { ) { telegraf.command(metadata.commands, instance[key].bind(instance)); } + + handleTelegrafStart( + instance: object, + key: string, + telegraf: Telegraf, + ) { + telegraf.start(instance[key].bind(instance)); + } + + handleTelegrafHelp( + instance: object, + key: string, + telegraf: Telegraf, + ) { + telegraf.help(instance[key].bind(instance)); + } }