refactor help

This commit is contained in:
iou1name 2019-10-08 09:06:52 -04:00
parent 6a067166fe
commit bf6e5784f2
2 changed files with 28 additions and 24 deletions

View File

@ -30,7 +30,7 @@ def unload_module(bot, name):
bot.unregister_callable(obj)
del old_module
delattr(sys.modules['modules'], name)
delattr(sys.modules['modules'], name.rpartition('.')[2])
del sys.modules[name]
@ -77,14 +77,6 @@ def process_callable(func, config):
Sets various helper atributes about a given function.
"""
prefix = config.core.prefix
doc = func.__doc__
if doc:
doc = doc.strip()
doc = doc.replace("\t", "")
doc = doc.replace("\n\n", "\x00")
doc = doc.replace("\n", " ")
doc = doc.replace("\x00", "\n")
func._docs = {}
func.example = getattr(func, "example", [(None, None)])
func.thread = getattr(func, "thread", True)
@ -108,5 +100,3 @@ def process_callable(func, config):
if ex_input[0] != prefix:
ex_input = prefix + ex_input
func.example[n] = (ex_input, example[1])
if doc:
func._docs = (doc, func.example)

View File

@ -7,24 +7,38 @@ import random
from module import commands, example
def clean_docstring(doc):
"""Cleans the docstring up a bit."""
if not doc:
return ''
doc = doc.strip()
doc = doc.replace("\t", "")
doc = doc.replace("\n\n", "\x00")
doc = doc.replace("\n", " ")
doc = doc.replace("\x00", "\n")
return doc
@commands('help', 'commands')
@example('.help tell')
def help(bot, trigger):
"""Shows a command's documentation, and possibly an example."""
if trigger.group(2):
name = trigger.group(2)
name = name.lower()
if name not in bot.commands:
command = trigger.group(2)
command = command.lower()
if command not in bot.commands:
return bot.msg("Command not found.")
cmd = bot.commands[name]
docstring, examples = cmd.doc
if examples:
ex = random.choice(examples)
bot.msg(docstring)
if cmd.aliases:
bot.msg("Aliases: " + ", ".join(cmd.aliases))
if ex[0]:
func = bot.commands[command]
doc = clean_docstring(func.__doc__)
bot.msg(doc)
aliases = [c for c, f in bot.commands.items() if f == func]
aliases.remove(command)
if aliases:
bot.msg("Aliases: " + ", ".join(aliases))
if hasattr(func, 'example'):
ex = random.choice(func.example)
bot.msg("Ex. In: " + ex[0])
if ex[1]:
bot.msg("Ex. Out: " + ex[1])