Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

python - send whats popping up in the console?

I have a run command with my discord bot, it basically runs a bash command from the terminal. It works perfectly fine, but I want the discord bot to send what the command is saying in the console. for example if you type .run pwd, it should send a message saying the current directory. This is my code so far

@commands.check(owner)
async def run(ctx, *, arg):
  response = os.system(arg)
  ctx.author.send(response)  

Any help would be appreciated! thanks

question from:https://stackoverflow.com/questions/65661436/send-whats-popping-up-in-the-console

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Are you looking for something like this?

@commands.is_owner()
@client.command()
async def run(ctx, *, arg):
    response = os.popen(arg).read()
    try:
        await ctx.author.send(response)
    except:
        await ctx.author.send(f'/bin/sh: 1: {arg}: not found')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...