Browse Source

feature -Add some logging to Bot, allow for handling of command/event exceptions

feature/docs
Andrei 8 years ago
parent
commit
48c8fa6fa9
  1. 6
      disco/bot/bot.py
  2. 7
      disco/bot/plugin.py

6
disco/bot/bot.py

@ -12,6 +12,7 @@ from disco.bot.plugin import Plugin
from disco.bot.command import CommandEvent, CommandLevels
from disco.bot.storage import Storage
from disco.util.config import Config
from disco.util.logging import LoggingClass
from disco.util.serializer import Serializer
@ -87,7 +88,7 @@ class BotConfig(Config):
storage_config = {}
class Bot(object):
class Bot(LoggingClass):
"""
Disco's implementation of a simple but extendable Discord bot. Bots consist
of a set of plugins, and a Disco client.
@ -380,6 +381,7 @@ class Bot(object):
unload.
"""
if cls.__name__ in self.plugins:
self.log.warning('Attempted to add already added plugin %s', cls.__name__)
raise Exception('Cannot add already added plugin: {}'.format(cls.__name__))
if not config:
@ -431,7 +433,7 @@ class Bot(object):
"""
Adds and loads a plugin, based on its module path.
"""
self.log.info('Adding plugin module at path "%s"', path)
mod = importlib.import_module(path)
loaded = False

7
disco/bot/plugin.py

@ -205,6 +205,9 @@ class Plugin(LoggingClass, PluginDeco):
when, typ = meta['type'].split('_', 1)
self.register_trigger(typ, when, member)
def handle_exception(self, greenlet, event):
pass
def spawn_wrap(self, spawner, method, *args, **kwargs):
def wrapped(*args, **kwargs):
self.ctx['plugin'] = self
@ -245,9 +248,13 @@ class Plugin(LoggingClass, PluginDeco):
getattr(self, '_' + when)[typ].append(func)
def dispatch(self, typ, func, event, *args, **kwargs):
# Link the greenlet with our exception handler
gevent.getcurrent().link_exception(lambda g: self.handle_exception(g, event))
# TODO: this is ugly
if typ != 'command':
self.greenlets.add(gevent.getcurrent())
self.ctx['plugin'] = self
if hasattr(event, 'guild'):

Loading…
Cancel
Save