errbotio / err-backend-discord

Backend for Discord for Errbot
GNU General Public License v3.0
23 stars 21 forks source link

Replying to a bot command only happens if discord sends another event #11

Closed shuhaowu closed 5 years ago

shuhaowu commented 7 years ago

I made a !ping command and I noticed that it will not send the reply until discord sends the bot some other event (for example, if you started typing and discord transmitted the typing event to the bot).

I suspect this has to do with the asyncio stuff that discordpy is using, specifically in the use of

self.client.loop.create_task(self.client.send_typing(recipient))
self.client.loop.create_task(self.client.send_message(destination=recipient, content=message))

Not quite sure how to fix it yet as I'm not too familiar with asyncio, tho.

shuhaowu commented 7 years ago

This is definitely not being scheduled correctly. I noticed that it only sends the message if either you get some event from discord, or the KeepAliveHandler in discord.py fires, which triggers a socket event.

This lead me to eventually look at how KeepAliveHandler works, and then I patched the code above with

asyncio.run_coroutine_threadsafe(self.client.send_typing(recipient), loop=self.client.loop)
asyncio.run_coroutine_threadsafe(self.client.send_message(destination=recipient, content=message), loop=self.client.loop)

This got rid of the lag problems.

Djiit commented 7 years ago

Hi @shuhaowu !

Nice one :) Would you mind sending a PR with your modifications ?

Is there any other place we should use this function ?