Skip to main content

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.

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, Emoji, Guild, User and Voice 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.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

restcord.py-0.0.5.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

restcord.py-0.0.5-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

Details for the file restcord.py-0.0.5.tar.gz.

File metadata

  • Download URL: restcord.py-0.0.5.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

File hashes

Hashes for restcord.py-0.0.5.tar.gz
Algorithm Hash digest
SHA256 51003f83f49a77836254c05e6d11a8feb8cb06754165767f17404a8a6e6794d0
MD5 1c24911ee7ad2c847208d079da05079e
BLAKE2b-256 d9680640cabd0b542adab8a1e7024d5a9e29c1f16780d8d222ddf2a7b425c3f6

See more details on using hashes here.

File details

Details for the file restcord.py-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: restcord.py-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 19.9 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/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

File hashes

Hashes for restcord.py-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 92130347e9955ab55b18ff59e53465bc961b2d7ca7f427efa1172d5174deddd5
MD5 b7cac16984539b8906aec10e3ca5ce93
BLAKE2b-256 0afe7fd1c4972d2947e76b5aefde01a01b15a54d65217f481e0e98303ef9a265

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page