Skip to content

Commit e1aad18

Browse files
committed
chore: add .on to scenes docs
1 parent 1ca97f8 commit e1aad18

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

docs/plugins/official/scenes.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,51 @@ const testScene = new Scene("test")
100100
});
101101
```
102102

103+
## on
104+
105+
This method allows you to register event handlers for a scene.
106+
107+
```ts
108+
const guessRandomNumberScene = new Scene("guess-random-number")
109+
.params<{ randomNumber: number }>()
110+
.on("message", async (context, next) => {
111+
return await Promise.all([context.delete(), next()]);
112+
})
113+
.step(["message", "callback_query"], async (context) => {
114+
if (context.scene.step.firstTime)
115+
return context.send("Try to guess a number from 1 to 10");
116+
117+
const number = Number(context.text);
118+
119+
if (
120+
Number.isNaN(number) ||
121+
number !== context.scene.params.randomNumber
122+
)
123+
return; // The handler above will delete the user's message
124+
125+
return Promise.all([
126+
context.send(
127+
format(
128+
`Congratulations! You guessed the number ${bold(
129+
context.scene.params.randomNumber
130+
)}!`
131+
)
132+
),
133+
context.scene.exit(),
134+
]);
135+
});
136+
```
137+
138+
Keep in mind that a handler is registered only for all subsequent steps (or .on handlers) after it is declared.
139+
140+
```ts
141+
new Scene("test")
142+
.on(...) // Called for all steps
143+
.step(...)
144+
.on(...) // Called only after the 2nd step is reached
145+
.step(...)
146+
```
147+
103148
## Storage usage
104149

105150
```ts

docs/ru/plugins/official/scenes.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,51 @@ const bot = new Bot(process.env.TOKEN as string)
120120

121121
[Подробнее о хранилищах](/ru/storages/)
122122

123+
### on
124+
125+
Этот метод позволяет зарегистрировать обработчик событий для сцены.
126+
127+
```ts
128+
const guessRandomNumberScene = new Scene("guess-random-number")
129+
.params<{ randomNumber: number }>()
130+
.on("message", (context, next) => {
131+
return await Promise.all([context.delete(), next()]);
132+
})
133+
.step(["message", "callback_query"], async (context) => {
134+
if (context.scene.step.firstTime)
135+
return context.send("Попробуй угадать число от 1 до 10");
136+
137+
const number = Number(context.text);
138+
139+
if (
140+
Number.isNaN(number) ||
141+
number !== context.scene.params.randomNumber
142+
)
143+
return; // Обработчик выше удалит отправленное пользователем сообщение
144+
145+
return Promise.all([
146+
context.send(
147+
format(
148+
`Поздравляю! Ты угадал число ${bold(
149+
context.scene.params.randomNumber
150+
)}!`
151+
)
152+
),
153+
context.scene.exit(),
154+
]);
155+
});
156+
```
157+
158+
Стоит помнить, что обработчик регистрируется только для всех идущих после него обработчиков (шагов или тех же `.on`).
159+
160+
```ts
161+
new Scene("test")
162+
.on(...) // Вызывается для всех шагов
163+
.step(...)
164+
.on(...) // Начинает вызываться только после достижения 2 шага
165+
.step(...)
166+
```
167+
123168
## Контекст сцены
124169

125170
<!-- Контекст сцены содержит в себе все данные . -->
@@ -132,6 +177,7 @@ const bot = new Bot(process.env.TOKEN as string)
132177
import { Scene } from "@gramio/scenes";
133178

134179
const testScene = new Scene("test")
180+
135181
.step("message", async (context) => {
136182
if (context.scene.step.firstTime)
137183
return context.send("Первое сообщение");

0 commit comments

Comments
 (0)