Skip to content

Commit

Permalink
feat: ctx.scenes.abort
Browse files Browse the repository at this point in the history
see #6
  • Loading branch information
IlyaSemenov committed May 24, 2022
1 parent 7ce0ea2 commit 718c4a9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`:
Expand Down
5 changes: 5 additions & 0 deletions src/scenes_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 718c4a9

Please sign in to comment.