Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何打开终端并运行脚本,然后通过discord bot关闭它_Python_Python 3.x_Bash_Discord_Discord.py - Fatal编程技术网

Python 如何打开终端并运行脚本,然后通过discord bot关闭它

Python 如何打开终端并运行脚本,然后通过discord bot关闭它,python,python-3.x,bash,discord,discord.py,Python,Python 3.x,Bash,Discord,Discord.py,我正试图制作一个discord机器人,通过discord启动我的minecraft服务器。这就是我现在所拥有的 import os import discord import subprocess @client.event async def on_message(message): if message.author == client.user: return if message.content == 'server!start': aw

我正试图制作一个discord机器人,通过discord启动我的minecraft服务器。这就是我现在所拥有的

import os
import discord
import subprocess

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content == 'server!start':
        await message.channel.send('Loading ngrok...')
        #run terminal with ngrok
        await message.channel.send('Loading minecraft server...')
        #run terminal with minecraft server
    if message.content == 'server!stop':
        await message.channel.send('Stopping server...')
        #stop minecraft and ngrok by killing terminal
正如在代码中一样,我想打开终端并执行bash脚本来运行服务器,然后,当需要关闭它时,我想杀死终端或将“stop”发送到控制台

编辑:我试图使用subprocess.Popen,但我无法从另一个if语句关闭它,而不是第一次执行它

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content == 'server!start':
        await message.channel.send('Loading ngrok...')
        ngrok = subprocess.Popen("/home/administrator/minecraft/server/startngrok.sh", shell=True)
        #run terminal with ngrok
        await message.channel.send('Loading minecraft server...')
        minecraft = subprocess.Popen("/home/administrator/minecraft/server/startserver.sh", shell=True)
        #run terminal with minecraft server
    if message.content == 'server!stop':
        await message.channel.send('Stopping server...')
        ngrok.terminate()
        server.terminate()
        #stop minecraft and ngrok by killing terminal
enter code here
这就是输出:

Ignoring exception in on_message
Traceback (most recent call last):
  File "/home/administrator/.local/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "/home/administrator/minecraft/bot/bot.py", line 25, in on_message
    ngrok.terminate()
UnboundLocalError: local variable 'ngrok' referenced before assignment

当前错误:
UnboundLocalError:赋值前引用的局部变量“ngrok”
这意味着在调用
ngrok.terminate()
之前没有初始化(定义)ngrok。发生这种情况是因为您在上面的
if
条件中初始化了ngrok,但是如果您从未运行过该条件呢?那么,
ngrok
将永远不会被定义为任何东西。您要做的是首先初始化它

导入操作系统
进口不和
导入子流程
@客户端事件
异步def on_消息(消息):
#定义“ngrok”变量
ngrok=null
如果message.author==client.user:
返回
如果message.content=='服务器!开始':
等待message.channel.send('Loading ngrok…'))
ngrok=subprocess.Popen(“/home/administrator/minecraft/server/startngrok.sh”,shell=True)
#使用ngrok运行终端
等待message.channel.send('正在加载minecraft服务器…')
minecraft=subprocess.Popen(“/home/administrator/minecraft/server/startserver.sh”,shell=True)
#使用minecraft服务器运行终端
如果message.content=='服务器!停止':
等待message.channel.send('正在停止服务器…')
#如果“ngrok”是非类型,请抓住它
尝试:
ngrok.terminate()
除属性错误外:
#异常消息或所需的异常代码
server.terminate()
#通过杀死终端来阻止雷舰和ngrok

然而,我看不到任何地方也定义了服务器。可能会出现更多的错误。如果您需要更多帮助,请提供。

到目前为止,您尝试了哪些帮助?你认为哪些选项有效?我正在尝试使用subprocess.Popen。它允许我打开终端并运行服务器,但我不能在运行它们之后的其他状态中终止它。看起来你走对了方向。您是否研究过如何终止子流程?这是一个好的开始,请做你的研究,并尝试实施它。我想我已经在这个网站上做了一切,但我不能终止它。我还是会出错。看看编辑,错误是不言自明的。它基本上意味着在这个范围级别上,它不知道什么是
ngrok
。您应该修复您的作用域,这样子流程对象就可以对这两个语句都进行访问。如果statementsNope不起作用,则子流程对象不起作用。它只是一直在给我这个例外信息。我试图定义
ngrok
server
本地和全局,但这两个选项都不起作用(我使用
None
定义null),很难确定到底发生了什么。请提供复制代码的步骤。