#!/usr/bin/env python3

###############################################################################
# Module Imports
###############################################################################

import sopel
import pyscp_bot.jarvis as vocab

###############################################################################


@sopel.module.commands('[^ ]*')
def autocomplete(bot, trigger):
    commands = {command: module
                for module, group in bot._command_groups.items()
                for command in group}
    partial = trigger.group(1)
    if partial in commands:
        return
    matches = {k: v for k, v in commands.items() if k.startswith(partial)}
    if len(matches) == 1:
        name, module = list(matches.items())[0]
        items = [i for pr, gr in bot._callables.items() for i in gr.items()]
        funcs = [f[0] for regexp, f in items]
        funcs = [
            f for f in funcs if f.__module__ == module and f.__name__ == name]
        wrapper = bot.SopelWrapper(bot, trigger)
        bot.call(funcs[0], wrapper, trigger)
        return
    commands = ['\x02{}\x02'.format(k) for k in matches]
    bot.say('{}: did you mean {} or {}?'.format(
        trigger.nick, ', '.join(commands[:-1]), commands[-1]))

@@ -195,16 +195,30 @@
 ######%0A%0A
+import random%0A
 import s
@@ -222,16 +222,16 @@
 t sopel%0A
-
 import p
@@ -686,16 +686,51 @@
 rtial)%7D%0A
+    if not matches:%0A        return%0A
     if l
@@ -1230,16 +1230,16 @@
 format(%0A
-
         
@@ -1293,8 +1293,295 @@
 s%5B-1%5D))%0A
+%0A%0A@sopel.module.commands('choose')%0Adef choose(bot, trigger):%0A    %22%22%22%0A    Randomly pick one of the options.%0A%0A    The options must be comma-separated.%0A    %22%22%22%0A    options = %5Bi.strip() for i in trigger.group(2).split(',')%5D%0A    bot.say('%7B%7D: %7B%7D'.format(trigger.nick, random.choice(options)))%0A

add !choose command