From 3a148ae34f636ad1b85d4c0e7da86ace89a83123 Mon Sep 17 00:00:00 2001 From: Mallory Hancock Date: Fri, 13 Oct 2017 15:22:47 -0700 Subject: [PATCH 1/3] add error exceptions for calling commands and for loading plugins --- pinhook/bot.py | 51 +++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/pinhook/bot.py b/pinhook/bot.py index 6fd7167..ddd20e3 100644 --- a/pinhook/bot.py +++ b/pinhook/bot.py @@ -26,20 +26,8 @@ class Bot(irc.bot.SingleServerIRCBot): irc.bot.SingleServerIRCBot.__init__(self, [(server, self.port)], nickname, nickname) self.chanlist = channels self.bot_nick = nickname + self.load_plugins() - # load all plugins - plugins = [] - for m in os.listdir(self.plugin_dir): - if m.endswith('.py'): - name = m[:-3] - fp, pathname, description = imp.find_module(name, [self.plugin_dir]) - plugins.append(imp.load_module(name, fp, pathname, description)) - - # gather all commands - self.cmds = {} - for plugin in plugins: - for cmd in plugin.pinhook.plugin.cmds: - self.cmds[cmd['cmd']] = cmd['func'] def set_kwargs(self, **kwargs): kwarguments = { @@ -56,6 +44,24 @@ class Bot(irc.bot.SingleServerIRCBot): if a not in kwargs: setattr(self, a, kwarguments[a]) + def load_plugins(self): + # load all plugins + plugins = [] + for m in os.listdir(self.plugin_dir): + if m.endswith('.py'): + try: + name = m[:-3] + fp, pathname, description = imp.find_module(name, [self.plugin_dir]) + p = imp.load_module(name, fp, pathname, description) + p.pinhook + plugins.append(p) + except Exception as e: + print(e) + # gather all commands + self.cmds = {} + for plugin in plugins: + for cmd in plugin.pinhook.plugin.cmds: + self.cmds[cmd['cmd']] = cmd['func'] def on_welcome(self, c, e): if self.ns_pass: @@ -92,14 +98,17 @@ class Bot(irc.bot.SingleServerIRCBot): msg = ', '.join(helplist) c.privmsg(chan, 'Available commands: {}'.format(msg)) elif cmd in self.cmds: - output = self.cmds[cmd](Message( - channel=chan, - cmd=cmd, - nick=nick, - arg=arg, - botnick=self.bot_nick, - ops=self.ops - )) + try: + output = self.cmds[cmd](Message( + channel=chan, + cmd=cmd, + nick=nick, + arg=arg, + botnick=self.bot_nick, + ops=self.ops + )) + except Exception as e: + print(e) if output: if output.msg_type == 'message': From 1be0f4798333215a547b203f8511714b75ddbf24 Mon Sep 17 00:00:00 2001 From: Mallory Hancock Date: Mon, 16 Oct 2017 10:34:35 -0700 Subject: [PATCH 2/3] add reload command to detect plugin changes --- pinhook/bot.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pinhook/bot.py b/pinhook/bot.py index ddd20e3..12a1157 100644 --- a/pinhook/bot.py +++ b/pinhook/bot.py @@ -97,6 +97,9 @@ class Bot(irc.bot.SingleServerIRCBot): helplist = sorted([i for i in self.cmds]) msg = ', '.join(helplist) c.privmsg(chan, 'Available commands: {}'.format(msg)) + elif cmd == '!reload' and nick in self.ops: + self.load_plugins() + c.privmsg(chan, 'Plugins reloaded') elif cmd in self.cmds: try: output = self.cmds[cmd](Message( From 34b09a6869056ac85f096df9c5d6227c018ec189 Mon Sep 17 00:00:00 2001 From: Mallory Hancock Date: Mon, 16 Oct 2017 10:36:25 -0700 Subject: [PATCH 3/3] update version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 60fdd0a..284edbc 100755 --- a/setup.py +++ b/setup.py @@ -72,7 +72,7 @@ class UploadCommand(Command): # Where the magic happens: setup( name=NAME, - version='1.0.2', + version='1.1.0', description=DESCRIPTION, long_description=long_description, author=AUTHOR,