-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackersBot.js
127 lines (116 loc) · 3.58 KB
/
BackersBot.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
const Config = require("./config.json");
const fetch = require('node-fetch');
const fetchEmailsQuery=`query collective ($offset: Int, $limit: Int){
collective(slug: "jmonkeyengine") {
name
backers: members(role: BACKER, offset: $offset, limit: $limit) {
limit
totalCount
nodes {
account {
slug
type
... on Individual {
email
name
website
}
... on Organization {
admins: members(role: ADMIN) {
nodes {
account {
... on Individual {
email
name
website
}
}
}
}
}
}
}
}
}
}
`;
async function getBackers(){
let offset=0;
let limit =1000;
const backers=[];
while(true){
const resp=await fetch(Config.openCollectiveApiEndPoint+"/"+Config.openCollectiveApiKey, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({
query: fetchEmailsQuery,
variables: {
offset:offset,
limit:limit
},
})
}).then((res)=>res.json());
for(let i in resp.data.collective.backers.nodes){
const node=resp.data.collective.backers.nodes[i];
backers.push(node.account);
}
const count=resp.data.collective.backers.totalCount;
if(offset+limit>=count)break;
offset+=limit;
}
return backers;
}
async function assignRoles() {
const emails = (await getBackers()).map(backer=>backer.email);
for (let i in emails) {
const email = emails[i];
let url = `${Config.discourseApiEndpoint}/admin/users/list/all.json?email=${email}`;
const res = await fetch(url, {
headers: {
"Api-Username": Config.discourseApiUser,
"Api-Key": Config.discourseApiKey
}
}).then(res => res.json());
console.log(res);
for (let j in res) {
const user = res[j];
url = `${Config.discourseApiEndpoint}/user_badges.json`;
const badges = [Config.backerHubBadgeId, Config.contributorHubBadge];
badges.forEach((b) => {
console.log("Assign badge " + b + " to ", user.username);
fetch(url, {
method: "post",
headers: {
"Api-Username": Config.discourseApiUser,
"Api-Key": Config.discourseApiKey,
'Content-Type': 'application/json'
},
body: JSON.stringify({ username: user.username, reason: "", badge_id: b })
}).then(res =>res.json()).then(res=>console.log( res));
});
}
}
}
async function listBackers(msg){
console.log(await getBackers());
const backers = (await getBackers()).map(backer=>backer.name+" "+(backer.website?backer.website:""));
msg.reply(`**Backers:**`);
backers.forEach(backer=>{
msg.reply(" - "+backer);
});
}
async function init(dbot) {
dbot.addCommand("backers",{
cmd:"backers",
desc:"List all the backers"
},(args,msg)=>listBackers(msg));
setInterval(assignRoles,1000*60*60);
assignRoles();
}
module.exports={
init:init,
getBackers:getBackers
}