npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@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

  1. Create CommandView instances and describe their options using decorators.
  2. Create a BotcmdContainer and add the created commands there.
  3. Add processBotcmd as middleware for your bot.

💡

  1. CommandView - is a class that contains information about commands and subcommands. It can be perceived as a Controller from NestJS.
  2. 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}`,
    });
  }
}