From 74ea15dc7ccd865cb28c1a3f86a816c1e8732580 Mon Sep 17 00:00:00 2001 From: Nayan <33187059+GShadow5@users.noreply.github.com> Date: Mon, 25 Aug 2025 19:35:04 -0400 Subject: [PATCH] add startserver command to create screen instance --- bot.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/bot.py b/bot.py index 10695bc..58fab7c 100644 --- a/bot.py +++ b/bot.py @@ -1,12 +1,15 @@ +import json import os import discord from discord.ext import commands import subprocess from dotenv import load_dotenv +import time load_dotenv() TOKEN = os.getenv('DISCORD_TOKEN') +BASE_PATH = os.getenv('BASE_PATH', './') print(TOKEN) intents = discord.Intents.default() @@ -29,6 +32,52 @@ async def mc(ctx, *args): stdout, stderr = process.communicate() await ctx.send(stdout) +@bot.command() +async def startserver(ctx, *args): + # Check if the user is an admin + if not ctx.author.guild_permissions.administrator: + await ctx.send("You don't have permission to run this command.") + return + # Validate and get the server name + if len(args) == 0: + await ctx.send("Please provide a server name.") + return + if len(args) > 1: + await ctx.send("Please provide only one server name.") + return + servername = args[0] + + # Load server data from json + with open('servers.json', 'r') as f: + servers = json.load(f) + servers = servers['servers'] + # Find the server in the json + server = None + for s in servers: + if s['servername'] == servername: + server = s + break + if server is None: + await ctx.send("Server not found.") + return + # Start the server + await ctx.send("Starting server, please wait...") + path = BASE_PATH + server['path'] if BASE_PATH else server['path'] + command = f"cd {path} && screen -dmS {servername} {server['startcommand']}" + print(f"Running command: {command}") + process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) + # Wait for the server to start then check if the screen instance is still live + stdout, stderr = process.communicate() + time.sleep(3) + process = subprocess.Popen(f"screen -ls | grep {servername}", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) + stdout, stderr = process.communicate() + print(stdout) + if len(stdout) == 0: + await ctx.send("Server failed to start. Minecraft screen instance terminated within 3 seconds.") + return + await ctx.send("Server started.") + await ctx.send(stdout) + @bot.command() async def ping(ctx): await ctx.send('Pong!')