feat(client): createChannel, deleteChannel, deleteSupergroup methods
intentionally no bound method for Chat because it would be way too easy to accidentally delete some chat.
This commit is contained in:
parent
c0103441d3
commit
02975b3793
3 changed files with 85 additions and 1 deletions
|
@ -15,6 +15,8 @@ import { signUp } from './methods/auth/sign-up'
|
|||
import { start } from './methods/auth/start'
|
||||
import { addChatMembers } from './methods/chats/add-chat-members'
|
||||
import { archiveChats } from './methods/chats/archive-chats'
|
||||
import { createChannel } from './methods/chats/create-channel'
|
||||
import { deleteChannel } from './methods/chats/delete-channel'
|
||||
import { getChatPreview } from './methods/chats/get-chat-preview'
|
||||
import { getChat } from './methods/chats/get-chat'
|
||||
import { getFullChat } from './methods/chats/get-full-chat'
|
||||
|
@ -333,7 +335,8 @@ export class TelegramClient extends BaseTelegramClient {
|
|||
*
|
||||
* @param chatId ID of the chat or its username
|
||||
* @param users ID(s) of the users, their username(s) or phone(s).
|
||||
* @param forwardCount(default: `100`)
|
||||
* @param forwardCount
|
||||
* (default: `100`)
|
||||
* Number of old messages to be forwarded (0-100).
|
||||
* Only applicable to legacy groups, ignored for supergroups and channels
|
||||
*/
|
||||
|
@ -352,6 +355,34 @@ export class TelegramClient extends BaseTelegramClient {
|
|||
archiveChats(chats: MaybeArray<InputPeerLike>): Promise<void> {
|
||||
return archiveChats.apply(this, arguments)
|
||||
}
|
||||
/**
|
||||
* Create a new broadcast channel
|
||||
*
|
||||
* @param title Channel title
|
||||
* @param description (default: `''`) Channel description
|
||||
* @returns Newly created channel
|
||||
*/
|
||||
createChannel(title: string, description?: string): Promise<Chat> {
|
||||
return createChannel.apply(this, arguments)
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a channel or a supergroup
|
||||
*
|
||||
* @param chatId Chat ID or username
|
||||
*/
|
||||
deleteChannel(chatId: InputPeerLike): Promise<void> {
|
||||
return deleteChannel.apply(this, arguments)
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a channel or a supergroup
|
||||
*
|
||||
* @param chatId Chat ID or username
|
||||
*/
|
||||
deleteSupergroup(chatId: InputPeerLike): Promise<void> {
|
||||
return deleteChannel.apply(this, arguments)
|
||||
}
|
||||
/**
|
||||
* Get preview information about a private chat.
|
||||
*
|
||||
|
|
33
packages/client/src/methods/chats/create-channel.ts
Normal file
33
packages/client/src/methods/chats/create-channel.ts
Normal file
|
@ -0,0 +1,33 @@
|
|||
import { TelegramClient } from '../../client'
|
||||
import { Chat, MtCuteTypeAssertionError } from '../../types'
|
||||
|
||||
/**
|
||||
* Create a new broadcast channel
|
||||
*
|
||||
* @param title Channel title
|
||||
* @param description Channel description
|
||||
* @returns Newly created channel
|
||||
* @internal
|
||||
*/
|
||||
export async function createChannel(
|
||||
this: TelegramClient,
|
||||
title: string,
|
||||
description = ''
|
||||
): Promise<Chat> {
|
||||
const res = await this.call({
|
||||
_: 'channels.createChannel',
|
||||
title,
|
||||
about: description,
|
||||
broadcast: true,
|
||||
})
|
||||
|
||||
if (!(res._ === 'updates' || res._ === 'updatesCombined')) {
|
||||
throw new MtCuteTypeAssertionError(
|
||||
'createChannel (@ channels.createChannel)',
|
||||
'updates | updatesCombined',
|
||||
res._
|
||||
)
|
||||
}
|
||||
|
||||
return new Chat(this, res.chats[0])
|
||||
}
|
20
packages/client/src/methods/chats/delete-channel.ts
Normal file
20
packages/client/src/methods/chats/delete-channel.ts
Normal file
|
@ -0,0 +1,20 @@
|
|||
import { InputPeerLike, MtCuteInvalidPeerTypeError } from '../../types'
|
||||
import { TelegramClient } from '../../client'
|
||||
import { normalizeToInputChannel } from '../../utils/peer-utils'
|
||||
|
||||
// @alias=deleteSupergroup
|
||||
/**
|
||||
* Delete a channel or a supergroup
|
||||
*
|
||||
* @param chatId Chat ID or username
|
||||
* @internal
|
||||
*/
|
||||
export async function deleteChannel(this: TelegramClient, chatId: InputPeerLike): Promise<void> {
|
||||
const peer = normalizeToInputChannel(await this.resolvePeer(chatId))
|
||||
if (!peer) throw new MtCuteInvalidPeerTypeError(chatId, 'channel')
|
||||
|
||||
await this.call({
|
||||
_: 'channels.deleteChannel',
|
||||
channel: peer
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue