From af035364106a202e1d682d618e47631aee2ffa98 Mon Sep 17 00:00:00 2001 From: Expand-sys Date: Wed, 6 Oct 2021 15:50:56 +1100 Subject: [PATCH] minecraft now spawnable as child process --- commands/moderation/mc.js | 3 ++- commands/moderation/start.js | 47 ++++++++++++++++++++++++++++++++++++ commands/moderation/stop.js | 30 +++++++++++++++++++++++ index.js | 16 ++++++++++-- package-lock.json | 12 ++++----- 5 files changed, 99 insertions(+), 9 deletions(-) create mode 100644 commands/moderation/start.js create mode 100644 commands/moderation/stop.js diff --git a/commands/moderation/mc.js b/commands/moderation/mc.js index 89bcb3f..0e386a0 100644 --- a/commands/moderation/mc.js +++ b/commands/moderation/mc.js @@ -2,6 +2,7 @@ const fs = require("fs"); const got = require("got"); const { Rcon } = require("rcon-client"); +const { spawn } = require("child_process"); module.exports = { name: "mc", @@ -13,7 +14,7 @@ module.exports = { return message.reply("OI you need to specify the command you want"); } else { const rcon = new Rcon({ - host: "twix.aosync.me", + host: "mc.expand.gay", port: 25575, password: "6pnyf2DsCfHV67d3", }); diff --git a/commands/moderation/start.js b/commands/moderation/start.js new file mode 100644 index 0000000..4e7d33d --- /dev/null +++ b/commands/moderation/start.js @@ -0,0 +1,47 @@ +const fs = require("fs"); +const got = require("got"); + +const { spawn } = require("child_process"); +let minecraftin; +module.exports = { + name: "start", + description: "start mc server", + guildOnly: true, + permissions: "KICK_MEMBERS", + async execute(message, args) { + let channel = message.guild.channels.cache.get(process.env.CONSOLECHAN); + spawnMC(channel); + }, +}; + +function spawnMC(channel) { + let options = { + shell: true, + cwd: "~/minecraft/", + }; + const minecraft = spawn( + "java", + ["-Xmx4096M", "-Xms1024M", "-jar", "server.jar", "nogui"], + options + ); + minecraftin = minecraft; + minecraft.stdout.on("data", (data) => { + try { + channel.send(`${data}`); + } catch (err) { + console.log(err); + } + }); + + minecraft.stderr.on("data", (data) => { + try { + channel.send(`${data}`); + } catch (err) { + console.log(err); + } + }); + + minecraft.on("close", (code) => { + console.log(`child process exited with code ${code}`); + }); +} diff --git a/commands/moderation/stop.js b/commands/moderation/stop.js new file mode 100644 index 0000000..e00cdda --- /dev/null +++ b/commands/moderation/stop.js @@ -0,0 +1,30 @@ +const fs = require("fs"); +const got = require("got"); +const { Rcon } = require("rcon-client"); + +const { spawn } = require("child_process"); +let minecraftin; +module.exports = { + name: "stop", + description: "stop mc server", + guildOnly: true, + permissions: "KICK_MEMBERS", + async execute(message, args) { + const rcon = new Rcon({ + host: "mc.expand.gay", + port: 25575, + password: "6pnyf2DsCfHV67d3", + }); + await rcon.connect(); + let res = await rcon.send(`/stop`); + message.reply(`Server stopped Safely`); + }, +}; + +function spawnMC(channel) { + let options = { + detached: true, + shell: true, + cwd: "/home/harrison/Desktop/minecraft/", + }; +} diff --git a/index.js b/index.js index 5e7fca2..016e1e7 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,24 @@ const fs = require("fs"); const fetch = require("node-fetch"); -const { Client, MessageAttachment, Collection } = require("discord.js"); +const { + Client, + MessageAttachment, + Intents, + Collection, +} = require("discord.js"); const dotenv = require("dotenv"); dotenv.config(); const prefix = process.env.PREFIX; -const client = new Client(); +const client = new Client({ + intents: [ + Intents.FLAGS.GUILDS, + Intents.FLAGS.GUILD_MESSAGES, + Intents.FLAGS.GUILD_MEMBERS, + Intents.FLAGS.GUILD_PRESENCES, + ], +}); client.commands = new Collection(); client.cooldowns = new Collection(); diff --git a/package-lock.json b/package-lock.json index 1b64a86..a863d87 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2327,9 +2327,9 @@ } }, "node_modules/tar": { - "version": "6.1.0", - "resolved": "https://registry.npmjs.org/tar/-/tar-6.1.0.tgz", - "integrity": "sha512-DUCttfhsnLCjwoDoFcI+B2iJgYa93vBnDUATYEeRx6sntCTdN01VnqsIuTlALXla/LWooNg0yEGeB+Y8WdFxGA==", + "version": "6.1.11", + "resolved": "https://registry.npmjs.org/tar/-/tar-6.1.11.tgz", + "integrity": "sha512-an/KZQzQUkZCkuoAA64hM92X0Urb6VpRhAFllDzz44U2mcD5scmT3zBc4VgVpkugF580+DQn8eAFSyoQt0tznA==", "dependencies": { "chownr": "^2.0.0", "fs-minipass": "^2.0.0", @@ -4347,9 +4347,9 @@ } }, "tar": { - "version": "6.1.0", - "resolved": "https://registry.npmjs.org/tar/-/tar-6.1.0.tgz", - "integrity": "sha512-DUCttfhsnLCjwoDoFcI+B2iJgYa93vBnDUATYEeRx6sntCTdN01VnqsIuTlALXla/LWooNg0yEGeB+Y8WdFxGA==", + "version": "6.1.11", + "resolved": "https://registry.npmjs.org/tar/-/tar-6.1.11.tgz", + "integrity": "sha512-an/KZQzQUkZCkuoAA64hM92X0Urb6VpRhAFllDzz44U2mcD5scmT3zBc4VgVpkugF580+DQn8eAFSyoQt0tznA==", "requires": { "chownr": "^2.0.0", "fs-minipass": "^2.0.0",