Skip to main content

Async Configuration

When you need to pass module options asynchronously instead of statically, use the .forRootAsync() method. As with most dynamic modules, Nest provides several techniques to deal with async configuration.

One technique is to use a factory function:

src/app.module.ts
import { NecordModule } from 'necord';
import { Module } from '@nestjs/common';
import { IntentsBitField } from 'discord.js';

@Module({
imports: [
NecordModule.forRootAsync({
useFactory: () => ({
token: 'DISCORD_BOT_TOKEN',
intents: [
IntentsBitField.Guilds,
IntentsBitField.GuildMessages,
IntentsBitField.DirectMessages
]
})
})
]
})
export class AppModule {}

Like other factory providers, our factory function can be async and can inject dependencies through inject.

src/app.module.ts
import { NecordModule } from 'necord';
import { Module } from '@nestjs/common';
import { IntentsBitField } from 'discord.js';

@Module({
imports: [
NecordModule.forRootAsync({
imports: [ConfigModule.forFeature(necordModuleConfig)],
useFactory: async (configService: ConfigService) => ({
token: configService.get<string>('DISCORD_BOT_TOKEN'),
intents: [
IntentsBitField.Guilds,
IntentsBitField.GuildMessages,
IntentsBitField.DirectMessages
]
}),
inject: [ConfigService]
})
]
})
export class AppModule {}

Alternatively, you can configure the NecordModule using a class instead of a factory, as shown below:

src/app.module.ts
import { NecordModule } from 'necord';
import { Module } from '@nestjs/common';
import { IntentsBitField } from 'discord.js';

@Module({
imports: [
NecordModule.forRootAsync({
useClass: NecordConfigService
})
]
})
export class AppModule {}

The construction above instantiates NecordConfigService inside NecordModule, using it to create the required options object. Note that in this example, the NecordConfigService has to implement the NecordOptionsFactory interface, as shown below. The NecordModule will call the .createNecordOptions() method on the instantiated object of the supplied class.

src/discord-config.service.ts
import { Injectable } from '@nestjs/common';
import { NecordOptionsFactory, NecordModuleOptions } from 'necord';
import { IntentsBitField } from 'discord.js';

@Injectable()
class NecordConfigService {
createNecordOptions(): NecordModuleOptions {
return {
token: 'DISCORD_BOT_TOKEN',
intents: [
IntentsBitField.Guilds,
IntentsBitField.GuildMessages,
IntentsBitField.DirectMessages
]
};
}
}

If you want to reuse an existing options provider instead of creating a private copy inside the NecordModule, use the useExisting syntax.