feat(client): setStickerSetThumb method

This commit is contained in:
teidesu 2021-05-06 12:11:09 +03:00
parent 09064bb084
commit b157b52ff6
4 changed files with 116 additions and 23 deletions

View file

@ -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<StickerSet>
/**
* 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<StickerSet>
/**
* 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

View file

@ -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<tl.TypeInputDocument> {
if (typeof file === 'object' && tl.isAnyInputDocument(file)) {
return file
}
const media = await this._normalizeInputMedia({
type: 'document',
file,

View file

@ -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<StickerSet> {
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)
}

View file

@ -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<TelegramClient['deleteStickerFromSet']>[0]): Promise<StickerSet> {
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<TelegramClient['moveStickerInSet']>[0], position: number): Promise<StickerSet> {
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<TelegramClient['setStickerSetThumb']>[1]): Promise<StickerSet> {
if (typeof thumb === 'number') {
thumb = this._getInputDocument(thumb)
}
return this.client.setStickerSetThumb(this.inputStickerSet, thumb)
}
}
makeInspectable(StickerSet, ['isFull'])