fulvia/modules/help.py

59 lines
1.5 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."""
if trigger.group(2):
2019-10-08 09:06:52 -04:00
command = trigger.group(2)
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:
if trigger.owner:
2019-10-08 09:14:26 -04:00
funcs = [func for cmd, func in bot.commands.items()]
elif trigger.admin:
2019-10-08 09:14:26 -04:00
funcs = [func for cmd, func in bot.commands.items() if cmd.priv <= 5]
else:
priv = bot.channels[trigger.channel].privileges[trigger.nick]
2019-10-08 09:14:26 -04:00
funcs = [func for cmd, func in bot.commands.items() if cmd.priv <= priv]
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)