Skip to content

Commit

Permalink
Large update
Browse files Browse the repository at this point in the history
New getVehicles() function, this seems to be a new api feature.
WIP Error handling.
  • Loading branch information
galaxine-senpai committed Mar 14, 2024
1 parent cee9e81 commit c086650
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 2 deletions.
15 changes: 15 additions & 0 deletions example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
16 changes: 16 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,28 @@ 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)
}).catch(console.error)

/**
* Now we are all done!
* **EXAMPLE UP TO DATE AS OF VERSION 1.3.0**
*/
```
24 changes: 24 additions & 0 deletions src/classes/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,30 @@ class erlcClient {
runCmd(command) {
return main.runCmd(command);
}

/**
* Get the list of vehicles spawned in the server.
* @returns {Promise<JSON>}
*/
getVehicles() {
return main.getVehicles();
};

/**
* Get the list of mod calls
* @returns {Promise<JSON>}
*/
getModCalls() {
return main.getModCalls();
};

/**
* Get the list of command logs
* @returns {Promise<JSON>}
*/
getCommandLogs() {
return main.getCommandLogs();
};
}

module.exports = erlcClient;
3 changes: 3 additions & 0 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
12 changes: 12 additions & 0 deletions src/utils/errorHandler.js
Original file line number Diff line number Diff line change
@@ -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'
};
}
15 changes: 15 additions & 0 deletions src/utils/getCommandLogs.js
Original file line number Diff line number Diff line change
@@ -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())
})
}
15 changes: 15 additions & 0 deletions src/utils/getModCalls.js
Original file line number Diff line number Diff line change
@@ -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())
})
}
15 changes: 15 additions & 0 deletions src/utils/getVehicles.js
Original file line number Diff line number Diff line change
@@ -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())
})
}

0 comments on commit c086650

Please sign in to comment.