在蟒蛇中运行泰拉瑞亚



好的,首先是我在不和谐服务器上运行的不和谐机器人,它将通过不和谐拉动命令并进入泰拉瑞亚服务器!就是这个想法! 机器人代码中用于启动服务器的代码:

def run_terraria_server():
global proc
proc = run_terraria.terraria_exe()

这是我用来启动服务器的方法,其代码如下所示:

class terraria_exe:
def __init__(self):
bat_location = r'F:SteamLibrarysteamappscommonTerraria'
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其中包含服务器需要的几个额外命令。

new_command方法基本上是在 discord 服务器中的某人对要执行的命令提出新请求时使用的,例如:"中午"(更改游戏时间(、"退出"(关闭服务器(、"保存"(拯救世界(等。

要启动服务器,我使用一个名为!terraria的命令

@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_commands某个命令,服务器应该执行它。但相反,我得到这个:

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

咦??就像服务器正在运行一样(确实如此,我可以毫无问题地输入它(,但是命令在标准cmd上执行。 我在这里没有得到什么吗?

这是泰拉瑞亚服务器正在运行的命令提示符的图片!

服务器命令提示符

任何帮助都是有的!

好吧,我找到了使用pywinauto的解决方案。

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

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

所以我所做的是我用一个子流程运行泰拉瑞亚:

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('%sn{ENTER}' % command)

由于我有一台专门用于运行泰拉瑞亚服务器的笔记本电脑,因此我忽略了任何人使用PC的可能性,因此我只是通过自动方法(type_keys(发送命令。

最新更新