-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (56 loc) · 2.07 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
const axios = require('axios');
const baseUrl = "https://api.mcsrvstat.us/";
async function fetchJavaServer(ip) {
if (!ip) throw new Error("Minecraft Java IP not supplied");
else {
return axios(baseUrl + `2/${ip}`).then((response) => {
return {
online: response.data.online,
version: response.data.version,
players: response.data.players,
connection: {
ip: response.data.ip,
hostname: response.data.hostname || null
}
}
}).catch((e) => {
throw new Error(`Failed to fetch '${ip}'`, {cause:e})
})
}
}
async function fetchBedrockServer(ip) {
if (!ip) throw new Error("Minecraft Bedrock IP not supplied");
return axios(baseUrl + `bedrock/2/${ip}`).then((response) => {
return {
online: response.data.online,
version: response.data.version,
players: response.data.players,
connection: {
ip: response.data.ip,
hostname: response.data.hostname || null
}
}
}).catch((e) => {
throw new Error(`Failed to fetch '${ip}'`, {cause:e})
})
}
async function fetchIcon(ip) {
return `https://api.mcsrvstat.us/icon/${ip}`;
}
async function javaHttpCode(ip) {
return axios(baseUrl + `simple/${ip}`).then((response) => {
return response.status;
}).catch((e) => {
if (e.toJSON && e.toJSON().status === 404) return 404;
else throw new Error(`An unexpected status code/error was received while fetching '${ip}'`, {cause:e})
})
}
const bedrockHttpCode = async function(ip) {
return axios(baseUrl + `bedrock/simple/${ip}`).then((response) => {
return response.status;
}).catch((e) => {
if (e.toJSON && e.toJSON().status === 404) return 404;
else throw new Error(`An unexpected status code/error was received while fetching '${ip}'`, {cause:e})
})
}
module.exports = {fetchJavaServer, fetchBedrockServer, fetchIcon, javaHttpCode, bedrockHttpCode}