Discordbot(Python)无法找到user_jvm_args.txt文件(启动bat文件,Minecraft服务器)

-1 投票
0 回答
30 浏览
提问于 2025-04-12 00:37

我想用Python做一个Discord机器人,来启动一个Minecraft服务器,所以我用了一些批处理文件,但它找不到user_jvm_args.txt这个文件,即使我已经告诉它这个文件在哪里了。我编程不太好,大部分代码都是用ChatGPT帮我写的,如果有奇怪的代码行请见谅。没错,我已经导入了令牌。

import discord
from discord.ext import commands 
import subprocess
import os
import asyncio

client = commands.Bot(command_prefix="!", intents=discord.Intents.all())
running_process = None
BOT_TOKEN = 'Token'  # Replace 'YOUR_DISCORD_BOT_TOKEN' with your actual Discord bot token

@client.event
async def on_ready():
    print("The bot is now ready for use!")

@client.command()
async def start(ctx):
    global running_process
    if running_process and running_process.poll() is None:
        await ctx.send("A process is already running.")
        return

    batch_file_path = r"C:/Users/tomwi/Desktop/Dommmamjaydenantom/run.bat"  # Update this path
    user_jvm_args_path = r"C:/Users/tomwi/Desktop/Dommmamjaydenantom/user_jvm_args.txt"  # Update this path
    try:
        await execute_batch_file(ctx, batch_file_path, user_jvm_args_path)
    except Exception as e:
        await ctx.send(f"Failed to start batch file: {e}")

async def execute_batch_file(ctx, batch_file_path, user_jvm_args_path):
    with open(user_jvm_args_path, 'r') as f:
        user_jvm_args = f.read().strip()
    command = f'java @{user_jvm_args} -Xmx10G -Xms9G "{batch_file_path}"'
    running_process = await asyncio.create_subprocess_shell(command)
    await ctx.send("Batch file started.")

@client.command()
async def stop(ctx):
    global running_process
    if running_process and running_process.poll() is None:
        try:
            running_process.terminate()
            await ctx.send("Batch file stopped.")
        except Exception as e:
            await ctx.send(f"Failed to stop batch file: {e}")
    else:
        await ctx.send("No batch file is currently running.")

client.run(BOT_TOKEN)

我尝试了很多方法,比如把这个文件放在机器人的目录里,最开始代码里没有提到这个文件,但后来我加上了,结果还是没用。不过就像我说的,我对编程真的不太懂。

0 个回答

暂无回答

撰写回答