-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchats.ts
363 lines (332 loc) · 10.1 KB
/
chats.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import { Api, bigInt } from "./deps.ts";
import ClientHolder from "./client_holder.ts";
export interface AddChatMembersParams {
chatId: Api.TypeEntityLike;
userId: Api.TypeEntityLike | Api.TypeEntityLike[];
forwardLimit?: number;
}
export interface ArchiveChatsParams {
chatIds: Api.TypeEntityLike | Api.TypeEntityLike[];
}
export interface BanChatMemberParams {
chatId: Api.TypeEntityLike;
userId: Api.TypeEntityLike;
untilDate?: number;
}
export interface CreateChannelParams {
title: string;
about?: string;
}
export interface CreateGroupParams {
title: string;
users: Api.TypeEntityLike | Api.TypeEntityLike[];
}
export interface CreateSupergroupParams {
title: string;
about?: string;
}
export interface DeleteChannelParams {
chatId: Api.TypeEntityLike;
}
export interface DeleteChatPhotoParams {
chatId: Api.TypeEntityLike;
}
export interface DeleteSupergroupParams {
chatId: Api.TypeEntityLike;
}
export interface DeleteUserHistoryParams {
chatId: Api.TypeEntityLike;
userId: Api.TypeEntityLike;
}
export interface GetChatMemberParams {
chatId: Api.TypeEntityLike;
userId: Api.TypeEntityLike;
}
export interface PinChatMessageParams {
/** Unique identifier or username of the target chat. Can be number | string | bigint */
chatId: Api.TypeEntityLike;
/** Identifier of a message to pin. */
messageId: number;
/**
* Pass True, if it is not necessary to send a notification to all chat members about the new pinned message.
* Notifications are always disabled in channels.
*/
disableNotification?: boolean;
/**
* Pass True to pin the message for both sides (you and recipient).
* Applicable to private chats only. Defaults to False.
*/
bothSides?: boolean;
}
export interface PromoteChatMemberParams {
chatId: Api.TypeEntityLike;
userId: Api.TypeEntityLike;
isAnonymous?: boolean;
canManageChat?: boolean;
canChangeInfo?: boolean;
canPostMessages?: boolean;
canEditMessages?: boolean;
canDeleteMessages?: boolean;
canRestrictMembers?: boolean;
canInviteUsers?: boolean;
canPinMessages?: boolean;
canPromoteMembers?: boolean;
canManageCalls?: boolean;
rank?: string;
}
export interface UnbanChatMemberParmas {
chatId: Api.TypeEntityLike;
userId: Api.TypeEntityLike;
}
export interface UnpinChatMessageParams {
/** Unique identifier or username of the target chat. Can be number | string | bigint */
chatId: Api.TypeEntityLike;
/** Identifier of a message to unpin. */
messageId: number;
}
export default class Chats extends ClientHolder {
async addChatMembers({
chatId,
userId,
forwardLimit = 100,
}: AddChatMembersParams) {
const chatEntity = await this.client.getEntity(chatId);
if (!Array.isArray(userId)) {
userId = [userId];
}
if (chatEntity instanceof Api.Chat) {
for (const user of userId) {
await this.client.invoke(
new Api.messages.AddChatUser({
chatId: chatEntity.id,
userId: await this.client.getEntity(user),
fwdLimit: forwardLimit,
}),
);
}
} else {
await this.client.invoke(
new Api.channels.InviteToChannel({
channel: chatEntity,
users: await Promise.all(
userId.map(async (user) => await this.client.getEntity(user)),
),
}),
);
}
return true;
}
async archiveChats({ chatIds }: ArchiveChatsParams) {
if (!Array.isArray(chatIds)) {
chatIds = [chatIds];
}
const folderPeers = new Array<Api.InputFolderPeer>();
for (const chat of chatIds) {
folderPeers.push(
new Api.InputFolderPeer({
peer: await this.client.getInputEntity(chat),
folderId: 1,
}),
);
}
await this.client.invoke(new Api.folders.EditPeerFolders({ folderPeers }));
return true;
}
async banChatMember({ chatId, userId, untilDate = 0 }: BanChatMemberParams) {
const userEntity = await this.client.getEntity(userId);
const chatEntity = await this.client.getEntity(chatId);
if (chatEntity instanceof Api.Channel) {
await this.client.invoke(
new Api.channels.EditBanned({
channel: chatEntity,
participant: userEntity,
bannedRights: new Api.ChatBannedRights({
untilDate,
viewMessages: true,
sendMessages: true,
sendMedia: true,
sendStickers: true,
sendGifs: true,
sendGames: true,
sendInline: true,
embedLinks: true,
}),
}),
);
} else {
await this.client.invoke(
new Api.messages.DeleteChatUser({
chatId: bigInt(<number> chatId),
userId: userEntity,
}),
);
}
return true;
}
async createChannel({ title, about = "" }: CreateChannelParams) {
return await this.client.invoke(
new Api.channels.CreateChannel({ title, about, broadcast: true }),
);
}
async createGroup({ title, users }: CreateGroupParams) {
if (!Array.isArray(users)) {
users = [users];
}
return await this.client.invoke(
new Api.messages.CreateChat({ title, users }),
);
}
async createSupergroup({ title, about = "" }: CreateSupergroupParams) {
return await this.client.invoke(
new Api.channels.CreateChannel({ title, about, megagroup: true }),
);
}
async deleteChannel({ chatId }: DeleteChannelParams) {
await this.client.invoke(
new Api.channels.DeleteChannel({
channel: await this.client.getEntity(chatId),
}),
);
return true;
}
async deleteChatPhoto({ chatId }: DeleteChatPhotoParams) {
const chatEntity = await this.client.getEntity(chatId);
if (chatEntity instanceof Api.Chat) {
await this.client.invoke(
new Api.messages.EditChatPhoto({
chatId: chatEntity.id,
photo: new Api.InputChatPhotoEmpty(),
}),
);
} else if (chatEntity instanceof Api.Channel) {
await this.client.invoke(
new Api.channels.EditPhoto({
channel: chatEntity,
photo: new Api.InputChatPhotoEmpty(),
}),
);
} else {
throw new Error("Provided entity belongs to user");
}
return true;
}
async deleteSupergroup({ chatId }: DeleteSupergroupParams) {
await this.client.invoke(
new Api.channels.DeleteChannel({
channel: await this.client.getEntity(chatId),
}),
);
return true;
}
async deleteUserHistory({ chatId, userId }: DeleteUserHistoryParams) {
const r = await this.client.invoke(
new Api.channels.DeleteParticipantHistory({
channel: await this.client.getEntity(chatId),
participant: await this.client.getEntity(userId),
}),
);
return Boolean(r.ptsCount);
}
async getChatMember({ chatId, userId }: GetChatMemberParams) {
const chat = await this.client.getEntity(chatId);
const user = await this.client.getEntity(userId);
if (chat instanceof Api.Channel) {
return (
await this.client.invoke(
new Api.channels.GetParticipant({ channel: chat, participant: user }),
)
).participant;
} else if (chat instanceof Api.Chat) {
const fullChat = await this.client.invoke(
new Api.messages.GetFullChat({ chatId: chat.id }),
);
const members = fullChat.users;
return members.find((v) => v.id == user.id);
} else {
throw new Error(`The chatId "${chatId}" belongs to a user`);
}
}
/**
* Pin a message in a group, channel or your own chat.
* You must be an administrator in the chat for this to work and must have the "can_pin_messages" admin right in the supergroup or "can_edit_messages" admin right in the channel.
*/
async pinChatMessage({
chatId,
messageId,
bothSides = false,
disableNotification = false,
}: PinChatMessageParams) {
return await this.client.invoke(
new Api.messages.UpdatePinnedMessage({
peer: await this.client.getEntity(chatId),
id: messageId,
silent: disableNotification,
pmOneside: !bothSides,
}),
);
}
async promoteChatMember({
chatId,
userId,
isAnonymous = false,
canManageChat = true,
canChangeInfo = false,
canPostMessages = false,
canEditMessages = false,
canDeleteMessages = false,
canRestrictMembers = false,
canInviteUsers = false,
canPinMessages = false,
canPromoteMembers = false,
canManageCalls = false,
rank = "Admin",
}: PromoteChatMemberParams) {
return await this.client.invoke(
new Api.channels.EditAdmin({
userId: await this.client.getEntity(userId),
channel: await this.client.getEntity(chatId),
rank: rank,
adminRights: new Api.ChatAdminRights({
anonymous: isAnonymous,
changeInfo: canChangeInfo,
postMessages: canPostMessages,
editMessages: canEditMessages,
deleteMessages: canDeleteMessages,
banUsers: canRestrictMembers,
inviteUsers: canInviteUsers,
pinMessages: canPinMessages,
addAdmins: canPromoteMembers,
manageCall: canManageCalls,
other: canManageChat,
}),
}),
);
}
async unbanChatMember({ chatId, userId }: UnbanChatMemberParmas) {
const chat = await this.client.getEntity(chatId);
const user = await this.client.getEntity(userId);
if (chat instanceof Api.Channel) {
await this.client.invoke(
new Api.channels.EditBanned({
channel: chat,
participant: user,
bannedRights: new Api.ChatBannedRights({ untilDate: 0 }),
}),
);
}
return true;
}
/**
* Unpin a message in a group, channel or your own chat.
* You must be an administrator in the chat for this to work and must have the "can_pin_messages" admin right in the supergroup or "can_edit_messages" admin right in the channel.
*/
async unpinChatMessage({ chatId, messageId }: UnpinChatMessageParams) {
return await this.client.invoke(
new Api.messages.UpdatePinnedMessage({
peer: await this.client.getEntity(chatId),
id: messageId,
unpin: true,
}),
);
}
}