From 718c4a918519dd4fe9e34b463354b811f04a8973 Mon Sep 17 00:00:00 2001 From: Ilya Semenov Date: Tue, 24 May 2022 19:30:22 +0700 Subject: [PATCH] feat: ctx.scenes.abort see #6 --- README.md | 24 ++++++++++++++++++++++++ src/scenes_manager.ts | 5 +++++ 2 files changed, 29 insertions(+) diff --git a/README.md b/README.md index 6eea488..279c361 100644 --- a/README.md +++ b/README.md @@ -280,6 +280,30 @@ onSomeExternalEvent(({ chat_id, resume_token, payload }) => { bot.start() ``` +### Abort scenes + +If only part of your code uses scenes, you will possibly want to abort whatever scene is being executed on certain (or all) commands. + +You can do this with: + +```ts +bot.command("help", async (ctx) => { + ctx.scenes.abort() + await ctx.reply("Help text") +}) +``` + +or you may do this universally for all commands: + +```ts +bot.on("message:text", (ctx, next) => { + if (ctx.message.text.startsWith("/")) { + ctx.scenes.abort() + } + return next() +}) +``` + ### Run middleware before each step To run certain middleware before each step, use `scene.always()`: diff --git a/src/scenes_manager.ts b/src/scenes_manager.ts index 0261d68..e3d7aae 100644 --- a/src/scenes_manager.ts +++ b/src/scenes_manager.ts @@ -26,6 +26,11 @@ export class ScenesManager< await this._run_stack([{ scene: sceneId, pos: 0 }], { arg }) } + /** Abort scenes execution */ + async abort() { + delete this.ctx.session.scenes + } + /** Resume scene if it's still on the same step */ async resume(token: string, arg?: unknown) { const stack = this.ctx.session.scenes?.stack