How to integrate async handler in Redis subscribe method?

Viewed 390

I'm trying to send a message to discord when a Redis message has been received. This is the warning from the terminal and discord doesn't send the message:

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

Here is my full code example:

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')

What is the correct approach to integrate async handler to Redis subscribe method?

I don't think I can use the two other approaches as mentioned in Redis documentation since this may have some side effects in the discord client such as on_message method which I've omitted in the example code above.

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

or

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