【问题标题】:How to make a discord.py command be able to execute a ctx.send command如何使 discord.py 命令能够执行 ctx.send 命令
【发布时间】:2021-01-18 14:00:40
【问题描述】:

现在,我有一个我希望能够使用 Python 命令控制的机器人。这是我的 >execute 命令代码,我想用它来执行 Python sn-ps。

async def execute(ctx, *, arg):
    if ctx.author.id == 645264167623983124:
        try:
            exec(arg.replace("```", ""))
            await ctx.send(embed=msg(title="Execution complete!", desc="The code ran successfully."))
        except Exception as e:
            await ctx.send(embed=msg(title="Error", desc=str(e)))
    else:
        await ctx.send(embed=msg(title="Nuh Uh Uhh", desc="You are not allowed to use this command!"))

不过,我也想用这个命令来发送测试消息,像这样:

>execute await ctx.send("Hello world!")

当我运行它时,它会说:

Error
'await' outside function (<string>, line 1)

但是当我在没有等待的情况下运行它时,它说它运行良好,但是我运行机器人的控制台说:

<string>:1: RuntimeWarning: coroutine 'Messageable.send' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

【问题讨论】:

  • 你使用了@client.command()装饰器吗?
  • 是的,我做到了。看起来我没有包含它,哎呀。

标签: python async-await discord.py


【解决方案1】:

我使用eval 来代替异步函数:

arg = arg.replace("```", "")
if arg.startswith("await"):
    await eval(arg.replace("await ", ""))
else:
    exec(arg)

【讨论】:

    猜你喜欢
    • 2020-12-31
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 2021-03-26
    • 2021-02-22
    相关资源
    最近更新 更多