mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-03-28 20:33:44 +03:00
feat: create inject all bots decorator
This commit is contained in:
parent
9935b87d45
commit
92ff5164a6
@ -2,3 +2,4 @@ export * from './update.decorator';
|
||||
export * from './scene.decorator';
|
||||
export * from './wizard.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)) {
|
||||
return [...modulesContainer.values()];
|
||||
}
|
||||
const whitelisted = this.includeWhitelisted(modulesContainer, include);
|
||||
return whitelisted;
|
||||
return this.includeWhitelisted(modulesContainer, include);
|
||||
}
|
||||
|
||||
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';
|
||||
import { ListenersExplorerService, MetadataAccessorService } from './services';
|
||||
import { telegrafStageProvider } from './stage.provider';
|
||||
import {
|
||||
allBotsMap,
|
||||
telegrafAllBotsProvider,
|
||||
} from './telegraf-all-bots.provider';
|
||||
import { createBotFactory, getBotToken } from './utils';
|
||||
|
||||
@Global()
|
||||
@ -43,7 +47,11 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
|
||||
|
||||
const telegrafBotProvider: Provider = {
|
||||
provide: telegrafBotName,
|
||||
useFactory: async () => await createBotFactory(options),
|
||||
useFactory: async () => {
|
||||
const bot = await createBotFactory(options);
|
||||
allBotsMap.set(telegrafBotName, bot);
|
||||
return bot;
|
||||
},
|
||||
};
|
||||
|
||||
return {
|
||||
@ -56,11 +64,13 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
|
||||
telegrafStageProvider,
|
||||
telegrafBotNameProvider,
|
||||
telegrafBotProvider,
|
||||
telegrafAllBotsProvider,
|
||||
],
|
||||
exports: [
|
||||
telegrafStageProvider,
|
||||
telegrafBotNameProvider,
|
||||
telegrafBotProvider,
|
||||
telegrafAllBotsProvider,
|
||||
],
|
||||
};
|
||||
}
|
||||
@ -77,8 +87,11 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
|
||||
|
||||
const telegrafBotProvider: Provider = {
|
||||
provide: telegrafBotName,
|
||||
useFactory: async (options: TelegrafModuleOptions) =>
|
||||
await createBotFactory(options),
|
||||
useFactory: async (options: TelegrafModuleOptions) => {
|
||||
const bot = await createBotFactory(options);
|
||||
allBotsMap.set(telegrafBotName, bot);
|
||||
return bot;
|
||||
},
|
||||
inject: [TELEGRAF_MODULE_OPTIONS],
|
||||
};
|
||||
|
||||
@ -91,11 +104,13 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
|
||||
telegrafStageProvider,
|
||||
telegrafBotNameProvider,
|
||||
telegrafBotProvider,
|
||||
telegrafAllBotsProvider,
|
||||
],
|
||||
exports: [
|
||||
telegrafStageProvider,
|
||||
telegrafBotNameProvider,
|
||||
telegrafBotProvider,
|
||||
telegrafAllBotsProvider,
|
||||
],
|
||||
};
|
||||
}
|
||||
|
@ -12,3 +12,5 @@ export const WIZARD_STEP_METADATA = 'WIZARD_STEP_METADATA';
|
||||
export const PARAM_ARGS_METADATA = ROUTE_ARGS_METADATA;
|
||||
|
||||
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;
|
Loading…
Reference in New Issue
Block a user