2014-08-27 23:39:31 +00:00
|
|
|
"""Help to make choice"""
|
|
|
|
|
2015-10-14 04:47:01 +00:00
|
|
|
# PYTHON STUFFS #######################################################
|
|
|
|
|
2013-01-07 11:22:07 +00:00
|
|
|
import random
|
2015-10-14 04:47:01 +00:00
|
|
|
import shlex
|
2013-01-07 11:22:07 +00:00
|
|
|
|
2015-10-14 04:47:01 +00:00
|
|
|
from nemubot import context
|
2015-10-30 20:57:45 +00:00
|
|
|
from nemubot.exception import IMException
|
2015-01-03 19:34:44 +00:00
|
|
|
from nemubot.hooks import hook
|
2013-01-07 11:22:07 +00:00
|
|
|
|
2014-09-26 16:00:22 +00:00
|
|
|
from more import Response
|
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
2015-10-14 04:47:01 +00:00
|
|
|
# MODULE INTERFACE ####################################################
|
|
|
|
|
2015-11-02 19:19:12 +00:00
|
|
|
@hook.command("choice")
|
2013-01-07 11:22:07 +00:00
|
|
|
def cmd_choice(msg):
|
2015-07-10 21:09:54 +00:00
|
|
|
if not len(msg.args):
|
2015-10-30 20:57:45 +00:00
|
|
|
raise IMException("indicate some terms to pick!")
|
2015-07-10 21:09:54 +00:00
|
|
|
|
|
|
|
return Response(random.choice(msg.args),
|
|
|
|
channel=msg.channel,
|
|
|
|
nick=msg.nick)
|
2015-10-14 04:47:01 +00:00
|
|
|
|
|
|
|
|
2015-11-02 19:19:12 +00:00
|
|
|
@hook.command("choicecmd")
|
2015-10-30 20:10:06 +00:00
|
|
|
def cmd_choicecmd(msg):
|
2015-10-14 04:47:01 +00:00
|
|
|
if not len(msg.args):
|
2015-10-30 20:57:45 +00:00
|
|
|
raise IMException("indicate some command to pick!")
|
2015-10-14 04:47:01 +00:00
|
|
|
|
|
|
|
choice = shlex.split(random.choice(msg.args))
|
|
|
|
|
2016-03-03 18:11:24 +00:00
|
|
|
return [x for x in context.subtreat(context.subparse(msg, choice))]
|
|
|
|
|
|
|
|
|
|
|
|
@hook.command("choiceres")
|
|
|
|
def cmd_choiceres(msg):
|
|
|
|
if not len(msg.args):
|
|
|
|
raise IMException("indicate some command to pick a message from!")
|
|
|
|
|
|
|
|
rl = [x for x in context.subtreat(context.subparse(msg, " ".join(msg.args)))]
|
|
|
|
if len(rl) <= 0:
|
|
|
|
return rl
|
|
|
|
|
|
|
|
r = random.choice(rl)
|
|
|
|
|
|
|
|
if isinstance(r, Response):
|
|
|
|
for i in range(len(r.messages) - 1, -1, -1):
|
|
|
|
if isinstance(r.messages[i], list):
|
|
|
|
r.messages = [ random.choice(random.choice(r.messages)) ]
|
|
|
|
elif isinstance(r.messages[i], str):
|
|
|
|
r.messages = [ random.choice(r.messages) ]
|
|
|
|
return r
|