forked from Konard/vk-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
friends.js
139 lines (117 loc) · 3.8 KB
/
friends.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
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
const { VK } = require('vk-io');
const { sleep } = require('./utils');
const token = require('fs').readFileSync('token', 'utf-8').trim();
const vk = new VK({ token });
const targetFriendsCount = Number(process.argv[2]) || 0;
const sourceCommunityId = 54530371;
const sex = {
male: 2
};
const requestsLimit = 10000; // Maximum number of requests you expect
const requestsSegmentSize = 1000; // Number of requests fetched per segment
async function fetchRequests(segment, offset) {
const req = await vk.api.friends.getRequests({ out: 1, count: segment, offset: offset });
return req || [];
}
async function fetchAllRequests() {
let requestsIds = [];
for (let offset = 0; offset < requestsLimit; offset += requestsSegmentSize) {
const segment = await fetchRequests(requestsSegmentSize, offset);
requestsIds = requestsIds.concat(segment.items);
if (segment.items.length < requestsSegmentSize) {
// Early exit if we fetched less requests than requested: end of data.
break;
}
}
return requestsIds;
}
function randomInRange(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min);
}
async function getOnlineFollowers(groupId, requestsIds) {
const offset = randomInRange(0, 428000);
let response = await vk.api.groups.getMembers({ group_id: groupId, sort: "id_desc", fields: ["online", "can_send_friend_request", "can_write_private_message", "can_see_all_posts", "is_friend", "sex", "has_photo", "language", "city"], offset, count: 1000 });
console.log('response.items.length', response.items.length);
let onlineFollowers = [], friendIds = [], requestIds = [];
for (let follower of response.items) {
if (!follower.online) {
continue;
}
if (follower.sex != sex.male) {
continue;
}
if (follower.is_friend) {
continue;
}
if (!follower.has_photo) {
continue;
}
if (!follower.can_see_all_posts) {
continue;
}
if (!follower.can_write_private_message) {
continue;
}
if (!follower.can_send_friend_request) {
continue;
}
if (follower.is_closed) {
continue;
}
if (!follower.can_access_closed) {
continue;
}
console.log('follower', follower);
if (!requestsIds.includes(follower.id)) {
onlineFollowers.push(follower.id);
}
}
return onlineFollowers;
}
async function sendFriendRequest(userId) {
return vk.api.friends.add({ user_id: userId });
}
let messagesHandlerInterval;
async function main() {
if (targetFriendsCount <= 0) {
return;
}
const requestsIds = await fetchAllRequests();
let onlineFollowersIds = [];
while (onlineFollowersIds.length < targetFriendsCount) {
onlineFollowersIds = [...onlineFollowersIds, ...await getOnlineFollowers(sourceCommunityId, requestsIds)];
console.log('Current number of friends to invite: ', onlineFollowersIds.length);
await sleep(3000);
}
if (onlineFollowersIds.length <= 0) {
console.log('No followers to add as friends found.');
return;
} else {
console.log(`${onlineFollowersIds.length} followers will be added as friends.`);
}
messagesHandlerInterval = setInterval(() => {
const followerId = onlineFollowersIds.shift(); // dequeue follower
if (!followerId) {
clearInterval(messagesHandlerInterval);
return;
}
sendFriendRequest(followerId)
.then(response => console.log(`Friend request sent to: ${followerId}`))
.catch(e => {
if (e.code === 8) {
console.log('Friend requests limit reached (40 per day or 10000 requests+friends).')
clearInterval(messagesHandlerInterval);
return;
}
console.log(e);
});
// try {
// await sendFriendRequest(followerId);
// } catch (e) {
// console.log(e);
// }
}, 5000);
}
main();