diff --git a/examples/app/src/app.service.ts b/examples/app/src/app.service.ts index c560825..8048834 100755 --- a/examples/app/src/app.service.ts +++ b/examples/app/src/app.service.ts @@ -1,7 +1,7 @@ import { Injectable } from '@nestjs/common'; import { TextCommandContext, Context, ContextOf } from '../../../packages/core/src/context'; import { Telegraf } from 'telegraf'; -import { TextCommand } from '../../../packages/core'; +import { Arguments, TextCommand } from '../../../packages/core'; @Injectable() export class AppService { @@ -16,7 +16,7 @@ export class AppService { name: 'start', description: 'Displays this help message.', }) - onStart(@Context() [ctx]: TextCommandContext) { - ctx.reply('Start message triggered'); + onStart(@Context() [ctx]: TextCommandContext, @Arguments() args: string[]) { + ctx.reply(`Start message triggered\nArgs: ${args.join(', ') || 'none'}`); } } diff --git a/packages/core/src/text-commands/text-commands.module.ts b/packages/core/src/text-commands/text-commands.module.ts index b2aa29f..e8501ad 100644 --- a/packages/core/src/text-commands/text-commands.module.ts +++ b/packages/core/src/text-commands/text-commands.module.ts @@ -22,22 +22,14 @@ export class TextCommandsModule implements OnModuleInit, OnApplicationBootstrap } public onApplicationBootstrap() { - this.client.on('message', async (ctx) => { - if (!ctx.message) { - return; - } - const content = ctx.message['text'].trim().toLowerCase(); - if (content.charAt(0) !== '/') { - return; - } - - const args = content.split(/ +/g); - const cmd = args.shift()?.substring(1); - if (!cmd) { - return; - } - - return this.textCommandsService.get(cmd)?.execute([ctx]); + this.textCommandsService.cache.forEach((command) => { + const commandName = command.getName(); + this.client.command(commandName, async (ctx) => { + const cmd = this.textCommandsService.get(commandName); + if (cmd) { + await cmd.execute([ctx]); + } + }); }); } }