Skip to content

Commit 2eea95e

Browse files
committed
chore: update
1 parent e1aad18 commit 2eea95e

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

docs/plugins/official/scenes.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,18 @@ This method allows you to register event handlers for a scene.
108108
const guessRandomNumberScene = new Scene("guess-random-number")
109109
.params<{ randomNumber: number }>()
110110
.on("message", async (context, next) => {
111+
// This check is needed so the handler does not trigger on firstTime because context will be the same as previous step
112+
if (context.scene.step.firstTime) return next();
113+
111114
return await Promise.all([context.delete(), next()]);
112115
})
113116
.step(["message", "callback_query"], async (context) => {
114117
if (context.scene.step.firstTime)
115118
return context.send("Try to guess a number from 1 to 10");
116119

120+
if (!context.is("message"))
121+
return context.answer("Please write a message with a number");
122+
117123
const number = Number(context.text);
118124

119125
if (

docs/ru/plugins/official/scenes.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,19 @@ const bot = new Bot(process.env.TOKEN as string)
127127
```ts
128128
const guessRandomNumberScene = new Scene("guess-random-number")
129129
.params<{ randomNumber: number }>()
130-
.on("message", (context, next) => {
130+
.on("message", async (context, next) => {
131+
// Это условие нужно, чтобы обработчик не срабатывал при firstTime так как context будет одинаковым с предыдущим шагом
132+
if (context.scene.step.firstTime) return next();
133+
131134
return await Promise.all([context.delete(), next()]);
132135
})
133136
.step(["message", "callback_query"], async (context) => {
134137
if (context.scene.step.firstTime)
135138
return context.send("Попробуй угадать число от 1 до 10");
136139

140+
if (!context.is("message"))
141+
return context.answer("Пожалуйста, отправьте число сообщением");
142+
137143
const number = Number(context.text);
138144

139145
if (
@@ -155,13 +161,13 @@ const guessRandomNumberScene = new Scene("guess-random-number")
155161
});
156162
```
157163

158-
Стоит помнить, что обработчик регистрируется только для всех идущих после него обработчиков (шагов или тех же `.on`).
164+
Обратите внимание: обработчик применяется только ко всем шагам, объявленным после него (или к следующим .on), а не к предыдущим.
159165

160166
```ts
161167
new Scene("test")
162168
.on(...) // Вызывается для всех шагов
163169
.step(...)
164-
.on(...) // Начинает вызываться только после достижения 2 шага
170+
.on(...) // Вызывается только после достижения второго шага
165171
.step(...)
166172
```
167173

0 commit comments

Comments
 (0)