fulvia/modules/help.py

57 lines
1.4 KiB
Python
Raw Normal View History

2018-03-16 03:13:43 -04:00
#!/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
2019-10-08 09:06:52 -04:00
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
2018-03-16 03:13:43 -04:00
@commands('help', 'commands')
@example('.help tell')
def help(bot, trigger):
"""Shows a command's documentation, and possibly an example."""
2020-01-07 18:58:19 -05:00
if len(trigger.args) >= 2:
command = trigger.args[1]
2019-10-08 09:06:52 -04:00
command = command.lower()
if command not in bot.commands:
return bot.msg("Command not found.")
2019-10-08 09:06:52 -04:00
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)
2018-03-16 03:13:43 -04:00
bot.msg("Ex. In: " + ex[0])
2019-10-08 09:06:52 -04:00
if ex[1]:
bot.msg("Ex. Out: " + ex[1])
2018-03-16 03:13:43 -04:00
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')]
2019-10-08 09:14:26 -04:00
cmds = {func.commands[0] for func in funcs}
cmds = sorted(list(cmds))
2018-03-16 03:13:43 -04:00
msg = "Available commands: " + ", ".join(cmds)
bot.msg(msg)