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

ctx.setCommands not recognized #62

Open
dominicpaul1911 opened this issue Feb 1, 2025 · 11 comments
Open

ctx.setCommands not recognized #62

dominicpaul1911 opened this issue Feb 1, 2025 · 11 comments
Labels
question Further information is requested

Comments

@dominicpaul1911
Copy link

dominicpaul1911 commented Feb 1, 2025

I am a noob to telegram bot development and grammy. I installed all of the necessary packages including Grammy and @grammyjs/commands. But I am running into below error, setMyCommands not found on ctx. Could you please help me.

Image

@KnorpelSenf
Copy link
Member

What's the error that you're seeing?

@KnorpelSenf KnorpelSenf added the question Further information is requested label Feb 1, 2025
@dominicpaul1911
Copy link
Author

What's the error that you're seeing?

In loggedOutCommands.command method, the ctx object I receive from callback, does not recognize setMyCommands

@KnorpelSenf
Copy link
Member

You need to include the actual error message in full.

Alternatively, consider posting your code. Right now, the only thing you gave away is an image of the code. This means that people would have to type out the entire source code you wrote if they wanted to reproduce the issue. That's tedious. Without reproducing the issue, it's very hard to help.

@KnightNiwrem
Copy link

I am a noob to telegram bot development and grammy. I installed all of the necessary packages including Grammy and @grammyjs/commands. But I am running into below error, setMyCommands not found on ctx. Could you please help me.

Image

CommandsFlavor is a transformative type flavor, so you should be doing CommandsFlavor<Context> instead.

@KnorpelSenf
Copy link
Member

Great catch, our documentation is wrong: https://grammy.dev/plugins/commands#context-shortcut

@dominicpaul1911
Copy link
Author

dominicpaul1911 commented Feb 2, 2025

I changed the code as suggested. Issue remains, setMyCommands method is not recognizable on ctx object

import { Bot, Context } from "grammy";
// import { Menu } from "@grammyjs/menu";
import { CommandGroup, CommandsFlavor, commands } from "@grammyjs/commands";

// TOKEN
const token = "";

// Use the flavor to create a custom context
type MyContext = CommandsFlavor<Context>;

// Use the new context to instantiate your bot
const bot = new Bot<MyContext>(token);

// Register the context shortcut
bot.use(commands());


const loggedOutCommands = new CommandGroup();
const loggedInCommands = new CommandGroup(); 

loggedOutCommands.command(
  "login",
  "Start your session with the bot",
  async (ctx) => {
    await ctx.setMyCommands(loggedInCommands);
    await ctx.reply("Welcome! Session started!");
  },
);

loggedInCommands.command(
  "logout",
  "End your session with the bot",
  async (ctx) => {
    await ctx.setMyCommands(loggedOutCommands);
    await ctx.reply("Goodbye :)");
  },
);

bot.use(loggedInCommands);
bot.use(loggedOutCommands);

// By default, users are not logged in,
// so you can set the logged out commands for everyone
await loggedOutCommands.setCommands(bot);

@KnightNiwrem
Copy link

KnightNiwrem commented Feb 2, 2025

I changed the code as suggested. Issue remains, setMyCommands method is not recognizable on ctx object

`import { Bot, Context } from "grammy"; // import { Menu } from "@grammyjs/menu"; import { CommandGroup, CommandsFlavor, commands } from "@grammyjs/commands";

// TOKEN const token = "";

// Use the flavor to create a custom context type MyContext = CommandsFlavor;

// Use the new context to instantiate your bot const bot = new Bot(token);

// Register the context shortcut bot.use(commands());

const loggedOutCommands = new CommandGroup(); const loggedInCommands = new CommandGroup();

loggedOutCommands.command( "login", "Start your session with the bot", async (ctx) => { await ctx.setMyCommands(loggedInCommands); await ctx.reply("Welcome! Session started!"); }, );

loggedInCommands.command( "logout", "End your session with the bot", async (ctx) => { await ctx.setMyCommands(loggedOutCommands); await ctx.reply("Goodbye :)"); }, );

bot.use(loggedInCommands); bot.use(loggedOutCommands);

// By default, users are not logged in, // so you can set the logged out commands for everyone await loggedOutCommands.setCommands(bot);`

I don't see the part where you "changed the code as suggested". For the current code shown, the error is completely expected.

I get the part of just doing type MyContext = CommandFlavor, taking advantage of the fact that CommandFlavor extends Context. But you have also changed new Bot<MyContext>() to new Bot(), so your MyContext type is right, but it's also just floating around doing nothing useful as it is not being used.

@carafelix
Copy link
Member

Addressed in:
grammyjs/website#1186
grammyjs/website@ec14c95

@KnightNiwrem KnightNiwrem reopened this Feb 3, 2025
@KnightNiwrem
Copy link

KnightNiwrem commented Feb 3, 2025

Thanks for the report @dominicpaul1911!

There are several issues going on here, but just to address your final issue, the documentations should have said new CommandGroup<MyContext>() instead. Check out the following screenshot that demonstrates the difference between the two:

Image

Do note that this doesn't actually fully resolve your type issues in your use case, where the commandGroups circular set each other. Check out the following comment for more details.

@KnightNiwrem
Copy link

@carafelix Currently the type of CommandsFlavor is interface CommandsFlavor<C extends Context = Context> extends Context {}.

Let us begin with this example provided by the issue author:

Image

Notice that ctx.setMyCommands exists as a property, but the type of CommandGroup expected is CommandGroup<Context>. This is because of the proposed documented type type MyContext = Context & CommandsFlavor<Context>, where the C in C extends Context is just the base Context. Hence, the method will expect CommandGroup<Context> rather than CommandGroup<MyContext>. Ironically, it is impossible to do new CommandGroup() instead of new CommandGroup<MyContext>(), because the type of Context needs to be extended so that ctx.setMyCommands exists.

@carafelix
Copy link
Member

Thanks for pointing it out @KnightNiwrem
grammyjs/website@c07981c
grammyjs/website@65307d3

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

No branches or pull requests

4 participants