在python中运行terraria

在python中运行terraria,python,Python,首先是我在我的discord服务器上运行的discord机器人,它将通过discord将命令拉到terraria服务器上!这就是我的想法! 在bot的代码中启动服务器的代码: def run_terraria_server(): global proc proc = run_terraria.terraria_exe() 这是我用来启动服务器的方法,其代码如下所示: class terraria_exe: def __init__(self): bat_location

首先是我在我的discord服务器上运行的discord机器人,它将通过discord将命令拉到terraria服务器上!这就是我的想法! 在bot的代码中启动服务器的代码:

def run_terraria_server():
    global proc
    proc = run_terraria.terraria_exe()
这是我用来启动服务器的方法,其代码如下所示:

class terraria_exe:
def __init__(self):
    bat_location = r'F:\SteamLibrary\steamapps\common\Terraria'
    Popen('TerrariaServer.exe -steam -lobby friends -config serverconfig.txt', cwd=f'{bat_location}',
                      stdin=PIPE, shell=True)

def new_command(self, command):
    subprocess.call(command, shell=True)
我在这里要做的是调用在init中定义的子进程,用服务器需要的两个额外命令运行TerrariaServer.exe

新的_命令方法基本上是在discord服务器中有人发出新的命令执行请求时使用的,例如:“中午”(更改ingame时间)、“退出”(关闭服务器)、“保存”(拯救世界)等

要启动服务器,我使用一个名为!特拉里亚

@client.command()
async def terraria(ctx):
    global is_server_on
    if is_server_on:
        ctx.send("Server is already running.")
    else:
        ctx.send("Server starting!")
        run_terraria_server()
        is_server_on = True
它所做的一切都是检查服务器是否已经在运行,如果它没有运行它

但后来我遇到了一个我无法解释的问题。它就像服务器在使用标准命令行同时运行一样。因为每次我调用这个函数:

@client.command()
async def terraria_commands(ctx, command):
    global is_server_on
    global proc
    if is_server_on:
        proc.new_command(command)
    else:
        ctx.send("Server is not running.")
因此,通过函数new_command,用户可以输入如下命令!terraria_命令一些命令,服务器应该执行它。但我得到的却是:

    'noon' is not recognized as an internal or external command,
     operable program or batch file.
什么??这就像服务器正在运行一样(我可以毫不费力地输入它),但命令是在标准cmd上执行的。我在这里没有得到什么吗

下面是terraria服务器正在运行的命令提示符的图片


任何帮助都是必要的

我已经找到了一个使用pywinauto的解决方案

基本上,这个脚本可以控制任何正在运行的exe以及运行它们

我所做的就是用一个子流程运行terraria:

self.proc = Popen('TerrariaServer.exe -steam -lobby friends -config serverconfig.txt', cwd=f'{bat_location}',
                      shell=True)
然后搜索使用pywinauto运行的应用程序:

dlg = Desktop(backend="uia")['TerrariaServer']
dlg.type_keys('%s\n{ENTER}' % command)
因为我有一台专用于运行terraria服务器的笔记本电脑,所以我忽略了任何人都会使用这台电脑的可能性,所以我只是通过一种自动方法(键入_键)发送命令