【问题标题】:Using Discord to accept input and execute on connected clients使用 Discord 接受输入并在连接的客户端上执行
【发布时间】:2017-07-25 19:47:21
【问题描述】:
我目前有 Python Discord 预制脚本来连接 Discord 频道。我把它设置得很好,它可以工作。我能够创建命令并让它做我想做的事。但是我想知道是否可以在 Discord 上发送类似 !dir、!net use、!wmic 等命令,并让它在连接的客户端上运行,就好像它直接与机器交互一样。像贝壳,但通过 Discord?
我当前的代码是在这里找到的脚本:https://github.com/Rapptz/discord.py
我一直在尝试使用套接字和子进程执行此操作,但不确定如何使用 Discord API 执行此操作。
【问题讨论】:
标签:
python
python-3.x
discord
【解决方案1】:
我找到了我正在寻找的答案。
if message.content.startswith('!cmd'):
await client.send_message(message.channel, 'Enter command')
msg = await client.wait_for_message()
proc = subprocess.Popen(msg.content, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
stdout_value = proc.stdout.read() + proc.stderr.read()
await client.send_message(message.channel, stdout_value)
基本上,一旦机器人检测到我说 !cmd,它就会请求命令。然后我可以输入 ipconfig、net user 或任何我想要的命令,机器人就会像在命令提示符中一样执行它们。