feat(client): mustReply parameter

This commit is contained in:
teidesu 2021-07-06 02:36:20 +03:00
parent d5e595d7cf
commit afd6f54e54
4 changed files with 106 additions and 2 deletions

View file

@ -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.
*

View file

@ -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.

View file

@ -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++) {

View file

@ -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,