feat(client): setChatDefaultPermissions and setSlowMode methods

This commit is contained in:
teidesu 2021-04-11 16:11:28 +03:00
parent a06c03eda9
commit e3ff2b9041
6 changed files with 143 additions and 0 deletions

View file

@ -28,9 +28,11 @@ 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 { setChatDefaultPermissions } from './methods/chats/set-chat-default-permissions'
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 { setSlowMode } from './methods/chats/set-slow-mode'
import { unarchiveChats } from './methods/chats/unarchive-chats'
import { downloadAsBuffer } from './methods/files/download-buffer'
import { downloadToFile } from './methods/files/download-file'
@ -77,6 +79,7 @@ import {
ChatMember,
ChatPreview,
FileDownloadParameters,
InputChatPermissions,
InputFileLike,
InputMediaLike,
InputPeerLike,
@ -526,6 +529,33 @@ export class TelegramClient extends BaseTelegramClient {
leaveChat(chatId: InputPeerLike, clear?: boolean): Promise<void> {
return leaveChat.apply(this, arguments)
}
/**
* Change default chat permissions for all members.
*
* You must be an administrator in the chat and have appropriate permissions.
*
* @param chatId Chat ID or username
* @param permissions New default chat permissions
* @example
* ```typescript
* // Completely restrict chat
* await tg.setDefaultChatPermissions('somechat', {})
*
* // Chat members can only send text, media, stickers and GIFs
* await tg.setDefaultChatPermissions('somechat', {
* canSendMessages: true,
* canSendMedia: true,
* canSendStickers: true,
* canSendGifs: true,
* })
* ```
*/
setChatDefaultPermissions(
chatId: InputPeerLike,
permissions: InputChatPermissions
): Promise<Chat> {
return setChatDefaultPermissions.apply(this, arguments)
}
/**
* Change chat description
*
@ -571,6 +601,19 @@ export class TelegramClient extends BaseTelegramClient {
setChatTitle(chatId: InputPeerLike, title: string): Promise<void> {
return setChatTitle.apply(this, arguments)
}
/**
* Set supergroup's slow mode interval.
*
* @param chatId Chat ID or username
* @param seconds
* (default: `0`)
* Slow mode interval in seconds.
* Users will be able to send a message only once per this interval.
* Valid values are: `0 (off), 10, 30, 60 (1m), 300 (5m), 900 (15m) or 3600 (1h)`
*/
setSlowMode(chatId: InputPeerLike, seconds?: number): Promise<void> {
return setSlowMode.apply(this, arguments)
}
/**
* Unarchive one or more chats
*

View file

@ -12,6 +12,7 @@ import {
Chat,
ChatPreview,
ChatMember,
InputChatPermissions,
TermsOfService,
SentCode,
MaybeDynamic,

View file

@ -0,0 +1,63 @@
import { TelegramClient } from '../../client'
import { Chat, InputChatPermissions, InputPeerLike, MtCuteTypeAssertionError } from '../../types'
import { normalizeToInputPeer } from '../../utils/peer-utils'
/**
* Change default chat permissions for all members.
*
* You must be an administrator in the chat and have appropriate permissions.
*
* @param chatId Chat ID or username
* @param permissions New default chat permissions
* @example
* ```typescript
* // Completely restrict chat
* await tg.setDefaultChatPermissions('somechat', {})
*
* // Chat members can only send text, media, stickers and GIFs
* await tg.setDefaultChatPermissions('somechat', {
* canSendMessages: true,
* canSendMedia: true,
* canSendStickers: true,
* canSendGifs: true,
* })
* ```
* @internal
*/
export async function setChatDefaultPermissions(
this: TelegramClient,
chatId: InputPeerLike,
permissions: InputChatPermissions
): Promise<Chat> {
const peer = normalizeToInputPeer(await this.resolvePeer(chatId))
const res = await this.call({
_: 'messages.editChatDefaultBannedRights',
peer,
bannedRights: {
_: 'chatBannedRights',
untilDate: 0,
sendMessages: !permissions.canSendMessages,
sendMedia: !permissions.canSendMedia,
sendStickers: !permissions.canSendStickers,
sendGifs: !permissions.canSendGifs,
sendGames: !permissions.canSendGames,
sendInline: !permissions.canUseInline,
embedLinks: !permissions.canAddWebPreviews,
sendPolls: !permissions.canSendPolls,
changeInfo: !permissions.canChangeInfo,
inviteUsers: !permissions.canInviteUsers,
pinMessages: !permissions.canPinMessages,
}
})
if (!(res._ === 'updates' || res._ === 'updatesCombined')) {
throw new MtCuteTypeAssertionError(
'joinChat, (@ channels.joinChannel)',
'updates | updatesCombined',
res._
)
}
return new Chat(this, res.chats[0])
}

View file

@ -0,0 +1,28 @@
import { TelegramClient } from '../../client'
import { InputPeerLike, MtCuteInvalidPeerTypeError } from '../../types'
import { normalizeToInputChannel } from '../../utils/peer-utils'
/**
* Set supergroup's slow mode interval.
*
* @param chatId Chat ID or username
* @param seconds
* Slow mode interval in seconds.
* Users will be able to send a message only once per this interval.
* Valid values are: `0 (off), 10, 30, 60 (1m), 300 (5m), 900 (15m) or 3600 (1h)`
* @internal
*/
export async function setSlowMode(
this: TelegramClient,
chatId: InputPeerLike,
seconds = 0
): Promise<void> {
const chat = normalizeToInputChannel(await this.resolvePeer(chatId))
if (!chat) throw new MtCuteInvalidPeerTypeError(chatId, 'channel')
await this.call({
_: 'channels.toggleSlowMode',
channel: chat,
seconds
})
}

View file

@ -127,3 +127,10 @@ export class ChatPermissions {
}
makeInspectable(ChatPermissions)
/**
* Chat permissions that are used as an input for API methods.
*/
export type InputChatPermissions = {
[k in Exclude<keyof ChatPermissions, '_bannedRights'>]?: boolean
}

View file

@ -3,6 +3,7 @@ import { tl } from '@mtcute/tl'
export * from './user'
export * from './chat'
export * from './chat-preview'
export { InputChatPermissions } from './chat-permissions'
export * from './chat-member'
/**