Python显示控制台输出

2024-10-03 19:30:31 发布

您现在位置:Python中文网/ 问答频道 /正文

现在我有一个命令,允许您将更改应用到我的托管服务器上的本地文件。有没有办法让bot将await ctx.send(...)命令的输出发送到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**")

任何建议或提示都会大有帮助


Tags: git命令sendoshavefetchoriginall
1条回答
网友
1楼 · 发布于 2024-10-03 19:30:31

使用^{}模块而不是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}")

相关问题 更多 >