diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 1219bc2a..d28a01eb 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -91,6 +91,7 @@ import { deleteStickerFromSet } from './methods/stickers/delete-sticker-from-set import { getInstalledStickers } from './methods/stickers/get-installed-stickers' import { getStickerSet } from './methods/stickers/get-sticker-set' import { moveStickerInSet } from './methods/stickers/move-sticker-in-set' +import { setStickerSetThumb } from './methods/stickers/set-sticker-set-thumb' import { _fetchUpdatesState, _handleUpdate, @@ -2128,6 +2129,27 @@ export interface TelegramClient extends BaseTelegramClient { | tl.TypeInputDocument, position: number ): Promise + /** + * Set sticker set thumbnail + * + * @param id Sticker set short name or a TL object with input sticker set + * @param thumb Sticker set thumbnail + * @param params + * @returns Modified sticker set + */ + setStickerSetThumb( + id: string | tl.TypeInputStickerSet, + thumb: InputFileLike | tl.TypeInputDocument, + params?: { + /** + * Upload progress callback + * + * @param uploaded Number of bytes uploaded + * @param total Total file size + */ + progressCallback?: (uploaded: number, total: number) => void + } + ): Promise /** * Base function for update handling. Replace or override this function * and implement your own update handler, and call this function @@ -2317,6 +2339,7 @@ export class TelegramClient extends BaseTelegramClient { getInstalledStickers = getInstalledStickers getStickerSet = getStickerSet moveStickerInSet = moveStickerInSet + setStickerSetThumb = setStickerSetThumb protected _fetchUpdatesState = _fetchUpdatesState protected _loadStorage = _loadStorage protected _saveStorage = _saveStorage diff --git a/packages/client/src/methods/files/normalize-file-to-document.ts b/packages/client/src/methods/files/normalize-file-to-document.ts index 03afa99f..6260853e 100644 --- a/packages/client/src/methods/files/normalize-file-to-document.ts +++ b/packages/client/src/methods/files/normalize-file-to-document.ts @@ -8,11 +8,15 @@ import { assertTypeIs } from '../../utils/type-assertion' */ export async function _normalizeFileToDocument( this: TelegramClient, - file: InputFileLike, + file: InputFileLike | tl.TypeInputDocument, params: { progressCallback?: (uploaded: number, total: number) => void }, ): Promise { + if (typeof file === 'object' && tl.isAnyInputDocument(file)) { + return file + } + const media = await this._normalizeInputMedia({ type: 'document', file, diff --git a/packages/client/src/methods/stickers/set-sticker-set-thumb.ts b/packages/client/src/methods/stickers/set-sticker-set-thumb.ts new file mode 100644 index 00000000..e2736456 --- /dev/null +++ b/packages/client/src/methods/stickers/set-sticker-set-thumb.ts @@ -0,0 +1,42 @@ +import { TelegramClient } from '../../client' +import { tl } from '@mtcute/tl' +import { InputFileLike, StickerSet } from '../../types' + +/** + * Set sticker set thumbnail + * + * @param id Sticker set short name or a TL object with input sticker set + * @param thumb Sticker set thumbnail + * @param params + * @returns Modified sticker set + * @internal + */ +export async function setStickerSetThumb( + this: TelegramClient, + id: string | tl.TypeInputStickerSet, + thumb: InputFileLike | tl.TypeInputDocument, + params?: { + /** + * Upload progress callback + * + * @param uploaded Number of bytes uploaded + * @param total Total file size + */ + progressCallback?: (uploaded: number, total: number) => void + }, +): Promise { + if (typeof id === 'string') { + id = { + _: 'inputStickerSetShortName', + shortName: id + } + } + + const res = await this.call({ + _: 'stickers.setStickerSetThumb', + stickerset: id, + thumb: await this._normalizeFileToDocument(thumb, params ?? {}) + }) + + return new StickerSet(this, res) +} diff --git a/packages/client/src/types/misc/sticker-set.ts b/packages/client/src/types/misc/sticker-set.ts index 2d86ba6f..bf89dd1c 100644 --- a/packages/client/src/types/misc/sticker-set.ts +++ b/packages/client/src/types/misc/sticker-set.ts @@ -211,29 +211,38 @@ export class StickerSet { return this.client.addStickerToSet(this.inputStickerSet, sticker) } + private _getInputDocument(idx: number): tl.TypeInputDocument { + if (!this.full) throw new MtCuteEmptyError() + + if (idx < 0) idx = this.full!.documents.length + idx + const doc = this.full!.documents[idx] as tl.RawDocument + + if (!doc) throw new RangeError(`Sticker set does not have sticker ${idx}`) + + return { + _: 'inputDocument', + id: doc.id, + accessHash: doc.accessHash, + fileReference: doc.fileReference + } + } + /** * Delete a sticker from this set. * * Only for bots, and the sticker set must * have been created by this bot. * + * Note that this method returns a new + * {@link StickerSet} object instead of modifying current. + * * @param sticker * Sticker File ID. In case this is a full sticker set object, * you can also pass index (even negative), and that sticker will be removed */ async deleteSticker(sticker: number | Parameters[0]): Promise { if (typeof sticker === 'number') { - if (!this.full) throw new MtCuteEmptyError() - - if (sticker < 0) sticker = this.full!.documents.length + sticker - const doc = this.full!.documents[sticker] as tl.RawDocument - - sticker = { - _: 'inputDocument', - id: doc.id, - accessHash: doc.accessHash, - fileReference: doc.fileReference - } + sticker = this._getInputDocument(sticker) } return this.client.deleteStickerFromSet(sticker) @@ -245,6 +254,9 @@ export class StickerSet { * Only for bots, and the sticker set must * have been created by this bot. * + * Note that this method returns a new + * {@link StickerSet} object instead of modifying current. + * * @param sticker * Sticker File ID. In case this is a full sticker set object, * you can also pass index (even negative), and that sticker will be removed @@ -252,21 +264,33 @@ export class StickerSet { */ async moveSticker(sticker: number | Parameters[0], position: number): Promise { if (typeof sticker === 'number') { - if (!this.full) throw new MtCuteEmptyError() - - if (sticker < 0) sticker = this.full!.documents.length + sticker - const doc = this.full!.documents[sticker] as tl.RawDocument - - sticker = { - _: 'inputDocument', - id: doc.id, - accessHash: doc.accessHash, - fileReference: doc.fileReference - } + sticker = this._getInputDocument(sticker) } return this.client.moveStickerInSet(sticker, position) } + + /** + * Set sticker set thumbnail. + * + * Only for bots, and the sticker set must + * have been created by this bot. + * + * Note that this method returns a new + * {@link StickerSet} object instead of modifying current. + * + * @param thumb + * Thumbnail file. In case this is a full sticker set object, + * you can also pass index (even negative), and that sticker + * will be used as a thumb + */ + async setThumb(thumb: number | Parameters[1]): Promise { + if (typeof thumb === 'number') { + thumb = this._getInputDocument(thumb) + } + + return this.client.setStickerSetThumb(this.inputStickerSet, thumb) + } } makeInspectable(StickerSet, ['isFull'])