diff options
-rw-r--r-- | discord/user.py | 4 | ||||
-rw-r--r-- | discord/utils/exceptions.py | 3 | ||||
-rw-r--r-- | discord/utils/rest.py | 60 | ||||
-rw-r--r-- | requirements.txt | 3 |
4 files changed, 69 insertions, 1 deletions
diff --git a/discord/user.py b/discord/user.py index 16ddf1b..6000b31 100644 --- a/discord/user.py +++ b/discord/user.py @@ -4,11 +4,15 @@ class User: "username", "discriminator", "avatar", + "avatar_decoration" "bot", "system", "mfa_enabled", "banner", + "banner_color", "accent_color", + "bio", + "pronouns", "locale", "verified", "email", diff --git a/discord/utils/exceptions.py b/discord/utils/exceptions.py new file mode 100644 index 0000000..a7f035c --- /dev/null +++ b/discord/utils/exceptions.py @@ -0,0 +1,3 @@ +class APIException(Exception): + """Raised when the Discord API returns an error.""" + diff --git a/discord/utils/rest.py b/discord/utils/rest.py new file mode 100644 index 0000000..f7f6ab8 --- /dev/null +++ b/discord/utils/rest.py @@ -0,0 +1,60 @@ +import aiohttp +import asyncio + +from discord.utils.exceptions import APIException + + +async def get(token, url): + async with aiohttp.ClientSession(headers={ + "Authorization": f"Bot {token}", + "User-Agent": f"DiscordBot (https://github.com/mounderfod/discobra 0.0.1)" + }) as session: + async with session.get(url='https://discord.com/api/v10' + url) as r: + data = await r.json() + match r.status: + case 200: + return data + case other: + raise APIException(data['message']) + + +async def post(token, url, data): + async with aiohttp.ClientSession(headers={ + "Authorization": f"Bot {token}", + "User-Agent": f"DiscordBot (https://github.com/mounderfod/discobra 0.0.1)" + }) as session: + async with session.post(url='https://discord.com/api/v10' + url, data=data) as r: + data = await r.json() + match r.status: + case 200 | 204: + return data + case other: + raise APIException(data['message']) + + +async def patch(token, url, data): + async with aiohttp.ClientSession(headers={ + "Authorization": f"Bot {token}", + "User-Agent": f"DiscordBot (https://github.com/mounderfod/discobra 0.0.1)" + }) as session: + async with session.patch(url='https://discord.com/api/v10' + url, data=data) as r: + data = await r.json() + match r.status: + case 200 | 204: + return data + case other: + raise APIException(data['message']) + + +async def delete(token, url): + async with aiohttp.ClientSession(headers={ + "Authorization": f"Bot {token}", + "User-Agent": f"DiscordBot (https://github.com/mounderfod/discobra 0.0.1)" + }) as session: + async with session.delete(url='https://discord.com/api/v10' + url) as r: + data = await r.json() + match r.status: + case 200: + return data + case other: + raise APIException(data['message']) diff --git a/requirements.txt b/requirements.txt index 6889a87..c047d67 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ websockets~=10.3 -setuptools~=60.2.0 \ No newline at end of file +setuptools~=60.2.0 +aiohttp~=3.8.1 \ No newline at end of file |