grammyjs / conversations

Conversational interfaces for grammY.
https://grammy.dev/plugins/conversations
MIT License
53 stars 17 forks source link

.wait & .waitFor Crashes after conversation ends. #70

Closed hanahaneull closed 1 year ago

hanahaneull commented 1 year ago

Using .wait() and .waitFor() will crash the app after the conversation ended.

async function askUser(conversation, ctx) {
  await conversation.wait();
  await ctx.reply('Crashes after sending this reply')
}

Logs

/x/node_modules/@grammyjs/conversations/out/conversation.js:372
                    if (session.conversation[id].length === 0) {
                                                 ^

TypeError: Cannot read properties of undefined (reading 'length')
    at /x/node_modules/@grammyjs/conversations/out/conversation.js:372:50
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /x/node_modules/grammy/out/composer.js:61:13

Node.js v19.0.0
KnorpelSenf commented 1 year ago

I cannot reproduce this. The below code works just fine.

type MyContext = Context & ConversationFlavor;
type MyConversation = Conversation<MyContext>;

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

bot.use(session({ initial: () => ({}) }));

bot.use(conversations());
async function convo(conversation: MyConversation, ctx: MyContext) {
    await conversation.wait();
    await ctx.reply("Does not crash?");
}

bot.use(createConversation(convo, "convo"));

bot.command("convo", (ctx) => ctx.conversation.enter("convo"));

bot.start();
hanahaneull commented 1 year ago

I just found the issue, seems like I forgor to use await on my next() middleware. Never thought the issue will be on middleware smh, since the code works without any wait() or waitFor().

KnorpelSenf commented 1 year ago

I wonder if we can somehow detect this problem and throw a more helpful error message than just crashing internally … forgetting to use await correctly is a common oversight and it's desirable if not everyone has to perform the same investigation as you.