minecraft-server-info
Version:
Package na wyświetlanie informacji na temat serwera minecraft
48 lines (37 loc) • 1.1 kB
JavaScript
const axios = require('axios')
async function getData(api, ip, port = 25565) {
try {
const url = port === 25565 ? `${api}${ip}` : `${api}${ip}:${port}`;
const response = await axios.get(url);
return response.data;
} catch (error) {
return {};
}
}
function getValue(data, key) {
return key.split('.').reduce((o, k) => (o || {})[k], data);
}
function filterData(data, filters) {
const filteredData = {};
filters.forEach(filter => {
filteredData[filter] = getValue(data, filter) || null;
});
return filteredData;
}
async function status({ type, ip, port = 25565, show = [] }) {
let api;
if (type === 'java') {
api = 'https://api.mcsrvstat.us/3/';
} else if (type === 'bedrock') {
api = 'https://api.mcsrvstat.us/bedrock/3/';
} else {
console.error(`Unsupported server type ${type}, please use 'java' or 'bedrock'.`);
return;
}
const data = await getData(api, ip, port);
if (show.length > 0) {
return filterData(data, show);
}
return data;
}
module.exports = { status };