【问题标题】:sending high and low priority messages with a discord bot使用不和谐机器人发送高优先级和低优先级消息
【发布时间】:2021-05-14 06:09:39
【问题描述】:

Discord 将机器人限制为每台服务器每 5 秒执行 5 次操作 - 因此,机器人尽可能快地响应紧急事务非常重要,即使这意味着降低其他事情的优先级。

我第一次尝试处理这个是这两个命令(特别是第一个使用时间模块)

@bot.command()
async def low_priority_count(ctx, num: int):

    for i in range(num):
        time.sleep(1)
        await ctx.send("low priority "+str(i))

@bot.command()
async def high_priority_count(ctx, num: int):

    for i in range(num):
        await ctx.send("high priority "+ str(i))
bot.run(TOKEN)

我的想法是,如果您调用 $high_priority_count 20,则循环将快速发送包含数字 0-19 的消息,尽管 discord 需要一些时间才能将它们打印出来。因此,我预计如果您调用 $low_priority_count 20,您会在高优先级消息之后收到低优先级消息。

相反,您可以将它们夹杂在一起。这是文本频道上消息的附加版本:

User:      $high_priority_count 20
Bot:       high priority 0
           high priority 1
           high priority 2
           ...
           high priority 9
User:      $low_priority_count 20
Bot:       high priority 10
           low priority 0
           high priority 11
           low priority 1
           high priority 12
           low priority 2
           ...
           high priority 19
           low priority 9
           low priority 10
           low priority 11
           low priority 12
           ...
           low priority 20

为什么机器人会一起发送消息?有没有办法确保在发送低优先级消息之前先发送高优先级消息?

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    您正在使用 time.sleep 这是一个阻塞函数,您不应该在异步代码中使用它。如果您使用asyncio.sleep,一切都应该按您的预期工作

    import asyncio
    
    @bot.command()
    async def low_priority_count(ctx, num: int):
        for i in range(num):
            await asyncio.sleep(1)
            await ctx.send("low priority "+str(i))
    
    
    @bot.command()
    async def high_priority_count(ctx, num: int):
        for i in range(num):
            await ctx.send("high priority "+ str(i))
    

    您还应该在high_priority_count 命令中添加延迟,在测试代码时,我已经多次达到速率限制。

    编辑:

    回答你的评论,这个比较复杂,你可以使用Synchronization Primitivesasyncio.Semaphore 应该可以工作

    bot.priority_semaphore = asyncio.Semaphore()
    
    @bot.command()
    async def high_priority_count(ctx, count: int):
        await bot.priority_semaphore.acquire()
        for i in range(count):
            await ctx.send(f"High priority {i}")
    
        bot.priority_semaphore.release()
    
    
    @bot.command()
    async def low_priority_count(ctx, count: int):
        await bot.priority_semaphore.acquire():
        for i in range(count):
            await asyncio.sleep(1)
            await ctx.send(f"High priority {i}")
    
        bot.priority_semaphore.release()
    

    遗憾的是,如果您先调用low_priority_count 命令,它将首先发送低优先级消息,如果首先调用低优先级消息,我想不出一种首先发送高优先级消息的方法。

    【讨论】:

    • 谢谢,这似乎有帮助,但不能解决问题。使用该代码,对于通过的每个低优先级消息,我都会收到更多的高优先级消息,但令人惊讶的是,仍然存在混合。这是来自文本频道的消息,高优先级缩写为 hp,低优先级缩写为 lp
    • 用户:$high_priority_count 20 机器人:hp 0 hp 1 hp 2 hp 3 hp 4 用户:$low_priority_count 20 机器人:hp 5 lp 0 hp 6 hp 7 hp 8 hp 9 lp 1 hp 10 hp 11马力 12 马力 13 升 2 马力 14 马力 15 马力 16 马力 17 升 3 马力 18 马力 19 升 4 升 5 升 6 升 7 ...
    • 哦,所以您想先发送高优先级的消息,然后再发送低优先级的消息?我以为你想让它们交错,让我编辑我的答案
    • 非常感谢 - 如果出现我必须暂停一个命令而另一个命令运行的情况,我会问另一个问题!
    • 好的,如果需要帮助也可以私信我,kwieeciol#6035
    猜你喜欢
    • 2020-12-21
    • 1970-01-01
    • 2015-02-11
    • 2012-05-31
    • 2012-09-04
    • 2015-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多