mirror of
				https://github.com/Maks1mS/nestjs-telegraf.git
				synced 2025-10-27 20:44:59 +03:00 
			
		
		
		
	feat: create inject all bots decorator
This commit is contained in:
		| @@ -2,3 +2,4 @@ export * from './update.decorator'; | |||||||
| export * from './scene.decorator'; | export * from './scene.decorator'; | ||||||
| export * from './wizard.decorator'; | export * from './wizard.decorator'; | ||||||
| export * from './inject-bot.decorator'; | export * from './inject-bot.decorator'; | ||||||
|  | export * from './inject-all-bots.decorator'; | ||||||
|   | |||||||
							
								
								
									
										9
									
								
								lib/decorators/core/inject-all-bots.decorator.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								lib/decorators/core/inject-all-bots.decorator.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | |||||||
|  | import { Inject } from '@nestjs/common'; | ||||||
|  | import { Telegraf } from 'telegraf'; | ||||||
|  |  | ||||||
|  | import { getAllBotsToken } from '../../utils/get-all-bots-token.util'; | ||||||
|  |  | ||||||
|  | export type AllBotsMap = Map<string, Telegraf<any>>; | ||||||
|  |  | ||||||
|  | export const InjectAllBots = (): ParameterDecorator => | ||||||
|  |   Inject(getAllBotsToken()); | ||||||
| @@ -10,8 +10,7 @@ export class BaseExplorerService { | |||||||
|     if (!include || isEmpty(include)) { |     if (!include || isEmpty(include)) { | ||||||
|       return [...modulesContainer.values()]; |       return [...modulesContainer.values()]; | ||||||
|     } |     } | ||||||
|     const whitelisted = this.includeWhitelisted(modulesContainer, include); |     return this.includeWhitelisted(modulesContainer, include); | ||||||
|     return whitelisted; |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   includeWhitelisted( |   includeWhitelisted( | ||||||
|   | |||||||
							
								
								
									
										12
									
								
								lib/telegraf-all-bots.provider.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								lib/telegraf-all-bots.provider.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | |||||||
|  | import { Provider } from '@nestjs/common'; | ||||||
|  | import { Telegraf } from 'telegraf'; | ||||||
|  | import { TELEGRAF_ALL_BOTS } from './telegraf.constants'; | ||||||
|  |  | ||||||
|  | export const allBotsMap = new Map<string, Telegraf<any>>(); | ||||||
|  |  | ||||||
|  | setTimeout(() => console.log(allBotsMap), 2000); | ||||||
|  |  | ||||||
|  | export const telegrafAllBotsProvider: Provider = { | ||||||
|  |   provide: TELEGRAF_ALL_BOTS, | ||||||
|  |   useValue: allBotsMap, | ||||||
|  | }; | ||||||
| @@ -19,6 +19,10 @@ import { | |||||||
| } from './telegraf.constants'; | } from './telegraf.constants'; | ||||||
| import { ListenersExplorerService, MetadataAccessorService } from './services'; | import { ListenersExplorerService, MetadataAccessorService } from './services'; | ||||||
| import { telegrafStageProvider } from './stage.provider'; | import { telegrafStageProvider } from './stage.provider'; | ||||||
|  | import { | ||||||
|  |   allBotsMap, | ||||||
|  |   telegrafAllBotsProvider, | ||||||
|  | } from './telegraf-all-bots.provider'; | ||||||
| import { createBotFactory, getBotToken } from './utils'; | import { createBotFactory, getBotToken } from './utils'; | ||||||
|  |  | ||||||
| @Global() | @Global() | ||||||
| @@ -43,7 +47,11 @@ export class TelegrafCoreModule implements OnApplicationShutdown { | |||||||
|  |  | ||||||
|     const telegrafBotProvider: Provider = { |     const telegrafBotProvider: Provider = { | ||||||
|       provide: telegrafBotName, |       provide: telegrafBotName, | ||||||
|       useFactory: async () => await createBotFactory(options), |       useFactory: async () => { | ||||||
|  |         const bot = await createBotFactory(options); | ||||||
|  |         allBotsMap.set(telegrafBotName, bot); | ||||||
|  |         return bot; | ||||||
|  |       }, | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|     return { |     return { | ||||||
| @@ -56,11 +64,13 @@ export class TelegrafCoreModule implements OnApplicationShutdown { | |||||||
|         telegrafStageProvider, |         telegrafStageProvider, | ||||||
|         telegrafBotNameProvider, |         telegrafBotNameProvider, | ||||||
|         telegrafBotProvider, |         telegrafBotProvider, | ||||||
|  |         telegrafAllBotsProvider, | ||||||
|       ], |       ], | ||||||
|       exports: [ |       exports: [ | ||||||
|         telegrafStageProvider, |         telegrafStageProvider, | ||||||
|         telegrafBotNameProvider, |         telegrafBotNameProvider, | ||||||
|         telegrafBotProvider, |         telegrafBotProvider, | ||||||
|  |         telegrafAllBotsProvider, | ||||||
|       ], |       ], | ||||||
|     }; |     }; | ||||||
|   } |   } | ||||||
| @@ -77,8 +87,11 @@ export class TelegrafCoreModule implements OnApplicationShutdown { | |||||||
|  |  | ||||||
|     const telegrafBotProvider: Provider = { |     const telegrafBotProvider: Provider = { | ||||||
|       provide: telegrafBotName, |       provide: telegrafBotName, | ||||||
|       useFactory: async (options: TelegrafModuleOptions) => |       useFactory: async (options: TelegrafModuleOptions) => { | ||||||
|         await createBotFactory(options), |         const bot = await createBotFactory(options); | ||||||
|  |         allBotsMap.set(telegrafBotName, bot); | ||||||
|  |         return bot; | ||||||
|  |       }, | ||||||
|       inject: [TELEGRAF_MODULE_OPTIONS], |       inject: [TELEGRAF_MODULE_OPTIONS], | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
| @@ -91,11 +104,13 @@ export class TelegrafCoreModule implements OnApplicationShutdown { | |||||||
|         telegrafStageProvider, |         telegrafStageProvider, | ||||||
|         telegrafBotNameProvider, |         telegrafBotNameProvider, | ||||||
|         telegrafBotProvider, |         telegrafBotProvider, | ||||||
|  |         telegrafAllBotsProvider, | ||||||
|       ], |       ], | ||||||
|       exports: [ |       exports: [ | ||||||
|         telegrafStageProvider, |         telegrafStageProvider, | ||||||
|         telegrafBotNameProvider, |         telegrafBotNameProvider, | ||||||
|         telegrafBotProvider, |         telegrafBotProvider, | ||||||
|  |         telegrafAllBotsProvider, | ||||||
|       ], |       ], | ||||||
|     }; |     }; | ||||||
|   } |   } | ||||||
|   | |||||||
| @@ -12,3 +12,5 @@ export const WIZARD_STEP_METADATA = 'WIZARD_STEP_METADATA'; | |||||||
| export const PARAM_ARGS_METADATA = ROUTE_ARGS_METADATA; | export const PARAM_ARGS_METADATA = ROUTE_ARGS_METADATA; | ||||||
|  |  | ||||||
| export const TELEGRAF_STAGE = 'TelegrafStage'; | export const TELEGRAF_STAGE = 'TelegrafStage'; | ||||||
|  |  | ||||||
|  | export const TELEGRAF_ALL_BOTS = 'TELEGRAF_ALL_BOTS'; | ||||||
|   | |||||||
							
								
								
									
										3
									
								
								lib/utils/get-all-bots-token.util.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								lib/utils/get-all-bots-token.util.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | |||||||
|  | import { TELEGRAF_ALL_BOTS } from '../telegraf.constants'; | ||||||
|  |  | ||||||
|  | export const getAllBotsToken = (): string => TELEGRAF_ALL_BOTS; | ||||||
		Reference in New Issue
	
	Block a user