Skip to main content

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.

Codacy Badge PyPI version Python 3.6

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

Metadata

Release files for restcord.py 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for restcord.py 0.0.6
File Size Uploaded
restcord.py-0.0.6.tar.gz 16.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for restcord.py 0.0.6
File Interpreter ABI Platform
restcord.py-0.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 41.4 kB

Release files / restcord.py-0.0.6.tar.gz

Download URL restcord.py-0.0.6.tar.gz
Size 16.7 kB
Tags Source
SHA-256 checksum
How to use checksums
d1875aa65edd6477935d415dde860f2f33d9ff331218113c27b862c90b872c5e
BLAKE2b-256 checksum
How to use checksums
96c5f4b53b844ca6eaf4de1ea35a2d704a569a045faf5fe3a9e60cfbe7f24af6
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / restcord.py-0.0.6-py3-none-any.whl

Download URL restcord.py-0.0.6-py3-none-any.whl
Size 24.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
52413b0424133a9db756329da4acc1774ef7e3bd042cbbf91c62c7db6f52ff56
BLAKE2b-256 checksum
How to use checksums
21622bfb79b7282138b498f4323e13e46ead53e48717384f756e532517af1bf8
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.0.6 This release

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page