def chainlist(notice, bot): """- Returns the list of commands allowed in 'chain'""" hooks = [get_hook_from_command(bot, name) for name, allowed in allow_cache.items() if allowed] cmds = itertools.chain.from_iterable(map(attrgetter("aliases"), hooks)) cmds = sorted(cmds) for part in chunk_str(", ".join(cmds)): notice(part)
async def listchans(conn, chan, message, notice): """- Lists the current channels the bot is in""" chans = ', '.join(sorted(conn.channels, key=lambda x: x.strip('#').lower())) lines = formatting.chunk_str("I am currently in: {}".format(chans)) func = notice if chan[:1] == "#" else message for line in lines: func(line)
def paginated_list(data, delim=" \u2022 ", suffix='...', max_len=256, page_size=2, pager_cls=Pager): """ >>> list(paginated_list(['abc', 'def'])) [['abc \u2022 def']] >>> list(paginated_list(['abc', 'def'], max_len=5)) [['abc...', 'def']] >>> list(paginated_list(list('foobarbaz'), max_len=2)) [['f...', 'o... (page 1/5)'], ['o...', 'b... (page 2/5)'], ['a...', 'r... (page 3/5)'], ['b...', 'a... (page 4/5)'], ['z (page 5/5)']] >>> list(paginated_list(['foo', 'bar'], max_len=1)) [['f...', 'o... (page 1/3)'], ['o...', 'b... (page 2/3)'], ['a...', 'r (page 3/3)']] >>> list(paginated_list(['foo', 'bar'], max_len=2)) [['fo...', 'o... (page 1/2)'], ['ba...', 'r (page 2/2)']] """ lines = [""] def get_delim(): if lines[-1]: return delim return '' for item in data: if len(item) > max_len: # The length of a single item is longer then our max line length, split it lines.extend(chunk_str(item, max_len)) elif len(lines[-1] + get_delim() + item) > max_len: lines.append(item) else: lines[-1] += get_delim() + item formatted_lines = [] lines = [line for line in lines if line] while lines: line = lines.pop(0) formatted_lines.append("{}{}".format(line, suffix if lines else "")) return pager_cls(formatted_lines, chunk_size=page_size)
def help_command(text, chan, conn, bot, notice, message, has_permission): """[command] - gives help for [command], or lists all available commands if no command is specified :type text: str :type conn: cloudbot.client.Client :type bot: cloudbot.bot.CloudBot """ if text: searching_for = text.lower().strip() if not re.match(r'^\w+$', searching_for): notice("Invalid command name '{}'".format(text)) return else: searching_for = None if searching_for: if searching_for in bot.plugin_manager.commands: doc = bot.plugin_manager.commands[searching_for].doc if doc: if doc.split()[0].isalpha(): # this is using the old format of `name <args> - doc` message = "{}{}".format(conn.config["command_prefix"], doc) else: # this is using the new format of `<args> - doc` message = "{}{} {}".format(conn.config["command_prefix"], searching_for, doc) notice(message) else: notice("Command {} has no additional documentation.".format( searching_for)) else: notice("Unknown command '{}'".format(searching_for)) else: commands = [] for plugin in sorted(set(bot.plugin_manager.commands.values()), key=attrgetter("name")): # use set to remove duplicate commands (from multiple aliases), and sorted to sort by name if plugin.permissions: # check permissions allowed = False for perm in plugin.permissions: if has_permission(perm, notice=False): allowed = True break if not allowed: # skip adding this command continue # add the command to lines sent command = plugin.name commands.append(command) # list of lines to send to the user lines = formatting.chunk_str( "Here's a list of commands you can use: " + ", ".join(commands)) for line in lines: if chan[:1] == "#": notice(line) else: #This is an user in this case. message(line) notice( "For detailed help, use {}help <command>, without the brackets.". format(conn.config["command_prefix"]))
def help_command(text, chan, conn, bot, notice, message, has_permission): """[command] - gives help for [command], or lists all available commands if no command is specified :type text: str :type conn: cloudbot.client.Client :type bot: cloudbot.bot.CloudBot """ if text: searching_for = text.lower().strip() if not re.match(r'^\w+$', searching_for): notice("Invalid command name '{}'".format(text)) return else: searching_for = None if searching_for: if searching_for in bot.plugin_manager.commands: doc = bot.plugin_manager.commands[searching_for].doc if doc: if doc.split()[0].isalpha(): # this is using the old format of `name <args> - doc` message = "{}{}".format(conn.config["command_prefix"][0], doc) else: # this is using the new format of `<args> - doc` message = "{}{} {}".format(conn.config["command_prefix"][0], searching_for, doc) notice(message) else: notice("Command {} has no additional documentation.".format(searching_for)) else: notice("Unknown command '{}'".format(searching_for)) else: commands = [] for plugin in sorted(set(bot.plugin_manager.commands.values()), key=attrgetter("name")): # use set to remove duplicate commands (from multiple aliases), and sorted to sort by name if plugin.permissions: # check permissions allowed = False for perm in plugin.permissions: if has_permission(perm, notice=False): allowed = True break if not allowed: # skip adding this command continue # add the command to lines sent command = plugin.name commands.append(command) # list of lines to send to the user lines = formatting.chunk_str("Here's a list of commands you can use: " + ", ". join(commands)) for line in lines: if chan[:1] == "#": notice(line) else: #This is an user in this case. message(line) notice("For detailed help, use {}help <command>, without the brackets.".format(conn.config["command_prefix"]))
def test_chunk_str(): assert chunk_str(test_chunk_str_input, 10) == test_chunk_str_result
def format_list(name, data): begin = colors.parse("$(dgreen){}$(clear): ".format(name)) body = ', '.join(data) return chunk_str(begin + body)
async def help_command(text, chan, bot, notice, message, has_permission, triggered_prefix): """[command] - gives help for [command], or lists all available commands if no command is specified :type chan: str :type text: str :type bot: cloudbot.bot.CloudBot """ if text: searching_for = text.lower().strip() else: searching_for = None if text: cmds = list(get_potential_commands(bot, text)) if not cmds: notice("Unknown command '{}'".format(text)) return if len(cmds) > 1: notice("Possible matches: {}".format( formatting.get_text_list( sorted([command for command, _ in cmds])))) return doc = cmds[0][1].doc if doc: notice("{}{} {}".format(triggered_prefix, searching_for, doc)) else: notice("Command {} has no additional documentation.".format( searching_for)) else: commands = [] for plugin in sorted(set(bot.plugin_manager.commands.values()), key=attrgetter("name")): # use set to remove duplicate commands (from multiple aliases), and sorted to sort by name if plugin.permissions: # check permissions allowed = False for perm in plugin.permissions: if has_permission(perm, notice=False): allowed = True break if not allowed: # skip adding this command continue # add the command to lines sent command = plugin.name commands.append(command) # list of lines to send to the user lines = formatting.chunk_str( "Here's a list of commands you can use: " + ", ".join(commands)) for line in lines: if chan[:1] == "#": notice(line) else: # This is an user in this case. message(line) notice( "For detailed help, use {}help <command>, without the brackets.". format(triggered_prefix))