-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathdelete-messages.js
81 lines (75 loc) · 1.82 KB
/
delete-messages.js
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
export async function deleteMessages(chatId, messageArray) {
if (typeof chatId != 'string') {
return WAPI.scope(
null,
true,
404,
'enter the chatid variable as an string'
);
}
const chat = await WAPI.sendExist(chatId);
if (chat && chat.status != 404) {
if (!Array.isArray(messageArray)) {
return WAPI.scope(
chat,
true,
404,
'enter the message identification variable as an array'
);
}
for (let i in messageArray) {
if (typeof messageArray[i] === 'string') {
let checkID = await WAPI.checkIdMessage(chatId, messageArray[i]);
if (checkID.erro == true) {
return checkID;
}
}
}
let messagesToDelete = (
await Promise.all(
messageArray.map(
async (msgId) => await WAPI.getMessageById(msgId, null, false)
)
)
).filter((x) => x);
const To = chat.id;
const m = { type: 'deleteMessages' };
let jobs = [
Store.sendRevokeMsgs(
chat,
messagesToDelete.filter((msg) => msg.id._serialized.includes('true')),
true
),
Store.sendDeleteMsgs(
chat,
messagesToDelete.filter((msg) => msg.id._serialized.includes('true')),
true
)
];
try {
var result = (await Promise.all(jobs))[1];
if (result >= 0) {
let obj = WAPI.scope(To, false, result, '');
Object.assign(obj, m);
return obj;
}
} catch (e) {
let obj = WAPI.scope(
null,
true,
result,
'The message has not been deleted'
);
Object.assign(obj, m);
return obj;
}
let obj = WAPI.scope(To, true, result, '');
Object.assign(obj, m);
return obj;
} else {
if (!chat.erro) {
chat.erro = true;
}
return chat;
}
}