Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fill ctx.match and add ctx.commandMatch #57

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

roziscoding
Copy link
Contributor

@roziscoding roziscoding commented Dec 7, 2024

  • Allow users to get the "arguments" of a command using ctx.match
  • Expose result of RegExp match through ctx.commandMatch

Example usage:

type MyContext = Context & CommandsFlavor;

const bot = new Bot<MyContext>("TOKEN");

const commands = new CommandGroup<MyContext>();

commands.command(/start ?(?<name>.*)?/, "a", async (ctx) => {
  console.log(ctx.commandMatch);
  // { command: /start ?(?<name>.*)?/, rest: 'name', match: ['start name', 'name'] }

  return ctx.reply(`Hello`);
});

bot.use(commands);

bot.start({
  onStart: () => {
    console.log("Bot started");
  },
});

Closes #58

@roziscoding roziscoding self-assigned this Dec 7, 2024
@roziscoding roziscoding requested a review from carafelix December 7, 2024 16:08
Copy link

@polRk polRk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, we should add fuzz tests

src/command.ts Outdated
const commands = ctx.msg.text.split(prefix).map((text) => ({ text }));

for (const { text } of commands) {
const [command, username] = text.split("@");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not good. for example /send_email user@mail

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This made me completely rewrite the command matching logic lol
But it works now, plus I've added tests for the new findMatchingCommand method, including one with a @ in the command.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's good! Thank you!

@roziscoding
Copy link
Contributor Author

I think, we should add fuzz tests

I don't really have the time for that right now, but I'd gladly review a new PQ :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to get command parameter
2 participants