From 6fd6aa756dac1b435196a266382c5a837dc11001 Mon Sep 17 00:00:00 2001 From: Valentijn Date: Sun, 10 May 2026 22:19:52 +0200 Subject: [PATCH] started with simple rcon code --- main.js | 27 +++++++++++++++++++++------ minecraft.js | 18 +++++++++++++++--- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/main.js b/main.js index a7816ed..45dddb1 100644 --- a/main.js +++ b/main.js @@ -1,17 +1,19 @@ + /* Imports */ - - - +import { runCommand } from "./minecraft"; +const express = require('express') +require('dotenv').config() /* Constant declaration */ +const app = express(); +app.use(express.json()) - - +const port = process.env.PORT || 3000; /* @@ -27,9 +29,22 @@ Passport.js OAuth2/OICD handler /* Express Routes */ +app.post("/api/command", (req,res)=>{ + const body = req.body; + if (!body["command"]) return res.sendStatus(400); + + let commandResponse = runCommand(body["command"]); + + res.send({ + response: commandResponse + }) +}) /* Run Express -*/ \ No newline at end of file +*/ +app.listen(() => { + console.log("Running Server!") +}, port) \ No newline at end of file diff --git a/minecraft.js b/minecraft.js index 6888ae2..64575e6 100644 --- a/minecraft.js +++ b/minecraft.js @@ -1,13 +1,25 @@ + /* Imports */ +const { Rcon } = require("rcon-client"); +require('dotenv').config() /* helper functions (eg, getOnlinePlayers()) */ +export async function runCommand(command){ + if (typeof command != String) return; + const rcon = await Rcon.connect({ + host: process.env.MC_HOST, port: process.env.MC_RCON_PORT, password: process.env.MC_RCON_PASSWORD + }) + + let response = await rcon.send(command); + + rcon.end(); + + return response; +} -/* -module.exports all those functions -*/ \ No newline at end of file