【问题标题】:bot.loop.create_task function does not work- Python Discord Botbot.loop.create_task 函数不起作用 - Python Discord Bot
【发布时间】:2021-10-22 00:32:09
【问题描述】:

我有一个 Discord Bot 程序,当我调用函数时需要发送消息,但代码卡在 bot.loop.create_task

下面是我的代码:

bot.py

async def send_msg(word):
    global bot
    await bot.wait_until_ready()
    ch=bot.get_channel(channel)
    print(ch)
    await ch.send("The word "+word+" has been spoken :)")


def action(word):
    bot.loop.create_task(send_msg(word))
    

send_msg.py

    from bot import action
    action(word)

【问题讨论】:

  • 如果你想实际创建一个在不和谐中发送消息的函数(没有不和谐机器人,我不明白你为什么需要它)你可以看看我的@987654321 @
  • 我会试试你的解决方案,非常感谢!
  • 这行得通!!!!谢谢????

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


【解决方案1】:

只需在主文件中的 bot.run 命令之前或之后创建任务即可。

async def send_msg(word):
    await bot.wait_until_ready()
    ch=bot.get_channel(00000000000000)
    print(ch)
    await ch.send("The word "+word+" has been spoken :)")

bot.loop.create_task(send_msg(word))
bot.run()

我建议提前做

编辑(抱歉没有仔细检查): 我刚刚注意到您的函数在主文件中,但是您将其导入到另一个不是主文件的文件中?您可以为此使用齿轮

【讨论】:

  • 非常感谢,但这不会执行功能吗?
  • 是的,它会执行这个功能。
  • 好的,但我只想在 send_msg.py 调用它时运行该函数。你有别的想法吗?
  • 你想在机器人启动时调用它吗?您可以只导入 send_message 函数,然后在主文件中创建任务
  • 等等,我想我看到了你的问题,让我快速编辑一下
猜你喜欢
  • 1970-01-01
  • 2022-10-15
  • 2019-01-06
  • 1970-01-01
  • 2020-08-16
  • 2021-02-22
  • 2021-02-28
  • 2020-07-17
  • 1970-01-01
相关资源
最近更新 更多