forked from Konard/vk-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accept-best-suggestion.js
48 lines (44 loc) · 1.98 KB
/
accept-best-suggestion.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
const { sleep } = require('./utils');
const { VK } = require('vk-io');
const token = require('fs').readFileSync('token', 'utf-8').trim();
const vk = new VK({ token });
const maximumSuggestionsToAccept = Number(process.argv[2]) || 0;
console.log('maximumSuggestionsToAccept', maximumSuggestionsToAccept);
async function deleteFriendRequests() {
try {
if (maximumSuggestionsToAccept <= 0) {
return;
}
const count = 100;
var currentUserId = (await vk.api.users.get())[0].id;
console.log('currentUserId', currentUserId);
const suggestions = (await vk.api.friends.getSuggestions({ filter: "mutual", fields: "online,can_post,can_see_all_posts,can_write_private_message,contacts,counters", count, offset: 0 })).items;
await sleep(3000);
console.log('suggestions: ', suggestions.length);
const candidates = suggestions.filter(s => s.can_post && s.can_see_all_posts && s.can_write_private_message && s.can_access_closed);
console.log('candidates: ', candidates.length);
const candidatesWithMutualFriendsCount = [];
for (const candidate of candidates) {
var mutualFriendsCount = (await vk.api.friends.getMutual({ source_uid: currentUserId, target_uid: candidate.id })).length;
await sleep(3000);
candidatesWithMutualFriendsCount.push([candidate.id, mutualFriendsCount]);
}
candidatesWithMutualFriendsCount.sort((a, b) => b[1] - a[1]);
console.log('candidatesWithMutualFriendsCount', candidatesWithMutualFriendsCount);
let suggestionsAccepted = 0;
for (const candidate of candidatesWithMutualFriendsCount) {
if (suggestionsAccepted >= maximumSuggestionsToAccept) {
break;
}
const candidateId = candidate[0];
const candidateMutualFriends = candidate[1];
(await vk.api.friends.add({ user_id: candidateId }));
await sleep(3000);
console.log('Friend request to', candidateId, 'sent.');
suggestionsAccepted++;
}
} catch (error) {
console.error(error);
}
}
deleteFriendRequests();