diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index e0519d3c..68a57be8 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -2399,6 +2399,19 @@ export interface TelegramClient extends BaseTelegramClient { */ replyTo?: number | Message + /** + * Whether to throw an error if {@link replyTo} + * message does not exist. + * + * If that message was not found, `NotFoundError` is thrown, + * with `text` set to `MESSAGE_NOT_FOUND`. + * + * Incurs an additional request, so only use when really needed. + * + * Defaults to `false` + */ + mustReply?: boolean + /** * List of formatting entities to use instead of parsing via a * parse mode. @@ -2441,6 +2454,19 @@ export interface TelegramClient extends BaseTelegramClient { */ replyTo?: number | Message + /** + * Whether to throw an error if {@link replyTo} + * message does not exist. + * + * If that message was not found, `NotFoundError` is thrown, + * with `text` set to `MESSAGE_NOT_FOUND`. + * + * Incurs an additional request, so only use when really needed. + * + * Defaults to `false` + */ + mustReply?: boolean + /** * Message to comment to. Either a message object or message ID. * @@ -2535,6 +2561,19 @@ export interface TelegramClient extends BaseTelegramClient { */ replyTo?: number | Message + /** + * Whether to throw an error if {@link replyTo} + * message does not exist. + * + * If that message was not found, `NotFoundError` is thrown, + * with `text` set to `MESSAGE_NOT_FOUND`. + * + * Incurs an additional request, so only use when really needed. + * + * Defaults to `false` + */ + mustReply?: boolean + /** * Message to comment to. Either a message object or message ID. * diff --git a/packages/client/src/methods/messages/send-copy.ts b/packages/client/src/methods/messages/send-copy.ts index f45f4cbc..42fd2668 100644 --- a/packages/client/src/methods/messages/send-copy.ts +++ b/packages/client/src/methods/messages/send-copy.ts @@ -60,6 +60,19 @@ export async function sendCopy( */ replyTo?: number | Message + /** + * Whether to throw an error if {@link replyTo} + * message does not exist. + * + * If that message was not found, `NotFoundError` is thrown, + * with `text` set to `MESSAGE_NOT_FOUND`. + * + * Incurs an additional request, so only use when really needed. + * + * Defaults to `false` + */ + mustReply?: boolean + /** * List of formatting entities to use instead of parsing via a * parse mode. diff --git a/packages/client/src/methods/messages/send-media-group.ts b/packages/client/src/methods/messages/send-media-group.ts index 9896cd0d..b5f9cdaf 100644 --- a/packages/client/src/methods/messages/send-media-group.ts +++ b/packages/client/src/methods/messages/send-media-group.ts @@ -3,7 +3,7 @@ import { BotKeyboard, InputFileLike, InputMediaLike, InputPeerLike, - Message, + Message, MtCuteArgumentError, ReplyMarkup, } from '../../types' import { @@ -14,6 +14,7 @@ import { import { tl } from '@mtcute/tl' import { assertIsUpdatesGroup } from '../../utils/updates-utils' import { createUsersChatsIndex } from '../../utils/peer-utils' +import { MessageNotFoundError } from '@mtcute/tl/errors' /** * Send a group of media. @@ -37,6 +38,19 @@ export async function sendMediaGroup( */ replyTo?: number | Message + /** + * Whether to throw an error if {@link replyTo} + * message does not exist. + * + * If that message was not found, `NotFoundError` is thrown, + * with `text` set to `MESSAGE_NOT_FOUND`. + * + * Incurs an additional request, so only use when really needed. + * + * Defaults to `false` + */ + mustReply?: boolean + /** * Message to comment to. Either a message object or message ID. * @@ -108,6 +122,18 @@ export async function sendMediaGroup( ) } + if (params.mustReply) { + if (!replyTo) + throw new MtCuteArgumentError( + 'mustReply used, but replyTo was not passed' + ) + + const msg = await this.getMessages(peer, replyTo) + + if (!msg) + throw new MessageNotFoundError() + } + const multiMedia: tl.RawInputSingleMedia[] = [] for (let i = 0; i < medias.length; i++) { diff --git a/packages/client/src/methods/messages/send-media.ts b/packages/client/src/methods/messages/send-media.ts index e06b43f8..76e12e06 100644 --- a/packages/client/src/methods/messages/send-media.ts +++ b/packages/client/src/methods/messages/send-media.ts @@ -3,7 +3,7 @@ import { BotKeyboard, InputMediaLike, InputPeerLike, - Message, + Message, MtCuteArgumentError, ReplyMarkup, } from '../../types' import { @@ -12,6 +12,7 @@ import { randomUlong, } from '../../utils/misc-utils' import { tl } from '@mtcute/tl' +import { MessageNotFoundError } from '@mtcute/tl/errors' /** * Send a single media (a photo or a document-based media) @@ -51,6 +52,19 @@ export async function sendMedia( */ replyTo?: number | Message + /** + * Whether to throw an error if {@link replyTo} + * message does not exist. + * + * If that message was not found, `NotFoundError` is thrown, + * with `text` set to `MESSAGE_NOT_FOUND`. + * + * Incurs an additional request, so only use when really needed. + * + * Defaults to `false` + */ + mustReply?: boolean + /** * Message to comment to. Either a message object or message ID. * @@ -135,6 +149,18 @@ export async function sendMedia( ) } + if (params.mustReply) { + if (!replyTo) + throw new MtCuteArgumentError( + 'mustReply used, but replyTo was not passed' + ) + + const msg = await this.getMessages(peer, replyTo) + + if (!msg) + throw new MessageNotFoundError() + } + const res = await this.call({ _: 'messages.sendMedia', peer,