feat(decorators): TelegrafSettings added

This commit is contained in:
Aleksandr Bukhalo 2020-03-19 16:46:31 +03:00
parent fa3a7f5258
commit 55c6f939a4
4 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,11 @@
import { SetMetadata } from '@nestjs/common';
import { DECORATORS } from '../telegraf.constants';
/**
* Handler for /settings command.
*
* https://telegraf.js.org/#/?id=settings
*/
export function TelegrafSettings(): MethodDecorator {
return SetMetadata(DECORATORS.SETTINGS, {});
}

View File

@ -60,4 +60,11 @@ export class TelegrafMetadataAccessor {
} }
return !!this.reflector.get(DECORATORS.HELP, target); return !!this.reflector.get(DECORATORS.HELP, target);
} }
isTelegrafSettings(target: Type<any> | Function): boolean {
if (!target) {
return false;
}
return !!this.reflector.get(DECORATORS.SETTINGS, target);
}
} }

View File

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

View File

@ -60,6 +60,8 @@ export class TelegrafExplorer implements OnModuleInit {
this.handleTelegrafStart(instance, key, telegraf); this.handleTelegrafStart(instance, key, telegraf);
} else if (this.metadataAccessor.isTelegrafHelp(instance[key])) { } else if (this.metadataAccessor.isTelegrafHelp(instance[key])) {
this.handleTelegrafHelp(instance, key, telegraf); this.handleTelegrafHelp(instance, key, telegraf);
} else if (this.metadataAccessor.isTelegrafSettings(instance[key])) {
this.handleTelegrafSettings(instance, key, telegraf);
} }
}, },
); );
@ -116,4 +118,13 @@ export class TelegrafExplorer implements OnModuleInit {
) { ) {
telegraf.help(instance[key].bind(instance)); telegraf.help(instance[key].bind(instance));
} }
handleTelegrafSettings(
instance: object,
key: string,
telegraf: Telegraf<ContextMessageUpdate>,
) {
// @ts-ignore
telegraf.settings(instance[key].bind(instance));
}
} }