-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiscordapi.py
38 lines (30 loc) · 1.27 KB
/
discordapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import requests
class DiscordAPI:
"""Discord API"""
endpoint = 'https://discord.com/api'
def __init__(self, TOKEN: str):
self.headers={'Authorization': f'Bot {TOKEN}'}
def _expandPathToUrl(self, path: str, params: dict = {}) -> str:
"""adds onto the `path` with additional query `params`"""
url = path
url += '?' if params else ''
return url + '&'.join([key + '=' + str(params[key]) for key in params])
def sendMessage(self, channel_id: str, body: dict) -> None:
requests.post(self.endpoint + f'/channels/{channel_id}/messages',
data=body, headers=self.headers)
return None
def getChannelMessages(self, channel_id: str, params: dict = {}, json=True) -> list:
"""valid params are `around`, `before`, `after`, `limit`"""
base_path = self.endpoint + f'/channels/{channel_id}/messages'
path = self._expandPathToUrl(base_path, params)
res = requests.get(path, headers=self.headers)
if json:
return res.json()
else:
return res
if __name__ == "__main__":
from secrets import TOKEN
api = DiscordAPI(TOKEN)
channel_id = '894438526580555817'
res = api.getChannelMessages(channel_id, {"limit": 5})
print(res)