grammyjs / commands

Work-in-progress plugin for managing commands with grammY.
https://grammy.dev
MIT License
12 stars 2 forks source link

bug: localization failing due to localized versions of setMyCommandsParams command property being an empty string #13

Closed carafelix closed 5 months ago

carafelix commented 6 months ago

With a sample bot using something in the lines:

const userCommands = new Commands<MyContext>();
bot.use(commands());

userCommands.command("start", "init bot", (c) => {
  c.reply("started");
}).localize("es", "iniciar", "Inicializa el bot")
  .addToScope(
    { type: "all_private_chats" },
    (ctx) => ctx.reply(`iniciado`),
  );

userCommands.command("end", "end", (c) => {
  c.reply("end");
}).localize("es", "fin", "finaliza")
  .addToScope(
    { type: "all_private_chats" },
    (ctx) => ctx.reply(`finalizado`),
  );

throws:

[
  {
    scope: { type: "chat", chat_id: <ID> },
    language_code: undefined,
    commands: [
      { command: "start", description: "init bot" },
      { command: "end", description: "end" }
    ]
  },
  {
    scope: { type: "chat", chat_id: <ID> },
    language_code: "es",
    commands: [
      { command: "", description: "Inicializa el bot" },
      { command: "", description: "finaliza" }
    ]
  }
]
Error in middleware while handling update 286148219 GrammyError: Call to 'setMyCommands' failed! (400: Bad Request: command must be non-empty)

Most likely due to probably this line inside the toObject utility: https://github.com/grammyjs/commands/blob/63bc0fa0e770e4c2e3955df3f99fb13e493607d6/src/command.ts#L306 Is there any particular reason why we are converting to RegExp the languages keys inside the Command._languages map? Seems unnecessary, since the regExp characteristics are not being used anywhere. I looked up the matchOnlyAtStart option and the fuzzySearch method and they don't depend on regExp usage neither