Skip to content

Commit ed79741

Browse files
committed
refactor(send-menu): improve return type of edit functions
changing function headers would be breaking but changing void to something isnt breaking in my opinion bool -> true shouldnt be an issue either
1 parent 465d34b commit ed79741

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

source/send-menu.ts

+9-11
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export type SendMenuToChatFunction<Context> = (chatId: string | number, context:
2323
* Method which is able to edit a message in a chat into a menu.
2424
* Generated via `generateEditMessageIntoMenuFunction`.
2525
*/
26-
export type EditMessageIntoMenuFunction<Context> = (chatId: number | string, messageId: number, context: Context, extra?: Readonly<ExtraEditMessageText | ExtraEditMessageMedia>) => Promise<Message | boolean>
26+
export type EditMessageIntoMenuFunction<Context> = (chatId: number | string, messageId: number, context: Context, extra?: Readonly<ExtraEditMessageText | ExtraEditMessageMedia>) => Promise<Message | true>
2727

2828
/**
2929
* Reply a menu to a context as a new message
@@ -46,15 +46,14 @@ export async function replyMenuToContext<Context extends TelegrafContext>(menu:
4646
* @param path path of the menu
4747
* @param extra optional additional Telegraf Extra options
4848
*/
49-
export async function editMenuOnContext<Context extends TelegrafContext>(menu: MenuLike<Context>, context: Context, path: string, extra: Readonly<ExtraEditMessageText | ExtraEditMessageMedia> = {}): Promise<void> {
49+
export async function editMenuOnContext<Context extends TelegrafContext>(menu: MenuLike<Context>, context: Context, path: string, extra: Readonly<ExtraEditMessageText | ExtraEditMessageMedia> = {}): Promise<Message | boolean> {
5050
ensurePathMenu(path)
5151
const body = await menu.renderBody(context, path)
5252
const keyboard = await menu.renderKeyboard(context, path)
5353

5454
const message = context.callbackQuery?.message
5555
if (!message) {
56-
await replyRenderedMenuPartsToContext(body, keyboard, context, extra)
57-
return
56+
return replyRenderedMenuPartsToContext(body, keyboard, context, extra)
5857
}
5958

6059
if (isMediaBody(body)) {
@@ -66,28 +65,27 @@ export async function editMenuOnContext<Context extends TelegrafContext>(menu: M
6665
parse_mode: body.parse_mode
6766
}
6867

69-
await context.editMessageMedia(media, createEditMediaExtra(keyboard, extra))
68+
return context.editMessageMedia(media, createEditMediaExtra(keyboard, extra))
7069
.catch(catchMessageNotModified)
71-
return
7270
}
7371
} else if (isLocationBody(body) || isVenueBody(body)) {
7472
// Dont edit the message, just recreate it.
7573
} else if (isTextBody(body)) {
7674
const text = getBodyText(body)
7775
if ('text' in message) {
78-
await context.editMessageText(text, createTextExtra(body, keyboard, extra))
76+
return context.editMessageText(text, createTextExtra(body, keyboard, extra))
7977
.catch(catchMessageNotModified)
80-
return
8178
}
8279
} else {
8380
throw new TypeError('The body has to be a string or an object containing text or media. Check the telegraf-inline-menu Documentation.')
8481
}
8582

8683
// The current menu is incompatible: delete and reply new one
87-
await Promise.all([
84+
const [repliedMessage] = await Promise.all([
8885
replyRenderedMenuPartsToContext(body, keyboard, context, extra),
8986
deleteMenuFromContext(context)
9087
])
88+
return repliedMessage
9189
}
9290

9391
/**
@@ -118,10 +116,10 @@ export async function resendMenuToContext<Context extends TelegrafContext>(menu:
118116
return menuMessage
119117
}
120118

121-
function catchMessageNotModified(error: unknown): void {
119+
function catchMessageNotModified(error: unknown): false {
122120
if (error instanceof Error && error.message.includes('message is not modified')) {
123121
// ignore
124-
return
122+
return false
125123
}
126124

127125
throw error

0 commit comments

Comments
 (0)