This repository has been archived by the owner on Oct 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
export.js
50 lines (40 loc) · 1.61 KB
/
export.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
const r2 = require('r2')
const fs = require('fs')
const baseurl = 'https://discordapp.com/api'
const headers = { 'authorization': process.argv[2] }
async function getMembers(id, headers, after = '') {
console.log(`[info] requesting users for guild: ${id}`);
try {
const response = await r2(`${baseurl}/guilds/${id}/members?limit=1000${after}`, { headers }).response
const results = (await response.json()) || []
const users = results.map(record => record.user)
if (users.length === 1000) {
const lastid = users[users.length - 1].id;
return users.concat(await getMembers(id, headers, `&after=${lastid}`))
}
return users
} catch (e) {
console.log(e)
return []
}
}
async function main(resultFolder) {
if (!headers.authorization) {
console.warn('[warn] you must provide a user discord auth token: node export.js [YOURTOKEN]')
return
}
// remove old data
fs.mkdir(resultFolder, () => {})
fs.readdirSync(resultFolder).map(file => fs.unlinkSync(`${resultFolder}/${file}`));
// get new data
const response = await r2(`${baseurl}/users/@me/guilds`, { headers }).json
const guildids = response.map(guild => guild.id)
const promises = guildids.map(async id => await getMembers(id, headers))
const results = await Promise.all(promises);
fs.writeFileSync('data/guilds.json', JSON.stringify(response, null, 4))
// save new data
results.map((members, i) => {
fs.writeFileSync(`${resultFolder}/${guildids[i]}.json`, JSON.stringify(members))
})
}
main('data/members')