【问题标题】:inconsistent timing in async discord bot异步不和谐机器人中的时间不一致
【发布时间】:2020-07-18 02:08:06
【问题描述】:

我试图让一个不和谐的机器人每 30 分钟发送一条消息,我能想出的唯一解决方案变得非常奇怪,它会开始以看似随机的间隔发送消息并一次发送多个消息。

这里是受影响的代码:

@client.event
async def on_ready():
    await test()

async def test():       
    print("running test")
    channel = client.get_channel(PLACEHOLDER)
    await channel.send("i like " +words)
    print(words)
    await asyncio.sleep(1800)
    await test()
                 

我尝试用 time.sleep 替换 asyncio.sleep 但这会导致机器人超时并从 on_ready 和本身运行 test() 函数导致它发送两次消息,我会在一致的基础上.我和几个朋友已经搞砸了一段时间,无法修复它。

【问题讨论】:

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


    【解决方案1】:

    你可以使用discord.ext.task.loop:

    from discord.ext import task
    
    @client.event
    async def on_ready():
        print("Bot is ready")
        await test.start()
    
    @task.loop(minutes=30)
    async def test():
        print("30 minutes have passed")
    

    【讨论】:

    • ImportError: cannot import name 'task' from 'discord.ext' (unknown location) 我绝对不知道为什么这么说,我可以导入discord.ext import commands 就好了。编辑: 1. 导入是任务而不是任务。 2. 我必须输入task = discord.ext.tasks 才能让它工作。
    • 我必须将 test.start() 添加到 on_ready。但现在可以了,谢谢!
    猜你喜欢
    • 2018-03-20
    • 2023-03-31
    • 2021-02-28
    • 2020-07-03
    • 2020-09-23
    • 2021-10-30
    • 2021-08-05
    • 2021-05-27
    • 2021-07-15
    相关资源
    最近更新 更多