Python-显示控制台输出



现在我有一个命令,允许您将更改应用到我的托管服务器上的本地文件。有没有办法让机器人程序将await ctx.send(...)作为git reset命令的输出发送给Discord?通常输出如下:

HEAD is now at f8dd7fe Slash Commands/Pull Feature/JSON Changes!

这是我当前的命令:

@client.command()
@commands.has_role('Bot Manager')
async def gitpull(ctx):
typebot = config['BotType']
if typebot == "BETA":
os.system("git fetch --all")
os.system("git reset --hard origin/TestingInstance")
await ctx.send("I have attempted to *pull* the most recent changes in **TestingInstance**")
elif typebot == "STABLE":
os.system("git fetch --all")
os.system("git reset --hard origin/master")
await ctx.send("I have attempted to *pull* the most recent changes in **Master**")

使用subprocess模块而不是os.system。这将允许您捕获子流程的输出。

类似于:

@client.command()
@commands.has_role('Bot Manager')
async def gitpull(ctx):
typebot = config['BotType']
output = ''
if typebot == "BETA":
p = subprocess.run("git fetch --all", shell=True, text=True, capture_output=True, check=True)
output += p.stdout
p = subprocess.run("git reset --hard origin/TestingInstance", shell=True, text=True, capture_output=True, check=True)
output += p.stdout
await ctx.send(f"I have attempted to *pull* the most recent changes in **TestingInstance**n{output}")
elif typebot == "STABLE":
p = subprocess.run("git fetch --all", shell=True, text=True, capture_output=True, check=True)
output += p.stdout
p = subprocess.run("git reset --hard origin/master", shell=True, text=True, capture_output=True, check=True)
output += p.stdout
await ctx.send(f"I have attempted to *pull* the most recent changes in **Master**n{output}")

相关内容

  • 没有找到相关文章

最新更新