@henta/botcmd
v0.2.2
Published
Command parser
Downloads
58
Readme
@henta/botcmd
Powerful command parser middleware for your bots.
📌 Is part of HENTA Framework
Usage
- Create
CommandView
instances and describe their options using decorators. - Create a
BotcmdContainer
and add the created commands there. - Add
processBotcmd
as middleware for your bot.
💡
- CommandView - is a class that contains information about commands and subcommands. It can be perceived as a Controller from NestJS.
- BootcmdContainer - is a container that stores a list of commands. Usually it can be used to separate commands and connect them only under certain conditions (for example, some scenemanager), but in most cases only 1 instance can be used.
// test.view.ts
import Context from '@app/interfaces/context'; // your context interface extends BotcmdContext
@BotcmdView('test')
export default class TestView extends CommandView {
@BotcmdCommand()
public async handler(ctx: Context) {
await ctx.answer({
text: 'I\'m fine'
});
}
}
// index.ts
const mainCommands: CommandView[] = [
new TestView()
];
const botcmdContainer = new BotcmdContainer();
botcmdContainer.applyViews(mainCommands);
const hentaBot = initHentaBot(); // your initialization logic
hentaBot.setMiddleware([
// ...some middlewares before command
(ctx, next) =>
processBotcmd(ctx, next, {
containers: [botcmdContainer],
}),
// ...some middlewares after command
]);
await hentaBot.run();
You can use a @henta/input to parse input arguments, attachments and custom requests.
// get-link.view.ts
import Context from '@app/interfaces/context'; // your context interface extends BotcmdContext
@BotcmdView('getlink')
export default class GetLinkView extends CommandView {
@BotcmdCommand()
public async handler(
ctx: Context,
@AttachmentRequest('photo', (item) => item.getUrl())
url: string
) {
await ctx.answer({
text: `Link: ${url}`
});
}
}
// index.ts
const botcmdMiddleware = compose([
(ctx: Context, next) => requestInputArgsMiddleware(ctx, next),
]);
hentaBot.setMiddleware([
// ...some middlewares before command
(ctx, next) =>
processBotcmd(ctx, next, {
containers: [botcmdContainer],
middlewares: botcmdMiddleware
}),
// ...some middlewares after command
]);
Command metadata
You can define your own command metadata using decorator @SetMetadata(key, value)
.
Sample:
import { BotcmdCommand, BotcmdView, CommandView } from '@henta/botcmd';
@SetMetadata('botcmd:custom:hello', 'world')
@BotcmdView('тест', 'test', 'tost')
export default class TestView extends CommandView {
@BotcmdCommand()
public async handler(ctx) {
const metadataValue = getCommandMetadata('botcmd:custom:hello', ctx.botcmdData.command);
await ctx.answer({
text: `Hello ${metadataValue}`,
});
}
}