有没有办法获取逐行控制台输出并将其打印在单独的脚本中?
Is there a way to get line-by-line console output and print it in a separate script?
在 运行 不和谐机器人的脚本中,我想在用户请求时 运行 其他脚本(如 /script2
)并逐行打印控制台这个调用的脚本正在输出的输出,每次有新输出时都会发送一条包含内容的新不和谐消息。
到目前为止,我已经通过在开头搜索“/”让脚本识别用户何时想要 运行 命令,但是我无法找到 [=20= 的正确方法] 从另一个程序中请求的脚本,并在不和谐频道中发送控制台输出。
有人知道怎么帮忙吗?
源代码
import discord
from subprocess import run #As of right now i'm using run to run the script, but it is simply the easiest way I managed to do it in.
TOKEN = 'TOKEN(Removed for the post)'
client = discord.Client()
@client.event
async def on_ready():
print("Logged in as {0.user}".format(client))
@client.event
async def on_message(message):
username = str(message.author).split("#")[0]
usermsg = str(message.content)
channel = str(message.channel.name)
print(f'{username}, {usermsg}, {channel}')
if message.author == client.user:
return
if message.channel.name == "test":
if usermsg.startswith("/"):
await message.channel.send(f'Executing your requested script, {username}')
arg = str(usermsg[1:len(usermsg)])
result = run(["python3", "Path/to/script", arg], capture_output=True)
output = result.stdout # At the moment, the second script doesn't output anything on the console, which is also not what I want. I would love if you could get both console output and send line-by-line output to discord, although I could live with the output only being sent to the discord channel.
print(output)
await message.channel.send(output)
client.run(TOKEN)
没有 result.output
属性,您要查找的属性是 stdout
,但是 returns 这是一个字节序列,您可以将其解码以将其转换为字符串:
import sys
result = run([sys.executable, "Path/to/script", arg], capture_output=True)
output = result.stdout.decode("utf-8")
print(output)
在 运行 不和谐机器人的脚本中,我想在用户请求时 运行 其他脚本(如 /script2
)并逐行打印控制台这个调用的脚本正在输出的输出,每次有新输出时都会发送一条包含内容的新不和谐消息。
到目前为止,我已经通过在开头搜索“/”让脚本识别用户何时想要 运行 命令,但是我无法找到 [=20= 的正确方法] 从另一个程序中请求的脚本,并在不和谐频道中发送控制台输出。
有人知道怎么帮忙吗?
源代码
import discord
from subprocess import run #As of right now i'm using run to run the script, but it is simply the easiest way I managed to do it in.
TOKEN = 'TOKEN(Removed for the post)'
client = discord.Client()
@client.event
async def on_ready():
print("Logged in as {0.user}".format(client))
@client.event
async def on_message(message):
username = str(message.author).split("#")[0]
usermsg = str(message.content)
channel = str(message.channel.name)
print(f'{username}, {usermsg}, {channel}')
if message.author == client.user:
return
if message.channel.name == "test":
if usermsg.startswith("/"):
await message.channel.send(f'Executing your requested script, {username}')
arg = str(usermsg[1:len(usermsg)])
result = run(["python3", "Path/to/script", arg], capture_output=True)
output = result.stdout # At the moment, the second script doesn't output anything on the console, which is also not what I want. I would love if you could get both console output and send line-by-line output to discord, although I could live with the output only being sent to the discord channel.
print(output)
await message.channel.send(output)
client.run(TOKEN)
没有 result.output
属性,您要查找的属性是 stdout
,但是 returns 这是一个字节序列,您可以将其解码以将其转换为字符串:
import sys
result = run([sys.executable, "Path/to/script", arg], capture_output=True)
output = result.stdout.decode("utf-8")
print(output)