refactored internal commands structure, made .help only show one alias per function
This commit is contained in:
parent
bad47e346e
commit
5a7d90b0ba
39
bot.py
39
bot.py
|
@ -70,24 +70,12 @@ class Fulvia(irc.IRCClient):
|
|||
received.
|
||||
"""
|
||||
|
||||
self._commands = {}
|
||||
self.commands = {}
|
||||
"""
|
||||
A dictionary containing all commands to look for and the name
|
||||
of the function they call.
|
||||
"""
|
||||
|
||||
self.cmd_priv = {}
|
||||
"""
|
||||
A dictionary with command names as keys and required privilege
|
||||
levels as values.
|
||||
"""
|
||||
|
||||
self.doc = {}
|
||||
"""
|
||||
A dictionary of command names to their docstring and example, if
|
||||
declared.
|
||||
"""
|
||||
|
||||
self._times = {}
|
||||
"""
|
||||
A dictionary full of times when certain functions were called for
|
||||
|
@ -108,7 +96,11 @@ class Fulvia(irc.IRCClient):
|
|||
Find and load all of our modules.
|
||||
"""
|
||||
print(f"Loading modules...")
|
||||
self._callables, self._rules, self._commands, self.doc = {}, {}, {}, {}
|
||||
self._callables = {}
|
||||
self._hooks = []
|
||||
self.commands = {}
|
||||
self._times = {}
|
||||
self.url_callbacks = {}
|
||||
# ensure they're empty
|
||||
|
||||
modules = loader.find_modules(self.config.homedir)
|
||||
|
@ -137,8 +129,12 @@ class Fulvia(irc.IRCClient):
|
|||
if hasattr(func, 'commands'):
|
||||
self._callables[func.__name__] = func
|
||||
for command in func.commands:
|
||||
self._commands[command] = func.__name__
|
||||
self.cmd_priv[command] = func.priv
|
||||
self.commands[command] = tools.Command(command)
|
||||
self.commands[command]._func_name = func.__name__
|
||||
self.commands[command].priv = func.priv
|
||||
self.commands[command].doc = func._docs
|
||||
if command in func.aliases:
|
||||
self.commands[command].alias = True
|
||||
|
||||
if func.hook:
|
||||
self._callables[func.__name__] = func
|
||||
|
@ -151,9 +147,6 @@ class Fulvia(irc.IRCClient):
|
|||
for url in func.url_callback:
|
||||
self.url_callbacks[url] = func
|
||||
|
||||
for command, docs in func._docs.items():
|
||||
self.doc[command] = docs
|
||||
|
||||
|
||||
def unregister_callable(self, func):
|
||||
"""
|
||||
|
@ -166,8 +159,7 @@ class Fulvia(irc.IRCClient):
|
|||
if hasattr(func, 'commands'):
|
||||
self._callables.pop(func.__name__)
|
||||
for command in func.commands:
|
||||
self._commands.pop(command)
|
||||
self.cmd_priv.pop(command)
|
||||
self.commands.pop(command)
|
||||
|
||||
if func.hook:
|
||||
self._callables.pop(func.__name__)
|
||||
|
@ -180,9 +172,6 @@ class Fulvia(irc.IRCClient):
|
|||
for url in func.url_callback:
|
||||
self.url_callbacks.pop(url)
|
||||
|
||||
for command, docs in func._docs.items():
|
||||
self.doc.pop(command)
|
||||
|
||||
|
||||
def stillConnected(self):
|
||||
"""Returns true if the bot is still connected to the server."""
|
||||
|
@ -217,7 +206,7 @@ class Fulvia(irc.IRCClient):
|
|||
if message.startswith(self.prefix):
|
||||
command, _, _ = message.partition(" ")
|
||||
command = command.replace(self.prefix, "", 1)
|
||||
func_name = self._commands.get(command)
|
||||
func_name = self.commands.get(command)._func_name
|
||||
if not func_name:
|
||||
return
|
||||
func_names.append(func_name)
|
||||
|
|
|
@ -93,6 +93,10 @@ def process_callable(func, config):
|
|||
func.priv = getattr(func, "priv", 0)
|
||||
|
||||
if hasattr(func, 'commands'):
|
||||
if len(func.commands) > 1:
|
||||
func.aliases = func.commands[1:]
|
||||
else:
|
||||
func.aliases = []
|
||||
if hasattr(func, 'example'):
|
||||
for n, example in enumerate(func.example):
|
||||
ex_input = example[0]
|
||||
|
@ -102,5 +106,4 @@ def process_callable(func, config):
|
|||
ex_input = prefix + ex_input
|
||||
func.example[n] = (ex_input, example[1])
|
||||
if doc:
|
||||
for command in func.commands:
|
||||
func._docs[command] = (doc, func.example)
|
||||
func._docs = (doc, func.example)
|
||||
|
|
|
@ -14,9 +14,9 @@ def help(bot, trigger):
|
|||
if trigger.group(2):
|
||||
name = trigger.group(2)
|
||||
name = name.lower()
|
||||
if name not in bot.doc:
|
||||
if name not in bot.commands:
|
||||
return
|
||||
doc = bot.doc[name]
|
||||
doc = bot.commands[name].doc
|
||||
docstring, examples = doc
|
||||
if examples:
|
||||
ex = random.choice(examples)
|
||||
|
@ -28,12 +28,14 @@ def help(bot, trigger):
|
|||
bot.msg("Ex. Out: " + ex[1])
|
||||
|
||||
else:
|
||||
if not trigger.admin and not trigger.owner:
|
||||
cmds = [cmd for cmd, priv in bot.cmd_priv.items() if priv < 5]
|
||||
elif trigger.admin and not trigger.owner:
|
||||
cmds = [cmd for cmd, priv in bot.cmd_priv.items() if priv <= 5]
|
||||
if trigger.owner:
|
||||
cmds = [cmd for _, cmd in bot.commands.items()]
|
||||
elif trigger.admin:
|
||||
cmds = [cmd for _, cmd in bot.commands.items() if cmd.priv <= 5]
|
||||
else:
|
||||
cmds = bot.cmd_priv.keys()
|
||||
cmds = [cmd for _, cmd in bot.commands.items() if cmd.priv < 5]
|
||||
|
||||
cmds = [cmd.name for cmd in cmds if not cmd.alias]
|
||||
cmds = sorted(cmds)
|
||||
msg = "Available commands: " + ", ".join(cmds)
|
||||
bot.msg(msg)
|
||||
|
|
|
@ -132,6 +132,19 @@ class Channel(object):
|
|||
self.privileges[new] = self.privileges.pop(old)
|
||||
|
||||
|
||||
class Command():
|
||||
"""
|
||||
A representation of a command and associated documentation and other
|
||||
atributes.
|
||||
"""
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
self._func_name = ""
|
||||
self.priv = 0
|
||||
self.doc = None
|
||||
self.alias = False
|
||||
|
||||
|
||||
def configureHostMask(mask):
|
||||
"""
|
||||
Returns a valid hostmask based on user input.
|
||||
|
|
Loading…
Reference in New Issue
Block a user