From a06c03eda958f710ecac30608a033d3f3ab246b7 Mon Sep 17 00:00:00 2001 From: teidesu Date: Sun, 11 Apr 2021 15:32:38 +0300 Subject: [PATCH] feat(client): setChatTitle and setChatDescription methods --- packages/client/src/client.ts | 27 +++++++++++++ .../src/methods/chats/set-chat-description.ts | 31 +++++++++++++++ .../src/methods/chats/set-chat-title.ts | 39 +++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 packages/client/src/methods/chats/set-chat-description.ts create mode 100644 packages/client/src/methods/chats/set-chat-title.ts diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 6728a78f..785ba23e 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -28,7 +28,9 @@ import { getChat } from './methods/chats/get-chat' import { getFullChat } from './methods/chats/get-full-chat' import { joinChat } from './methods/chats/join-chat' import { leaveChat } from './methods/chats/leave-chat' +import { setChatDescription } from './methods/chats/set-chat-description' import { setChatPhoto } from './methods/chats/set-chat-photo' +import { setChatTitle } from './methods/chats/set-chat-title' import { unarchiveChats } from './methods/chats/unarchive-chats' import { downloadAsBuffer } from './methods/files/download-buffer' import { downloadToFile } from './methods/files/download-file' @@ -524,6 +526,20 @@ export class TelegramClient extends BaseTelegramClient { leaveChat(chatId: InputPeerLike, clear?: boolean): Promise { return leaveChat.apply(this, arguments) } + /** + * Change chat description + * + * You must be an administrator and have the appropriate permissions. + * + * @param chatId Chat ID or username + * @param description New chat description, 0-255 characters + */ + setChatDescription( + chatId: InputPeerLike, + description: string + ): Promise { + return setChatDescription.apply(this, arguments) + } /** * Set a new chat photo or video. * @@ -544,6 +560,17 @@ export class TelegramClient extends BaseTelegramClient { ): Promise { return setChatPhoto.apply(this, arguments) } + /** + * Change chat title + * + * You must be an administrator and have the appropriate permissions. + * + * @param chatId Chat ID or username + * @param title New chat title, 1-255 characters + */ + setChatTitle(chatId: InputPeerLike, title: string): Promise { + return setChatTitle.apply(this, arguments) + } /** * Unarchive one or more chats * diff --git a/packages/client/src/methods/chats/set-chat-description.ts b/packages/client/src/methods/chats/set-chat-description.ts new file mode 100644 index 00000000..a937a77e --- /dev/null +++ b/packages/client/src/methods/chats/set-chat-description.ts @@ -0,0 +1,31 @@ +import { TelegramClient } from '../../client' +import { + InputPeerLike, + MtCuteInvalidPeerTypeError, +} from '../../types' +import { normalizeToInputPeer } from '../../utils/peer-utils' + +/** + * Change chat description + * + * You must be an administrator and have the appropriate permissions. + * + * @param chatId Chat ID or username + * @param description New chat description, 0-255 characters + * @internal + */ +export async function setChatDescription( + this: TelegramClient, + chatId: InputPeerLike, + description: string +): Promise { + const chat = normalizeToInputPeer(await this.resolvePeer(chatId)) + if (!(chat._ === 'inputPeerChat' || chat._ === 'inputPeerChannel')) + throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel') + + await this.call({ + _: 'messages.editChatAbout', + peer: chat, + about: description + }) +} diff --git a/packages/client/src/methods/chats/set-chat-title.ts b/packages/client/src/methods/chats/set-chat-title.ts new file mode 100644 index 00000000..7b768911 --- /dev/null +++ b/packages/client/src/methods/chats/set-chat-title.ts @@ -0,0 +1,39 @@ +import { TelegramClient } from '../../client' +import { + InputPeerLike, + MtCuteInvalidPeerTypeError, +} from '../../types' +import { normalizeToInputChannel, normalizeToInputPeer } from '../../utils/peer-utils' + +/** + * Change chat title + * + * You must be an administrator and have the appropriate permissions. + * + * @param chatId Chat ID or username + * @param title New chat title, 1-255 characters + * @internal + */ +export async function setChatTitle( + this: TelegramClient, + chatId: InputPeerLike, + title: string +): Promise { + const chat = normalizeToInputPeer(await this.resolvePeer(chatId)) + if (!(chat._ === 'inputPeerChat' || chat._ === 'inputPeerChannel')) + throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel') + + if (chat._ === 'inputPeerChat') { + await this.call({ + _: 'messages.editChatTitle', + chatId: chat.chatId, + title + }) + } else { + await this.call({ + _: 'channels.editTitle', + channel: normalizeToInputChannel(chat)!, + title + }) + } +}