【问题标题】:How to run discordpy terminal in bot如何在机器人中运行不和谐的终端
【发布时间】:2021-04-15 21:02:34
【问题描述】:

我想为我的机器人创建一个命令,让我可以直接通过机器人运行命令 如:

输入:/run await ctx.send("hi stackoverflow")

机器人:你好 stackoverflow

我一直在研究如何在 discordpy 中制作这样的命令,但我得到的最接近的是使用 exec,但它只是抛出了 discord.ext.commands.errors.CommandInvokeError: Command raised an exception: SyntaxError: 'await' outside function (<string>, line 1)

我讨厌在没有适当代码示例的情况下提出问题,但感谢您提供任何帮助

【问题讨论】:

  • 您是否尝试过使用exec()

标签: python python-3.x discord discord.py python-os


【解决方案1】:

如果您正在寻找命令行界面,我建议您这样做:

import threading, time


def waitfor():
    cmd = input('command: ')
    print(cmd)
    ## do command processing here eg. eval(cmd)

def runthread():
    t = threading.Thread(target=waitfor)
    t.start()
    return t

async def commandInterface():
    t = runthread()
    while 1:
        if not t.is_alive():
            t = runthread()
        time.sleep(1)
        ## loop stuff

client.loop.create_task(commandInterface)
client.run(TOKEN)

这使用线程模块等待输入并运行命令,而不会阻塞程序的其余部分。显然,它需要根据您的需要进行调整,但这可能是一个好的开始。

【讨论】:

    猜你喜欢
    • 2020-02-28
    • 1970-01-01
    • 2021-05-24
    • 2018-04-17
    • 2021-08-09
    • 1970-01-01
    • 2020-06-30
    • 2021-06-15
    • 2018-03-20
    相关资源
    最近更新 更多