首页 > 解决方案 > discord.py 异步功能阻塞

问题描述

我不是异步东西的专家,但我不明白这一点。我正在使用 TwitterAPI 为新帖子获取 Twitter API 流端点。我设置了频道,它应该会收听新帖子,而且效果很好。但是为什么我不能在 set_channel 函数运行时使用我的 foo 命令呢?我认为它是异步的......我可能理解完全错误的东西,无论如何。帮助将不胜感激。

@bot.command()
async def set_channel(ctx):
    while True:
        try:
            iterator = api.request(
                'statuses/filter', {'follow': user_ids}).get_iterator()
            for item in iterator:
                if 'text' in item:
                    await ctx.channel.send(item['text'])
                elif 'disconnect' in item:
                    event = item['disconnect']
                    if event['code'] in [2, 5, 6, 7]:
                        raise Exception(event['reason'])
                    else:
                        break
        except TwitterRequestError as e:
            if e.status_code < 500:
                raise
            else:
                pass
        except TwitterConnectionError:
            pass


@bot.command()
async def foo(ctx):
    await ctx.channel.send("bar")

```

标签: pythonasynchronouspython-asynciodiscord.py

解决方案


推荐阅读