57 lines
1.4 KiB
Python
Executable File
57 lines
1.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
Displays help docs and examples for commands, as well as lists all commands
|
|
available.
|
|
"""
|
|
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):
|
|
command = trigger.group(2)
|
|
command = command.lower()
|
|
if command not in bot.commands:
|
|
return bot.msg("Command not found.")
|
|
|
|
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])
|
|
|
|
else:
|
|
funcs = [func for cmd, func in bot.commands.items()]
|
|
if not trigger.owner:
|
|
funcs = [f for f in funcs if not hasattr(f, 'require_owner')]
|
|
if not trigger.admin:
|
|
funcs = [f for f in funcs if not hasattr(f, 'require_admin')]
|
|
|
|
cmds = {func.commands[0] for func in funcs}
|
|
cmds = sorted(list(cmds))
|
|
msg = "Available commands: " + ", ".join(cmds)
|
|
bot.msg(msg)
|