-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeedrun.js
87 lines (73 loc) · 2.33 KB
/
speedrun.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
const SpeedrunAPI = require('speedrunapi');
const sr = new SpeedrunAPI();
module.exports = {
getUserByUsername(username) {
return new Promise((resolve, reject) => {
if (username && username.length <= 20) {
sr.users(username)
.exec()
.then(response => {
const user = response.items;
resolve({
user_id: user.id
});
})
.catch(error => {
if (error.statusCode == 404) resolve(null); // Doesn't exist
else reject(error); // Unknown error...
});
} else resolve(null); // Invalid or too long
});
},
async getUserByID(id) {
// Default values
let data = {
id: null,
username: null
};
const account = await sr.users(id).exec()
.then(res => res.items)
.catch((error) => (data = null, __logError('[Speedrun] getUserByID() error', error)));
if (data) {
data.id = account.id;
data.username = account.names.international;
}
return {
requests: 1,
data
};
},
async getUserPersonalBests(user_id) {
// Default values
let data = {
username: null,
runs: []
};
const runs = await sr.users(user_id).personalBests()
.param({
max: 200
})
.param({
embed: ['players']
})
.exec().then(runs => runs.items)
.catch((error) => (data = null, __logError('[Speedrun] getUserPersonalBests() error', error)));
if (data) { // If no errors
for (const srRun of runs) {
data.username = srRun.players.data[0].names.international;
data.runs.push({
place: srRun.place,
time: srRun.run.times.primary_t,
settings: {
game: srRun.run.game,
category: srRun.run.category
}
});
}
}
return {
requests: 1,
data
};
},
};