-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
37 lines (30 loc) · 1.08 KB
/
utils.js
File metadata and controls
37 lines (30 loc) · 1.08 KB
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
const store = require('./store');
const lodash = require('lodash');
exports.getUniqueRandomMembers = (n, members) =>
Array(n).fill(null)
.reduce((indexes, index) => {
const randomIndex = getUniqueRandomIndex(indexes, members.length);
return [...indexes, randomIndex];
}, [])
.map((index) => members[index]);
exports.replaceUser = (selectedUsers, users) => {
const candidates = lodash.difference(users, selectedUsers);
return this.getUniqueRandomMembers(1, candidates)[0];
}
exports.getElegibleMembers = async (app, channel) => {
const users = await app.client.conversations.members({
token: process.env.SLACK_BOT_TOKEN,
channel,
limit: 1000
});
const unsubscribedUsers = store.getUnsubscribed();
return lodash.difference(users.members, unsubscribedUsers);
}
const getUniqueRandomIndex = (previous, length) => {
const randomIndex = Math.floor(Math.random() * length);
const unsubscribedUsers = store.getUnsubscribed();
if (previous.includes(randomIndex)) {
return getUniqueRandomIndex(previous, length);
}
return randomIndex;
}