Discord bots built with the discord.py library run asynchronously by default. This allows your bot to perform multiple actions in parallel without blocking. However, making HTTP requests with the standard requests library is synchronous and will block the event loop.
This is where the aiohttp library comes in. aiohttp allows you to make HTTP requests asynchronously without blocking the bot. Here's a quick overview of how to use it properly in a Discord bot.
Installation
First, install aiohttp with pip:
pip install aiohttp
Then import it in your bot code:
import aiohttp
Creating a Session
You should create an aiohttp
import aiohttp
session = None
@bot.event
async def on_ready():
global session
session = aiohttp.ClientSession()
@bot.event
async def on_disconnect():
await session.close()
This session can be reused for all requests.
Making Requests
To make a GET request:
async with session.get(url) as response:
data = await response.text() # or json()
For POST, PUT, DELETE, etc. just use
The
Handling Exceptions
Wrap requests in try/except blocks to catch errors:
try:
async with session.get(url) as response:
data = await response.json()
except aiohttp.ClientConnectorError:
print("Connection error")
This catches network errors. Catch other exceptions like
Waiting for Requests
Since requests run in the background, you need to
data = await get_data(session)
embed = build_embed(data)
await channel.send(embed=embed)
If you don't await, the rest of the code keeps running before the request finishes.
Timeouts
To avoid a request getting stuck, use timeouts:
try:
async with session.get(url, timeout=10) as response:
data = await response.json()
except asyncio.TimeoutError:
print("Timeout exceeded")
This raises a TimeoutError if the request takes > 10 seconds.
Limits
Be careful not to open too many connections at once or you may hit rate limits or performance issues. Typically you should limit concurrent requests to 5-10 per domain. Consider using a Semaphore:
sem = asyncio.Semaphore(10)
async with sem:
async with session.get(url) as response:
data = await response.json()
This limits it to 10 concurrent requests.
Following Best Practices
That covers the basics! Additionally, follow general best practices like:
By properly leveraging aiohttp, you can make your Discord bots more efficient and concurrent.