-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
185 lines (162 loc) · 5.26 KB
/
index.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//Dependencies
const Request = require("request")
//Main
async function user_info(username = String){
return new Promise((resolve)=>{
Request(`https://tryhackme.com/api/discord/user/${username}`, function(err, res, body){
if(err){
resolve("Something went wrong, make sure the username is valid.")
return
}
if(body.indexOf("avatar") != -1){
resolve(JSON.parse(body))
}else{
resolve("Invalid username.")
}
})
})
}
async function global_leaderboard(){
return new Promise((resolve)=>{
Request("https://tryhackme.com/api/leaderboards", function(err, res, body){
if(err){
resolve("Something went wrong with the API, please try again later.")
return
}
resolve(JSON.parse(body))
})
})
}
async function site_stats(){
return new Promise((resolve)=>{
Request("https://tryhackme.com/api/site-stats", function(err, res, body){
if(err){
resolve("Something went wrong with the API, please try again later.")
return
}
resolve(JSON.parse(body))
})
})
}
async function new_rooms(){
return new Promise((resolve)=>{
Request("http://tryhackme.com/api/new-rooms/", function(err, res, body){
if(err){
resolve("Something went wrong with the API, please try again later.")
return
}
resolve(JSON.parse(body))
})
})
}
async function room_info(room_code = String){
return new Promise((resolve)=>{
Request(`https://tryhackme.com/api/room/details?codes=${room_code}`, function(err, res, body){
if(err){
resolve("Something went wrong with the API, please make sure the room_code is valid.")
return
}
if(body.indexOf('"success": false') != -1){
resolve(JSON.parse(body))
}else{
resolve("Invalid room_code.")
}
})
})
}
async function user_badges(username = String){
return new Promise((resolve)=>{
Request(`https://tryhackme.com/api/badges/get/${username}`, function(err, res, body){
if(err){
resolve("Something went wrong with the API, please make sure the username is valid.")
return
}
if(body.indexOf('"name":') != -1){
resolve(JSON.parse(body))
}else{
resolve("Invalid username.")
}
})
})
}
async function room_series(keyword = String){
return new Promise((resolve)=>{
Request(`https://tryhackme.com/api/series?${keyword}`, function(err, res, body){
if(err){
resolve("Something went wrong with the API, please make sure the keyword is valid.")
return
}
if(body.indexOf('"_id":') != -1){
resolve(JSON.parse(body))
}else{
resolve("Invalid keyword.")
}
})
})
}
async function common_network_rooms(){
return new Promise((resolve)=>{
Request("https://tryhackme.com/api/networks", function(err, res, body){
if(err){
resolve("Something went wrong with the API, please try again later.")
return
}
resolve(JSON.parse(body))
})
})
}
async function common_rooms(){
return new Promise((resolve)=>{
Request("https://tryhackme.com/modules/summary", function(err, res, body){
if(err){
resolve("Something went wrong with the API, please try again later.")
return
}
resolve(JSON.parse(body))
})
})
}
async function user_rooms(username = String){
return new Promise((resolve)=>{
Request(`https://tryhackme.com/api/created-rooms/${username}`, function(err, res, body){
if(err){
resolve("Something went wrong with the API, please make sure the username is valid.")
return
}
if(body.indexOf('"success": true') != -1){
resolve(JSON.parse(body))
}else{
resolve("Invalid username.")
}
})
})
}
async function user_tickets(username = String){
return new Promise((resolve)=>{
Request(`https://tryhackme.com/games/tickets/won?username=${username}`, function(err, res, body){
if(err){
resolve("Something went wrong with the API, please make sure the username is valid.")
return
}
if(body.indexOf('"success": true') != -1){
resolve(JSON.parse(body))
}else{
resolve("Invalid username.")
}
})
})
}
//Exporter
module.exports = {
user_info: user_info,
global_leaderboard: global_leaderboard,
site_stats: site_stats,
new_rooms: new_rooms,
room_info: room_info,
user_badges: user_badges,
room_series: room_series,
common_network_rooms: common_network_rooms,
common_rooms: common_rooms,
user_rooms: user_rooms,
user_tickets: user_tickets
}