feat(decorators): TelegrafHelp added

This commit is contained in:
Aleksandr Bukhalo 2020-03-19 16:44:05 +03:00
parent c0ffdeff34
commit fa3a7f5258
5 changed files with 47 additions and 17 deletions

View File

@ -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';

View File

@ -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, {});
}

View File

@ -14,13 +14,6 @@ export class TelegrafMetadataAccessor {
return !!this.reflector.get(DECORATORS.USE, target);
}
isTelegrafStart(target: Type<any> | Function): boolean {
if (!target) {
return false;
}
return !!this.reflector.get(DECORATORS.START, target);
}
isTelegrafOn(target: Type<any> | Function): boolean {
if (!target) {
return false;
@ -53,4 +46,18 @@ export class TelegrafMetadataAccessor {
getTelegrafCommandMetadata(target: Type<any> | Function) {
return this.reflector.get(DECORATORS.COMMAND, target);
}
isTelegrafStart(target: Type<any> | Function): boolean {
if (!target) {
return false;
}
return !!this.reflector.get(DECORATORS.START, target);
}
isTelegrafHelp(target: Type<any> | Function): boolean {
if (!target) {
return false;
}
return !!this.reflector.get(DECORATORS.HELP, target);
}
}

View File

@ -8,4 +8,5 @@ export const DECORATORS = {
HEARS: `${DECORATORS_PREFIX}/HEARS`,
COMMAND: `${DECORATORS_PREFIX}/COMMAND`,
START: `${DECORATORS_PREFIX}/START`,
HELP: `${DECORATORS_PREFIX}/HELP`,
};

View File

@ -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<ContextMessageUpdate>,
) {
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<ContextMessageUpdate>,
) {
telegraf.start(instance[key].bind(instance));
}
handleTelegrafHelp(
instance: object,
key: string,
telegraf: Telegraf<ContextMessageUpdate>,
) {
telegraf.help(instance[key].bind(instance));
}
}