Added few internals modules
- Added game setting parser/stringifier 📝 - Added skin upload using mojang api 👾 - Added server ping using mcsrvstat.us 🖥️ - Added api and endpoints to the config ⚙️ - Added syscalls for modules 🧩
This commit is contained in:
43
modules/gameSettings.js
Normal file
43
modules/gameSettings.js
Normal file
@@ -0,0 +1,43 @@
|
||||
const fs = require("node:fs")
|
||||
const path = require("node:path")
|
||||
|
||||
function parseSettings(settingsFile) {
|
||||
const settings = fs.readFileSync(settingsFile, "utf-8").toString().split("\n").filter(line => line.trim() !== "")
|
||||
const parseSettings = {}
|
||||
for (let i = 0; i < settings.length; i++) {
|
||||
const line = settings[i].trim()
|
||||
const [key, value] = line.split(":")
|
||||
if (key && value) {
|
||||
const trimmedValue = value.trim().toLowerCase()
|
||||
if (trimmedValue === "true" || trimmedValue === "false") {
|
||||
parseSettings[key.trim()] = trimmedValue === "true"
|
||||
} else {
|
||||
parseSettings[key.trim()] = isNaN(value) ? value.trim() : parseInt(value, 10)
|
||||
}
|
||||
}
|
||||
}
|
||||
return parseSettings
|
||||
}
|
||||
|
||||
function stringfySettings(settingsFile) {
|
||||
const settingsObject = JSON.parse(fs.readFileSync(settingsFile, "utf-8").toString())
|
||||
const settingsArray = []
|
||||
if (typeof settingsObject !== "object" || settingsObject === null) {
|
||||
throw new Error("Invalid settings object")
|
||||
}
|
||||
for (const settingKey in settingsObject) {
|
||||
if (Object.prototype.hasOwnProperty.call(settingsObject, settingKey)) {
|
||||
const settingValue = settingsObject[settingKey]
|
||||
if (typeof settingValue === "object" || typeof settingValue === "function" || typeof settingValue === "symbol") {
|
||||
throw new Error(`Invalid value for setting "${settingKey}": ${settingValue}`)
|
||||
}
|
||||
settingsArray.push(`${settingKey}:${settingValue}`)
|
||||
}
|
||||
}
|
||||
return settingsArray.join("\n")
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
parseSettings,
|
||||
stringfySettings
|
||||
}
|
||||
26
modules/mojangAPI.js
Normal file
26
modules/mojangAPI.js
Normal file
@@ -0,0 +1,26 @@
|
||||
const fs = require("node:fs")
|
||||
const path = require("node:path")
|
||||
const config = require("../config.json")
|
||||
|
||||
async function uploadSkin(filePath, variant, token) {
|
||||
const form = new FormData()
|
||||
form.append("variant", variant)
|
||||
form.append("file", fs.readFileSync(filePath), path.parse(filePath).base)
|
||||
const response = await fetch(`${config.minecraft.services.api.base}${config.minecraft.services.api.endpoints.uploadSkin}`, {
|
||||
method: "POST",
|
||||
body: form,
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`
|
||||
}
|
||||
})
|
||||
if (response.ok) {
|
||||
const json = await response.json()
|
||||
return json
|
||||
} else {
|
||||
throw new Error(`Error uploading skin: ${response.status} ${response.statusText}`)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
uploadSkin
|
||||
}
|
||||
18
modules/serverPing.js
Normal file
18
modules/serverPing.js
Normal file
@@ -0,0 +1,18 @@
|
||||
const config = require("../config.json")
|
||||
|
||||
async function fetchServerStatus() {
|
||||
const response = await fetch(`https://api.mcsrvstat.us/3/${config.minecraft.server.host}:${config.minecraft.server.port}`)
|
||||
const json = await response.json()
|
||||
return {
|
||||
online: json.online,
|
||||
players: {
|
||||
online: json.players.online || 0,
|
||||
max: json.players.max || 0
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
fetchServerStatus
|
||||
}
|
||||
Reference in New Issue
Block a user