【问题标题】:Is there a way discord.py can listen for multiple commands at once?有没有一种方法 discord.py 可以一次监听多个命令?
【发布时间】:2022-01-18 09:33:59
【问题描述】:

我对 discord.py API 有点陌生,我有一个命令需要大约 4 分钟才能执行,但我似乎无法弄清楚如何让机器人在该命令执行时仍然听命令加工。如果有人有任何建议,请告诉我。

【问题讨论】:

  • 请提供您的命令代码。
  • 我认为你需要让你的命令异步。

标签: python api discord.py


【解决方案1】:

由于 discord.py 使用异步函数,机器人应该仍然可以工作

【讨论】:

    【解决方案2】:

    首先,discord.py 不是 API。它是用 python 编写的用于不和谐 API 的包装器。 More about API wrappers

    很有可能,您正在运行a blocking code 在异步函数中运行东西不会使其“非阻塞” (我完全知道该链接是针对讨论 JavaScript 但阻止的想法相同的网页)

    如果您正在执行繁重的任务,例如图像渲染或其他工作,您可能希望使用 loop.run_in_executor 运行它

    from discord.ext import commands
    
    def heavy_task(arg1, arg2):
        # your blocking code here, not asynchronous or coro calls here
        return # return from this function
    
    
    @bot.command()
    async def test(ctx: commands.Context, ...)
        loop = bot.loop
        arg1 = # some value function needs
        arg2 = # some other value function needs
        return_variable = await loop.run_in_executor(None, heavy_task, arg1, arg2)
        # do the rest of the stuff with return_variable, sending or whatever
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-21
      • 2022-06-11
      • 2010-10-08
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      • 2012-03-27
      相关资源
      最近更新 更多