IlyaSemenov / grammy-scenes

Nested named scenes for grammY
MIT License
28 stars 0 forks source link

how to use scenes for two composers? #14

Open sergej-js opened 1 year ago

sergej-js commented 1 year ago

image User scenes(userComposer) image Admin scenes(adminComposer) image If I use the admin scene and the user scene at the same time, then the user scene works without problems, but when I enter the admin scene, I get the following error image The error comes out after I entered the scene and entered some kind of message

IlyaSemenov commented 1 year ago

When you call bot.use(scenes.manager()) it injects ctx.scenes. So when you call it the second time with user scenes manager, it overwrites the previously injected admin scenes manager, and admin scenes are lost.

What are you trying to achieve ultimately? Can you describe your use case?

sergej-js commented 1 year ago

When you call bot.use(scenes.manager()) it injects ctx.scenes. So when you call it the second time with user scenes manager, it overwrites the previously injected admin scenes manager, and admin scenes are lost.

What are you trying to achieve ultimately? Can you describe your use case?

I have two composer Admin and user. And each of them has a filter, I want the scenes for administrators to work only for them, and for users only their scenes work.

sergej-js commented 1 year ago

When you call bot.use(scenes.manager()) it injects ctx.scenes. So when you call it the second time with user scenes manager, it overwrites the previously injected admin scenes manager, and admin scenes are lost.

What are you trying to achieve ultimately? Can you describe your use case?

I know that you can specify your own filter for each scene, but this does not suit me, because there can be a lot of scenes

sergej-js commented 1 year ago

When you call bot.use(scenes.manager()) it injects ctx.scenes. So when you call it the second time with user scenes manager, it overwrites the previously injected admin scenes manager, and admin scenes are lost.

What are you trying to achieve ultimately? Can you describe your use case?

I just want the scenes to work in different composers, but be available from a common ctx.scene

IlyaSemenov commented 1 year ago

That is not currently possible, I need to think of a good way to separate/filter scenes.

I think at the time being you can keep them together. Besides scenes, you probably have commands that actually start them, and there you can filter. So if users can never start admin scenes in the first place, then scenes can actually live together under the same ScenesComposer.

I agree that it's better to have a way separate them more reliably (which will help when e.g. admin is demoted to normal user while in the scene).

sergej-js commented 1 year ago

That is not currently possible, I need to think of a good way to separate/filter scenes.

I think at the time being you can keep them together. Besides scenes, you probably have commands that actually start them, and there you can filter. So if users can never start admin scenes in the first place, then scenes can actually live together under the same ScenesComposer.

I agree that it's better to have a way separate them more reliably (which will help when e.g. admin is demoted to normal user while in the scene).

Okay, but I hope you can solve this problem

IlyaSemenov commented 1 year ago

On second thought, if admin & user scenes are unrelated, it should be possible right now, with something like:

import { Bot } from "grammy"
import { Scene, ScenesComposer } from "grammy-scenes"

import { BotContext } from "./context"

const bot = new Bot<BotContext>("token")

const user1 = new Scene<BotContext>("user1")
const user2 = new Scene<BotContext>("user2")
const userScenes = new ScenesComposer(user1, user2)

const admin1 = new Scene<BotContext>("admin1")
const admin2 = new Scene<BotContext>("admin2")
const adminScenes = new ScenesComposer(admin1, admin2)

const isAdmin = () => false
const isUser = () => true

bot
  .filter(isAdmin)
  .use(adminScenes.manager())
  .command("start", (ctx) => ctx.scenes.enter("admin1"))
  .use(adminScenes)

bot
  .filter(isUser)
  .use(userScenes.manager())
  .command("start", (ctx) => ctx.scenes.enter("user1"))
  .use(userScenes)
sergej-js commented 1 year ago

On second though, if admin & user scenes are unrelated, it should be possible right now, with something like:

Thank you!I will try