Added/Cleaned up rest methods. Also added a widget getter. Implemented caching to some endpoints

This commit is contained in:
Jeremy Zhang 2017-03-23 23:52:32 -07:00
parent 9e096fe930
commit d3e04fec5f

View File

@ -2,8 +2,11 @@ import requests
import sys import sys
import time import time
import json import json
from cachetools import cached, TTLCache
_DISCORD_API_BASE = "https://discordapp.com/api/v6" _DISCORD_API_BASE = "https://discordapp.com/api/v6"
get_widget_cache = TTLCache(200, 300)
guild_list_cache = TTLCache(1, 300) # Pop the list if new guilds are added
def json_or_text(response): def json_or_text(response):
text = response.text text = response.text
@ -81,6 +84,63 @@ class DiscordREST:
'content': json_or_text(req), 'content': json_or_text(req),
} }
#####################
# Channel
#####################
def get_channel_messages(self, channel_id, after_snowflake=None):
_endpoint = "/channels/{channel_id}/messages".format(channel_id=channel_id)
params = {}
if after_snowflake is not None:
params = {'after': after_snowflake}
r = self.request("GET", _endpoint, params=params)
return r
def create_message(self, channel_id, content):
_endpoint = "/channels/{channel_id}/messages".format(channel_id=channel_id)
payload = {'content': content}
r = self.request("POST", _endpoint, data=payload)
return r
#####################
# Guild
#####################
def get_guild_channels(self, guild_id):
_endpoint = "/guilds/{guild_id}/channels".format(guild_id=guild_id)
r = self.request("GET", _endpoint)
return r
def get_guild_roles(self, guild_id):
_endpoint = "/guilds/{guild_id}/roles".format(guild_id=guild_id)
r = self.request("GET", _endpoint)
return r
def get_guild_member(self, guild_id, user_id):
_endpoint = "/guilds/{guild_id}/members/{user_id}".format(guild_id=guild_id, user_id=user_id)
r = self.request("GET", _endpoint)
return r
def modify_guild_member(self, guild_id, user_id, **kwargs):
_endpoint = "/guilds/{guild_id}/members/{user_id}".format(guild_id=guild_id, user_id=user_id)
r = self.request("PATCH", _endpoint, data=kwargs, json=True)
return r
def get_guild_embed(self, guild_id):
_endpoint = "/guilds/{guild_id}/embed".format(guild_id=guild_id)
r = self.request("GET", _endpoint)
return r
def modify_guild_embed(self, guild_id, **kwargs):
_endpoint = "/guilds/{guild_id}/embed".format(guild_id=guild_id)
r = self.request("PATCH", _endpoint, data=kwargs, json=True)
return r
#####################
# User
#####################
@cached(guild_list_cache)
def get_all_guilds(self): def get_all_guilds(self):
_endpoint = "/users/@me/guilds" _endpoint = "/users/@me/guilds"
params = {} params = {}
@ -99,16 +159,15 @@ class DiscordREST:
count = 0 count = 0
return guilds return guilds
def get_channel_messages(self, channel_id, after_snowflake=None): #####################
_endpoint = "/channels/{channel_id}/messages".format(channel_id=channel_id) # Widget Handler
params = {} #####################
if after_snowflake is not None:
params = {'after': after_snowflake}
r = self.request("GET", _endpoint, params=params)
return r
def create_message(self, channel_id, content): @cached(get_widget_cache)
_endpoint = "/channels/{channel_id}/messages".format(channel_id=channel_id) def get_widget(self, guild_id):
payload = {'content': content} _endpoint = _DISCORD_API_BASE + "/servers/{guild_id}/widget.json".format(guild_id=guild_id)
r = self.request("POST", _endpoint, data=payload) embed = self.get_guild_embed(guild_id)
return r if not embed['content']['enabled']:
self.modify_guild_embed(guild_id, enabled=True, channel_id=guild_id)
widget = requests.get(_endpoint).json()
return widget