簡體   English   中英

Python - 顯示控制台 output

[英]Python - Showing console output

現在我有一個命令,允許您將更改應用到我的托管服務器上的本地文件。 有沒有辦法讓機器人發送await ctx.send(...) git reset命令的 output 到 Discord? 通常 output 看起來像這樣:

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 這將允許您捕獲子進程的 output。

就像是:

@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}")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM