An asynchronous Python client for Discord's API
Project description
restcord.py
An asynchronous Python client for Discord's REST API.
You can aquire an application token from Discord's developer portal but please take care to read through Discord's developer terms of service and policy document as you agree to use this library in accordance with these terms.
Find Discord's REST API documentation here.
Requirements
python>=3.6.0
asyncio
aiohttp
json
Install
pip3 install restcord.py
Import
from restcord import RestCord
from restcord.errors import (
BadRequest,
Forbidden,
NotFound,
RateLimited,
InternalServerError,
BadGateway
)
Initialise
You can initialise the RestCord client with an application token, but you may also provide your own asyncio event loop, aiohttp client session.
client = RestCord("Your Discord application token here")
Structure
RestCord is structured in the same way as Discord's REST API documentation. Once you have initialised RestCord you will have access to channel_client, emoji_client, guild_client, user_client and voice_client objects, which contain asynchronous methods to communicate with API end points.
Example: get a guild object
In this example, we use RestCord to perform a GET request to Discord's guild API end point, which will return a guild object. Printing this object will output some basic details about the guild such as id and name but the object contains fields as per the documentation.
try:
async with client as rc:
guild = await rc.guild_client.get_guild(guild_id=265561352683126786)
print(guild)
except Forbidden as ex:
print(ex)
AsyncContextManager
RestCord can be used with or without the AsyncContextManager. Using it will ensure that any open aiohttp client sessions are closed.
async with client as rc:
voice_regions = await rc.voice_client.get_voice_regions()
Or more granularly:
async with client.guild_client as gc:
channel = await gc.get_channel(265586371178135562)
print(channel)
If you do not use the AsyncContextManager, you must call the close() function on RestCord to close any open aiohttp client sessions.
member = await client.guild_client.get_member(265561352683126786, 50527603626344448)
await client.close()
Errors
If Discord return error HTTP status codes, RestCord will throw a relevent exception for you to handle in your own way.
Example: add a reaction to a message
In this example we try to add a reaction to a message. There are several things to be aware of here:
-
Does our application have permission to add reactions to messages? If not, a Forbidden exception will be thrown.
-
Have we URL encoded the emoji as per specified by the documentation. If not, a BadRequest exception will be thrown.
-
This particular end point has strict rate limits. If we have already added a reaction to the message we may be rate limited. In this case a RateLimited exception will be thrown and you can get more information about that limit from the exception object.
try:
async with client.channel_client as cc:
await cc.add_reaction(331893934454472707, 736436235140333599, emoji="msq:285508293596807168")
except Forbidden as ex:
print(ex)
except BadRequest as ex:
print(ex)
except RateLimited as ex:
if ex.is_global:
print(f'Global rate limit has been hit. Retry in {ex.retry_after:.2f} seconds.')
else:
print(f'Rate limit hit. Retry in {ex.retry_after:.2f} seconds.')
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file restcord.py-0.0.6.tar.gz
.
File metadata
- Download URL: restcord.py-0.0.6.tar.gz
- Upload date:
- Size: 16.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d1875aa65edd6477935d415dde860f2f33d9ff331218113c27b862c90b872c5e |
|
MD5 | d81c35cf4e376edcdacda3fc9b7abf48 |
|
BLAKE2b-256 | 96c5f4b53b844ca6eaf4de1ea35a2d704a569a045faf5fe3a9e60cfbe7f24af6 |
File details
Details for the file restcord.py-0.0.6-py3-none-any.whl
.
File metadata
- Download URL: restcord.py-0.0.6-py3-none-any.whl
- Upload date:
- Size: 24.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52413b0424133a9db756329da4acc1774ef7e3bd042cbbf91c62c7db6f52ff56 |
|
MD5 | 2ae27abf78317905d20eb53a752bff16 |
|
BLAKE2b-256 | 21622bfb79b7282138b498f4323e13e46ead53e48717384f756e532517af1bf8 |