Browse Source

Make event based handlers first class

Change Client.event decorator to assign the event handler function to
the instance it self and levarage dispatch to handle calling these
event.  Remove old _invoke_event method.
pull/9/head
Hornwitser 10 years ago
parent
commit
68c3fde089
  1. 34
      discord/client.py

34
discord/client.py

@ -357,24 +357,6 @@ class Client(object):
self.connection = ConnectionState(self.dispatch, **kwargs) self.connection = ConnectionState(self.dispatch, **kwargs)
self.dispatch_lock = threading.RLock() self.dispatch_lock = threading.RLock()
self.token = '' self.token = ''
self.events = {
'on_ready': _null_event,
'on_disconnect': _null_event,
'on_error': self.on_error,
'on_response': _null_event,
'on_message': _null_event,
'on_message_delete': _null_event,
'on_message_edit': _null_event,
'on_status': _null_event,
'on_channel_delete': _null_event,
'on_channel_create': _null_event,
'on_channel_update': _null_event,
'on_member_join': _null_event,
'on_member_remove': _null_event,
'on_member_update': _null_event,
'on_server_create': _null_event,
'on_server_delete': _null_event,
}
# the actual headers for the request... # the actual headers for the request...
# we only override 'authorization' since the rest could use the defaults. # we only override 'authorization' since the rest could use the defaults.
@ -448,17 +430,6 @@ class Client(object):
except Exception as e: except Exception as e:
getattr(self, 'on_error')(event_method, *args, **kwargs) getattr(self, 'on_error')(event_method, *args, **kwargs)
# Compatibility shim to old event system.
if event_method in self.events:
self._invoke_event(event_method, *args, **kwargs)
def _invoke_event(self, event_name, *args, **kwargs):
try:
log.info('attempting to invoke event {}'.format(event_name))
self.events[event_name](*args, **kwargs)
except Exception as e:
self.events['on_error'](event_name, *args, **kwargs)
def handle_socket_update(self, event, data): def handle_socket_update(self, event, data):
method = '_'.join(('handle', event.lower())) method = '_'.join(('handle', event.lower()))
getattr(self.connection, method)(data) getattr(self.connection, method)(data)
@ -739,10 +710,7 @@ class Client(object):
print('Ready!') print('Ready!')
""" """
if function.__name__ not in self.events: setattr(self, function.__name__, function)
raise InvalidEventName('The function name {} is not a valid event name'.format(function.__name__))
self.events[function.__name__] = function
log.info('{0.__name__} has successfully been registered as an event'.format(function)) log.info('{0.__name__} has successfully been registered as an event'.format(function))
return function return function

Loading…
Cancel
Save