From 0da04acc4ec92a8374d4eb93f687b1ea95554814 Mon Sep 17 00:00:00 2001 From: Mallory Hancock Date: Mon, 16 Sep 2019 12:27:43 -0700 Subject: [PATCH] make commands entirely class-based, enhance help --- pinhook/bot.py | 20 ++++++++++++---- pinhook/plugin.py | 59 ++++++++++++++++++++++++----------------------- 2 files changed, 45 insertions(+), 34 deletions(-) diff --git a/pinhook/bot.py b/pinhook/bot.py index 2df123c..c8521b8 100644 --- a/pinhook/bot.py +++ b/pinhook/bot.py @@ -34,6 +34,11 @@ class Bot(irc.bot.SingleServerIRCBot): self.start_logging(self.log_level) self.output_message = pinhook.plugin.message self.output_action = pinhook.plugin.action + self.internal_commands = { + self.cmd_prefix + 'join': 'join a channel', + self.cmd_prefix + 'quit': 'force the bot to quit', + self.cmd_prefix + 'reload': 'force bot to reload all plugins' + } self.load_plugins() class Message: @@ -130,7 +135,12 @@ class Bot(irc.bot.SingleServerIRCBot): self.process_event(c, e) def call_help(self, op): - helplist = sorted([i for i in pinhook.plugin.cmds if op or not ('ops' in pinhook.plugin.cmds[i] and pinhook.plugin.cmds[i]['ops'])]) + cmds = [i for i in pinhook.plugin.cmds if not pinhook.plugin.cmds[i].ops] + cmds.append(self.cmd_prefix + 'help') + if op: + cmds += [i for i in pinhook.plugin.cmds if pinhook.plugin.cmds[i].ops] + cmds += [i for i in self.internal_commands] + helplist = sorted(cmds) msg = ', '.join(helplist) return self.output_message('Available commands: {}'.format(msg)) @@ -160,12 +170,12 @@ class Bot(irc.bot.SingleServerIRCBot): output = None if cmd in pinhook.plugin.cmds: try: - if 'ops' in pinhook.plugin.cmds[cmd] and nick not in self.ops: - if pinhook.plugin.cmds[cmd]['ops_msg']: - output = self.output_message(pinhook.plugin.cmds[cmd]['ops_msg']) + if pinhook.plugin.cmds[cmd].ops and nick not in self.ops: + if pinhook.plugin.cmds[cmd].ops_msg: + output = self.output_message(pinhook.plugin.cmds[cmd].ops_msg) else: self.logger.debug('executing {}'.format(cmd)) - output = pinhook.plugin.cmds[cmd]['run'](self.Message( + output = pinhook.plugin.cmds[cmd].run(self.Message( bot=self, channel=chan, cmd=cmd, diff --git a/pinhook/plugin.py b/pinhook/plugin.py index 614b214..bfd8862 100644 --- a/pinhook/plugin.py +++ b/pinhook/plugin.py @@ -1,4 +1,3 @@ -from abc import ABC, abstractmethod from enum import Enum from functools import wraps @@ -24,30 +23,34 @@ class Output: return msg -class Command(ABC): - def __init__(self, **kwargs): - self.cmd = kwargs.get('cmd') - self.help = kwargs.get('help', '') +class Command: + def __init__(self, cmd, **kwargs): + self.cmd = cmd + self.help_txt = kwargs.get('help_txt', '') self.ops = kwargs.get('ops', False) self.ops_msg = kwargs.get('ops_msg', '') self.enabled = True - self.add_command() + self.run = kwargs.get('run', self.run) - @abstractmethod def run(self, msg): pass + def enable_ops(self, ops_msg): + self.ops = True + self.ops_msg = ops_msg + + def update_plugin(self, **kwargs): + self.help_text = kwargs.get('help_text') + self.run = kwargs.get('run', self.run) + def add_command(self): - cmds[self.cmd] = { - 'run': self.run, - 'help': self.help, - 'enabled': self.enabled, - } - if self.ops: - cmds[self.cmd].update({ - 'ops': self.ops, - 'ops_msg': self.ops_msg, - }) + cmds[self.cmd] = self + + def enable(self): + self.enabled = True + + def disable(self): + self.enabled = False def __str__(self): return self.cmd @@ -61,21 +64,19 @@ def message(msg): return Output(OutputType.Message, msg) -def _add_plugin(command, help_text, func): +def _add_command(command, help_text, func): if command not in cmds: - cmds[command] = {} - cmds[command].update({ - 'run': func, - 'help': help_text - }) + cmds[command] = Command(command, help_txt=help_text, run=func) + else: + cmds[command].update_plugin(help_text=help_text, run=func) + print(cmds) + def _ops_plugin(command, ops_msg, func): if command not in cmds: - cmds[command] = {} - cmds[command].update({ - 'ops': True, - 'ops_msg': ops_msg, - }) + cmds[command] = Command(command, ops=True, ops_msg=ops_msg) + else: + cmds[command].enable_ops(ops_msg) def _add_listener(name, func): @@ -90,7 +91,7 @@ def clear_plugins(): def register(command, help_text=None): @wraps(command) def register_for_command(func): - _add_plugin(command, help_text, func) + _add_command(command, help_text, func) return func return register_for_command