make commands entirely class-based, enhance help

pull/74/head
Mallory Hancock 2019-09-16 12:27:43 -07:00
parent 4ae8753095
commit 0da04acc4e
2 changed files with 45 additions and 34 deletions

View File

@ -34,6 +34,11 @@ class Bot(irc.bot.SingleServerIRCBot):
self.start_logging(self.log_level) self.start_logging(self.log_level)
self.output_message = pinhook.plugin.message self.output_message = pinhook.plugin.message
self.output_action = pinhook.plugin.action 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() self.load_plugins()
class Message: class Message:
@ -130,7 +135,12 @@ class Bot(irc.bot.SingleServerIRCBot):
self.process_event(c, e) self.process_event(c, e)
def call_help(self, op): 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) msg = ', '.join(helplist)
return self.output_message('Available commands: {}'.format(msg)) return self.output_message('Available commands: {}'.format(msg))
@ -160,12 +170,12 @@ class Bot(irc.bot.SingleServerIRCBot):
output = None output = None
if cmd in pinhook.plugin.cmds: if cmd in pinhook.plugin.cmds:
try: try:
if 'ops' in pinhook.plugin.cmds[cmd] and nick not in self.ops: if pinhook.plugin.cmds[cmd].ops and nick not in self.ops:
if pinhook.plugin.cmds[cmd]['ops_msg']: if pinhook.plugin.cmds[cmd].ops_msg:
output = self.output_message(pinhook.plugin.cmds[cmd]['ops_msg']) output = self.output_message(pinhook.plugin.cmds[cmd].ops_msg)
else: else:
self.logger.debug('executing {}'.format(cmd)) self.logger.debug('executing {}'.format(cmd))
output = pinhook.plugin.cmds[cmd]['run'](self.Message( output = pinhook.plugin.cmds[cmd].run(self.Message(
bot=self, bot=self,
channel=chan, channel=chan,
cmd=cmd, cmd=cmd,

View File

@ -1,4 +1,3 @@
from abc import ABC, abstractmethod
from enum import Enum from enum import Enum
from functools import wraps from functools import wraps
@ -24,30 +23,34 @@ class Output:
return msg return msg
class Command(ABC): class Command:
def __init__(self, **kwargs): def __init__(self, cmd, **kwargs):
self.cmd = kwargs.get('cmd') self.cmd = cmd
self.help = kwargs.get('help', '') self.help_txt = kwargs.get('help_txt', '')
self.ops = kwargs.get('ops', False) self.ops = kwargs.get('ops', False)
self.ops_msg = kwargs.get('ops_msg', '') self.ops_msg = kwargs.get('ops_msg', '')
self.enabled = True self.enabled = True
self.add_command() self.run = kwargs.get('run', self.run)
@abstractmethod
def run(self, msg): def run(self, msg):
pass 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): def add_command(self):
cmds[self.cmd] = { cmds[self.cmd] = self
'run': self.run,
'help': self.help, def enable(self):
'enabled': self.enabled, self.enabled = True
}
if self.ops: def disable(self):
cmds[self.cmd].update({ self.enabled = False
'ops': self.ops,
'ops_msg': self.ops_msg,
})
def __str__(self): def __str__(self):
return self.cmd return self.cmd
@ -61,21 +64,19 @@ def message(msg):
return Output(OutputType.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: if command not in cmds:
cmds[command] = {} cmds[command] = Command(command, help_txt=help_text, run=func)
cmds[command].update({ else:
'run': func, cmds[command].update_plugin(help_text=help_text, run=func)
'help': help_text print(cmds)
})
def _ops_plugin(command, ops_msg, func): def _ops_plugin(command, ops_msg, func):
if command not in cmds: if command not in cmds:
cmds[command] = {} cmds[command] = Command(command, ops=True, ops_msg=ops_msg)
cmds[command].update({ else:
'ops': True, cmds[command].enable_ops(ops_msg)
'ops_msg': ops_msg,
})
def _add_listener(name, func): def _add_listener(name, func):
@ -90,7 +91,7 @@ def clear_plugins():
def register(command, help_text=None): def register(command, help_text=None):
@wraps(command) @wraps(command)
def register_for_command(func): def register_for_command(func):
_add_plugin(command, help_text, func) _add_command(command, help_text, func)
return func return func
return register_for_command return register_for_command