Browse Source

Cleanup, global rate limit support, fix register_command, etc

pull/3/head
Andrei 9 years ago
parent
commit
bd75deb29a
  1. 10
      disco/api/client.py
  2. 8
      disco/api/ratelimit.py
  3. 2
      disco/bot/plugin.py
  4. 1
      disco/gateway/events.py
  5. 4
      disco/types/base.py

10
disco/api/client.py

@ -9,10 +9,20 @@ from disco.types.invite import Invite
def optional(**kwargs):
"""
Takes a set of keyword arguments, creating a dictionary with only the non-
null values.
:returns: dict
"""
return {k: v for k, v in kwargs.items() if v is not None}
class APIClient(LoggingClass):
"""
An abstraction over the :class:`HTTPClient` that composes requests, and fits
the models with the returned data.
"""
def __init__(self, client):
super(APIClient, self).__init__()

8
disco/api/ratelimit.py

@ -47,17 +47,23 @@ class RateLimiter(object):
self.states = {}
def check(self, route, timeout=None):
return self._check(None, timeout) and self._check(route, timeout)
def _check(self, route, timeout=None):
if route in self.states:
# If we're current waiting, join the club
if self.states[route].chilled:
return self.states[route].wait(timeout)
if self.states[route].next_will_ratelimit():
self.states[route].cooldown()
gevent.spawn(self.states[route].cooldown).wait(timeout)
return True
def update(self, route, request):
if 'X-RateLimit-Global' in request.headers:
route = None
if route in self.states:
self.states[route].update(request)
else:

2
disco/bot/plugin.py

@ -112,7 +112,7 @@ class Plugin(LoggingClass, PluginDeco):
def register_command(self, func, *args, **kwargs):
wrapped = functools.partial(self._dispatch, 'command', func)
self.commands[func.__name__] = Command(self, func, *args, **kwargs)
self.commands[func.__name__] = Command(self, wrapped, *args, **kwargs)
def destroy(self):
map(lambda k: k.remove(), self._events)

1
disco/gateway/events.py

@ -5,6 +5,7 @@ from disco.util import skema_find_recursive_by_type
from disco.types import Guild, Channel, User, GuildMember, Role, Message, VoiceState
# TODO: clean this... use BaseType, etc
class GatewayEvent(skema.Model):
@staticmethod
def from_dispatch(client, data):

4
disco/types/base.py

@ -19,8 +19,8 @@ class BaseType(skema.Model):
# Valdiate
obj.validate()
for item in skema_find_recursive_by_type(obj, skema.ModelType):
item.client = client
for field, value in skema_find_recursive_by_type(obj, skema.ModelType):
value.client = client
obj.client = client
return obj

Loading…
Cancel
Save