A Nest framework to run your Discord bot.
// test.command.ts
@Command.Name('test')
@Command.Alias('t', 'tt')
export class TestCommand implements CommandHandler {
async handle(client: Client, args: string[], msg: Message): Promise<void> {
// do something
}
}
//test.123.command.ts
@Command.Name('123')
@Command.Parent(TestCommand)
export class Test123Command implements CommandHandler {
async handle(client: Client, args: string[], msg: Message): Promise<void> {
await msg.reply('123');
}
}
All commands and events have to be registered as providers
By default NestCord comes without a command handler.
To use the default handler you can just add NestcordCommandHandler
to your providers, along with your commands..
The prefix can be set using NestcordCommandHandler.prefix
.
@Event.Handler('ready')
export class TestEvent implements EventHandler {
constructor(@InjectClient() private readonly client: Client) {}
private logger: Logger = new Logger('ReadyLogger');
async handle(): Promise<void> {
this.logger.log(`Logged in as ${this.client.user.tag}`);
}
}
All commands and events have to be registered as providers
Import Nestcord somewhere in your modules
@Module({
imports: [
NestcordModule.forRoot('YOUR_BOT_TOKEN'),
],
})
export class AppModule {}
WARNING: Sharding is an experimental feature that is working by forking multiple instances of NestJS, use with caution!
// launcher.ts
NestFactory.createApplicationContext(LauncherModule, {
logger: ShardLogger.create(process.env.SHARD_ID),
});
This example is using a custom logger, to distinguish the logged messages.
// launcher.module.ts
@Module({
imports: [NestcordModule.forShard()],
providers: [],
})
export class LauncherModule {}
@Module({
providers: [/* Your Commands and Events */],
imports: [ShardcordModule.forRoot(
require.resolve('./launcher'),
{
token: 'YOUR_BOT_TOKEN',
totalShards: 4,
},
],
})
export class AppModule {}
To use the shard all commands/events have to be registered in the shard's context
- Author - LambdaXCF
- The one who can actually type - Sascha_T
NestCord is MIT licensed.