在Python中运行Terraria

2024-05-31 23:33:23 发布

您现在位置: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 = 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服务器中有人提出执行命令的新请求时使用的,例如:“noon”(更改ingame时间)、“exit”(关闭服务器)、“save”(保存世界)等

为了启动服务器,我使用了一个名为!Terria公司

@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\u command,用户可以输入如下命令!terraria_命令一些命令,服务器应该执行它。但我得到的却是:

    'noon' is not recognized as an internal or external command,
     operable program or batch file.

什么??它就像服务器正在运行(它是,我可以毫无困难地进入它),但命令是在一个标准的cmd上执行的。我是不是有什么收获?你知道吗

下面是terraria服务器正在运行的命令提示符的图片!你知道吗

server command prompt

任何帮助都是必要的!你知道吗


Tags: run代码命令服务器serverisondef
1条回答
网友
1楼 · 发布于 2024-05-31 23:33:23

我用pywinauto找到了一个解决方案。你知道吗

https://pywinauto.readthedocs.io/en/latest/

基本上这个脚本可以控制任何正在运行的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服务器的笔记本电脑,所以我忽略了任何人都会使用这台电脑的可能性,所以我只是通过一种自动方法(键入\u键)发送命令。你知道吗

相关问题 更多 >