mirror of
https://github.com/opus-tango/minecraft-server-discord-bot.git
synced 2026-03-20 03:55:20 +00:00
add startserver command to create screen instance
This commit is contained in:
49
bot.py
49
bot.py
@@ -1,12 +1,15 @@
|
|||||||
|
import json
|
||||||
import os
|
import os
|
||||||
import discord
|
import discord
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
import subprocess
|
import subprocess
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
import time
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
TOKEN = os.getenv('DISCORD_TOKEN')
|
TOKEN = os.getenv('DISCORD_TOKEN')
|
||||||
|
BASE_PATH = os.getenv('BASE_PATH', './')
|
||||||
print(TOKEN)
|
print(TOKEN)
|
||||||
|
|
||||||
intents = discord.Intents.default()
|
intents = discord.Intents.default()
|
||||||
@@ -29,6 +32,52 @@ async def mc(ctx, *args):
|
|||||||
stdout, stderr = process.communicate()
|
stdout, stderr = process.communicate()
|
||||||
await ctx.send(stdout)
|
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()
|
@bot.command()
|
||||||
async def ping(ctx):
|
async def ping(ctx):
|
||||||
await ctx.send('Pong!')
|
await ctx.send('Pong!')
|
||||||
|
|||||||
Reference in New Issue
Block a user