mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2024-12-24 15:04:38 +03:00
feat(decorators): TelegrafMention added
This commit is contained in:
parent
8bd20131a1
commit
f5d9cf13b6
12
lib/decorators/telegraf-mention.decorator.ts
Normal file
12
lib/decorators/telegraf-mention.decorator.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { SetMetadata } from '@nestjs/common';
|
||||
import { DECORATORS } from '../telegraf.constants';
|
||||
|
||||
/**
|
||||
* Mention handling.
|
||||
* @param username Username
|
||||
*
|
||||
* https://telegraf.js.org/#/?id=mention
|
||||
*/
|
||||
export function TelegrafMention(username: string | string[]): MethodDecorator {
|
||||
return SetMetadata(DECORATORS.MENTION, { username });
|
||||
}
|
@ -78,4 +78,15 @@ export class TelegrafMetadataAccessor {
|
||||
getTelegrafEntityMetadata(target: Type<any> | Function) {
|
||||
return this.reflector.get(DECORATORS.ENTITY, target);
|
||||
}
|
||||
|
||||
isTelegrafMention(target: Type<any> | Function): boolean {
|
||||
if (!target) {
|
||||
return false;
|
||||
}
|
||||
return !!this.reflector.get(DECORATORS.MENTION, target);
|
||||
}
|
||||
|
||||
getTelegrafMentionMetadata(target: Type<any> | Function) {
|
||||
return this.reflector.get(DECORATORS.MENTION, target);
|
||||
}
|
||||
}
|
||||
|
@ -11,4 +11,5 @@ export const DECORATORS = {
|
||||
HELP: `${DECORATORS_PREFIX}/HELP`,
|
||||
SETTINGS: `${DECORATORS_PREFIX}/SETTINGS`,
|
||||
ENTITY: `${DECORATORS_PREFIX}/ENTITY`,
|
||||
MENTION: `${DECORATORS_PREFIX}/MENTION`,
|
||||
};
|
||||
|
@ -67,6 +67,11 @@ export class TelegrafExplorer implements OnModuleInit {
|
||||
instance[key],
|
||||
);
|
||||
this.handleTelegrafEntity(instance, key, telegraf, metadata);
|
||||
} else if (this.metadataAccessor.isTelegrafMention(instance[key])) {
|
||||
const metadata = this.metadataAccessor.getTelegrafMentionMetadata(
|
||||
instance[key],
|
||||
);
|
||||
this.handleTelegrafMention(instance, key, telegraf, metadata);
|
||||
}
|
||||
},
|
||||
);
|
||||
@ -142,4 +147,14 @@ export class TelegrafExplorer implements OnModuleInit {
|
||||
// @ts-ignore
|
||||
telegraf.entity(metadata.entity, instance[key].bind(instance));
|
||||
}
|
||||
|
||||
handleTelegrafMention(
|
||||
instance: object,
|
||||
key: string,
|
||||
telegraf: Telegraf<ContextMessageUpdate>,
|
||||
metadata: any,
|
||||
) {
|
||||
// @ts-ignore
|
||||
telegraf.mention(metadata.username, instance[key].bind(instance));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user