I am using Python with discord.py
. Documentation here
I\'ve got a bot that is running on a Discord server that links the server with a subr
You want your search_submissions()
function to be async so other functions of your bot can still be invoked and your bot stays responsive. Define it to be def async
and use aiohttp
to send async HTTP requests to reddit -- what this does is send off the request, relinquish control to the event loop, and then take back control once the results have been transmitted back. If you use a standard HTTP library here instead then your whole bot will be blocked until the result comes back. This of course only makes sense if the task is mainly I/O-bound and less CPU-bound.
Then call search_submissions()
in on_message(message)
-- but call it asynchronously using result = await search_submissions()
. This will resume execution of on_message
once the result of search_submissions
is ready.
If you truly want to do something else in the same context while waiting on search_submissions
(which I think is unlikely), dispatch it as task = asyncio.create_task(search_submissions())
. This will start the task immediately and allow you to do something else within the same function. Once you need the result you will have to result = await task
.
async def search_submissions():
async with aiohttp.ClientSession() as session:
async with session.get(some_reddit_url) as response:
return await response.read()
@client.event
async def on_message(message):
if message.content.startswith("reddit!hot"):
result = await search_submissions()
await message.channel.send(result)
The other answers here don't take into account discord.py's helpful tasks.loop
decorator.
To make an event occur every 5 seconds, you would use
from discord.ext import tasks, commands
class MyCog(commands.Cog):
def __init__(self):
self.foo.start()
def cog_unload(self):
self.printer.cancel()
@tasks.loop(seconds=5.0)
async def foo(self):
print('bar')
More can be found here: https://discordpy.readthedocs.io/en/latest/ext/tasks/
You can add a function to the bot event loop with Client.loop.create_task(search_submissions())
like this:
async def search_submissions():
pass
client = discord.Client()
client.loop.create_task(search_submissions())
client.run(TOKEN)
Update:
If you want your function to continue working you can put it in a while loop with some sleeping in between:
async def search_submissions():
while(true):
# do your stuff
await asyncio.sleep(1)