如何在redis subscribe方法中集成异步处理程序?

y0u0uwnf  于 2021-06-07  发布在  Redis
关注(0)|答案(0)|浏览(228)

我正试图在收到redis消息后向discord发送消息。这是来自终端的警告,discord不发送消息:

chat-bot.py:18: RuntimeWarning: coroutine 'Messageable.send' was never awaited
  channel.send(message['data'])

下面是我的完整代码示例:

import discord
import redis

r = redis.Redis(host='localhost', port=6379, db=0)

channel_id = 123
client = discord.Client

class MyClient(discord.Client):

    async def on_ready(self):
        p = r.pubsub()
        p.subscribe(**{'bot-discord': self.send_message})
        self.thread = p.run_in_thread(sleep_time=0.001)

    async def send_message(self, message):
        channel = client.get_channel(channel_id)
        await channel.send(message['data'])

    # async def on_message(message):
    #     if message.author == client.user:
    #         return

    #     if message.content.startswith('$hello'):
    #         await message.channel.send('Hello!')

    def __del__(self):
        self.thread.stop()

if __name__ == '__main__':
    client = MyClient()
    client.run('abc')

将异步处理程序集成到redis subscribe方法的正确方法是什么?
我不认为我可以使用redis文档中提到的其他两种方法,因为这可能会对discord客户机产生一些副作用,比如在上面的示例代码中我省略了on_消息方法。

>> while True:
>>>     message = p.get_message()
>>>     if message:
>>>         # do something with the message
>>>     time.sleep(0.001)  # be nice to the system :)

>>> for message in p.listen():
...     # do something with the message

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题