From c086650400a617c13c7dc0c19ea1c5684d55d931 Mon Sep 17 00:00:00 2001 From: Ayden <70488944+galaxine-senpai@users.noreply.github.com> Date: Thu, 14 Mar 2024 07:59:26 -0700 Subject: [PATCH] Large update New getVehicles() function, this seems to be a new api feature. WIP Error handling. --- example/example.js | 15 +++++++++++++++ package-lock.json | 2 +- package.json | 2 +- readme.md | 16 ++++++++++++++++ src/classes/client.js | 24 ++++++++++++++++++++++++ src/client.js | 3 +++ src/utils/errorHandler.js | 12 ++++++++++++ src/utils/getCommandLogs.js | 15 +++++++++++++++ src/utils/getModCalls.js | 15 +++++++++++++++ src/utils/getVehicles.js | 15 +++++++++++++++ 10 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 src/utils/errorHandler.js create mode 100644 src/utils/getCommandLogs.js create mode 100644 src/utils/getModCalls.js create mode 100644 src/utils/getVehicles.js diff --git a/example/example.js b/example/example.js index 92fccdf..a332168 100644 --- a/example/example.js +++ b/example/example.js @@ -29,6 +29,21 @@ Client.getServerInfo().then(info => { "Server Info: " + console.log(info) }).catch(console.error) +// Hey lets try getting the vehicles in the server +Client.getVehicles().then(vehicles => { + "Vehicles: " + console.log(vehicles) +}).catch(console.error) + +// Now lets try getting the mod calls +Client.getModCalls().then(calls => { + "Mod Calls: " + console.log(calls) +}).catch(console.error) + +// Okay thats all fine and dandy but what about command logs? +Client.getCommandLogs().then(logs => { + "Command Logs: " + console.log(logs) +}).catch(console.error) + // Finally, lets run a command Client.runCmd(':h Hello World!').then(response => { "Command: " + console.log(response) diff --git a/package-lock.json b/package-lock.json index c3290dc..6b1eaf0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { "name": "erlc-js-wrapper", - "version": "1.2.0", + "version": "1.3.0", "lockfileVersion": 1 } diff --git a/package.json b/package.json index 9d28044..b2298e8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "erlc-js-wrapper", - "version": "1.2.0", + "version": "1.3.0", "description": "A JavaScript wrapper for the ER:LC API", "main": "index.js", "scripts": { diff --git a/readme.md b/readme.md index d823536..9654857 100644 --- a/readme.md +++ b/readme.md @@ -44,6 +44,21 @@ Client.getServerInfo().then(info => { "Server Info: " + console.log(info) }).catch(console.error) +// Hey lets try getting the vehicles in the server +Client.getVehicles().then(vehicles => { + "Vehicles: " + console.log(vehicles) +}).catch(console.error) + +// Now lets try getting the mod calls +Client.getModCalls().then(calls => { + "Mod Calls: " + console.log(calls) +}).catch(console.error) + +// Okay thats all fine and dandy but what about command logs? +Client.getCommandLogs().then(logs => { + "Command Logs: " + console.log(logs) +}).catch(console.error) + // Finally, lets run a command Client.runCmd(':h Hello World!').then(response => { "Command: " + console.log(response) @@ -51,5 +66,6 @@ Client.runCmd(':h Hello World!').then(response => { /** * Now we are all done! + * **EXAMPLE UP TO DATE AS OF VERSION 1.3.0** */ ``` diff --git a/src/classes/client.js b/src/classes/client.js index a32af84..252945d 100644 --- a/src/classes/client.js +++ b/src/classes/client.js @@ -79,6 +79,30 @@ class erlcClient { runCmd(command) { return main.runCmd(command); } + + /** + * Get the list of vehicles spawned in the server. + * @returns {Promise} + */ + getVehicles() { + return main.getVehicles(); + }; + + /** + * Get the list of mod calls + * @returns {Promise} + */ + getModCalls() { + return main.getModCalls(); + }; + + /** + * Get the list of command logs + * @returns {Promise} + */ + getCommandLogs() { + return main.getCommandLogs(); + }; } module.exports = erlcClient; diff --git a/src/client.js b/src/client.js index d3200a9..5aaf0d2 100644 --- a/src/client.js +++ b/src/client.js @@ -8,5 +8,8 @@ exports.getOnlinePlayers = require('./utils/getOnlinePlayers.js') exports.getServerBans = require('./utils/getServerBans.js') exports.getServerInfo = require('./utils/getServerInfo.js') exports.getServerKL = require('./utils/getServerKL.js') +exports.getVehicles = require('./utils/getVehicles.js') +exports.getModCalls = require('./utils/getModCalls.js') +exports.getCommandLogs = require('./utils/getCommandLogs.js') exports.init = require('./classes/client.js') diff --git a/src/utils/errorHandler.js b/src/utils/errorHandler.js new file mode 100644 index 0000000..c0988a2 --- /dev/null +++ b/src/utils/errorHandler.js @@ -0,0 +1,12 @@ +// WIP TO GET BETTER ERROR HANDLING FOR MORE INFO + +function errorHandler(int) { + // Array for error messages + const errorMessages = { + 400: 'Bad Request', + 401: 'Unauthorized', + 403: 'Forbidden (Did you forget the token or inputted it incorrectly?)', + 404: 'Not Found', + 500: 'Internal Server Error' + }; +} \ No newline at end of file diff --git a/src/utils/getCommandLogs.js b/src/utils/getCommandLogs.js new file mode 100644 index 0000000..31f7f0e --- /dev/null +++ b/src/utils/getCommandLogs.js @@ -0,0 +1,15 @@ +const { globalConfig, apiURL } = require('../client.js') + +module.exports = (token) => { + return new Promise(async (resolve, reject) => { + const r = await fetch(`${apiURL}/server/commandlogs`, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + 'Server-Key': globalConfig.token + } + }) + if (r.status !== 200) return reject(new Error('[getCommandLogs] API Returned: ' + r.status)) + else return resolve(r.json()) + }) +} \ No newline at end of file diff --git a/src/utils/getModCalls.js b/src/utils/getModCalls.js new file mode 100644 index 0000000..38d911b --- /dev/null +++ b/src/utils/getModCalls.js @@ -0,0 +1,15 @@ +const { globalConfig, apiURL } = require('../client.js') + +module.exports = (token) => { + return new Promise(async (resolve, reject) => { + const r = await fetch(`${apiURL}/server/killlogs`, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + 'Server-Key': globalConfig.token + } + }) + if (r.status !== 200) return reject(new Error('[getServerKL] API Returned: ' + r.status)) + else return resolve(r.json()) + }) +} \ No newline at end of file diff --git a/src/utils/getVehicles.js b/src/utils/getVehicles.js new file mode 100644 index 0000000..26aa5eb --- /dev/null +++ b/src/utils/getVehicles.js @@ -0,0 +1,15 @@ +const { globalConfig, apiURL } = require('../client.js') + +module.exports = (token) => { + return new Promise(async (resolve, reject) => { + const r = await fetch(`${apiURL}/server/vehicles`, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + 'Server-Key': globalConfig.token + } + }) + if (r.status !== 200) return reject(new Error('[getVehicles] API Returned: ' + r.status)) + else return resolve(r.json()) + }) +} \ No newline at end of file