Skip to content

Instantly share code, notes, and snippets.

@simonmeusel
Last active June 23, 2023 09:44
Show Gist options
  • Save simonmeusel/1de7166288b12c04518c59158a85f5ef to your computer and use it in GitHub Desktop.
Save simonmeusel/1de7166288b12c04518c59158a85f5ef to your computer and use it in GitHub Desktop.
Start minecraft server via discord bot
var spawn = require('child_process').spawn;
var discord = require("discord.js");
var bot = new discord.Client();
var mcserver;
// The start.bat has to include a 'cd "C:/Users/_____/Desktop/Server/"' command (See start.bat for more details)
var MC_SERVER_START_SCRIPT = "C:/Users/_____/Desktop/Server/start.bat";
bot.on("message", function(message){
if (message.content == "start") {
// Only start if not running
if (mcserver == null) {
bot.sendMessage(message, "Starting server...");
// Start the server
mcserver = spawn(MC_SERVER_START_SCRIPT);
mcserver.stdout.on('data', (data) => {
console.log("stdout: " + data);
// Not everything is send (because i think there is a send limit per time)
// bot.sendMessage(message, "stdout: " + data);
});
mcserver.stderr.on('data', (data) => {
console.log("stderr: " + data);
bot.sendMessage(message, "stdout: " + data);
});
mcserver.on('close', (code) => {
console.log("child process exited with code " + code);
bot.sendMessage(message, "child process exited with code " + code);
});
}
} else if (message.content == "stop") {
// Only stop if running
if (message.content == "start") {
bot.sendMessage(message, "Stopping server...");
// Stop the server
mcserver.kill();
mcserver = null;
}
}
});
bot.login("email", "pass")
cd C:\Users\_____\Desktop\Server
java -jar spigot.jar
@TeddySenpai
Copy link

TeddySenpai commented Jul 7, 2022

@Stonley890 I tried your code and got the following error

Bot online!
Attempting to start server.
node:events:368
throw er; // Unhandled 'error' event
^

Error: spawn C:UsersHasnain JavedDocumentsMCServerDiscordBot-masterServersServerBStart.bat ENOENT
at Process.ChildProcess._handle.onexit (node:internal/child_process:282:19)
at onErrorNT (node:internal/child_process:477:16)
Emitted 'error' event on ChildProcess instance at:
at Process.ChildProcess._handle.onexit (node:internal/child_process:288:12)
at onErrorNT (node:internal/child_process:477:16) {
errno: -2,
code: 'ENOENT',
syscall: 'spawn C:UsersHasnain JavedDocumentsMCServerDiscordBot-masterServersServerBStart.bat',
path: 'C:UsersHasnain JavedDocumentsMCServerDiscordBot-masterServersServerBStart.bat',
spawnargs: []
}

I used your code but put in the .bat location ("C:\Users\Hasnain Javed\Documents\MCServerDiscordBot-master\Servers\ServerB\Start.bat") and the Bot Token.

Can you help?

(I'm on Windows if that means anything)

@BogTheMudWing
Copy link

@TeddySenpai I've reworked the script in this repository: Stonley890/mc-console-bot
Try using this one.

@Skywalker8510
Copy link

Skywalker8510 commented Dec 1, 2022

@Stonley890 or @Whitelisted1 is the code currently up to date because i tried it a few months back and couldn't get it to work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment