Skip to content
This repository was archived by the owner on Dec 22, 2019. It is now read-only.

Commit 047d4c9

Browse files
authored
Add fancy new error system
1 parent 21a908e commit 047d4c9

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

dbipyt/dbipyt.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
import aiohttp
2-
import json
2+
from .errors import InvalidEndpoint, InvalidToken, InvalidClientID
33

44
class Client:
55
def __init__(self, token):
66
self.auth = token
77

88
async def post(self, bot_id:str=None, guild_count:int=None):
9-
async with aiohttp.ClientSession(headers={'Authorization': self.auth}) as s:
9+
async with aiohttp.ClientSession(headers={'Authorization': 'e'}) as s:
1010
async with s.post(f"https://discordbotindex.com/apiv1/bot/{bot_id}", data={'server_count': guild_count}) as r:
1111
r = await r.json()
12-
return r
12+
if r == {"error": "unauthorized"}:
13+
raise InvalidToken("You provided an invalid token.")
14+
else:
15+
return r
1316

14-
#class Fetch:
1517
async def fetch(bot_id, endpoint:str=None):
1618
async with aiohttp.ClientSession() as s:
1719
async with s.get(f"https://discordbotindex.com/apiv1/bot/{bot_id}") as r:
1820
r = await r.json()
19-
if endpoint is None:
20-
try:
21-
return r
22-
except:
23-
return "[dbipyt.0.1.2] Invalid client ID. Please provide a client ID with the bot_id argument."
21+
if r == {"error":"unknown_bot"}:
22+
raise InvalidClientID(f"'{bot_id}' is not a valid ID. Please provide a correct client ID.")
23+
elif r == {"error":"param_must_be_int"}:
24+
raise InvalidClientID("Client IDs must be in int.")
25+
elif endpoint is None:
26+
return r
2427
else:
2528
try:
2629
return r[endpoint]
27-
except KeyError as e:
28-
return f"[dbipyt.0.1.2] Invalid endpoint: {e}"
30+
except KeyError as error:
31+
raise InvalidEndpoint(error)

0 commit comments

Comments
 (0)