~ubuntu-branches/ubuntu/utopic/gozerbot/utopic

« back to all changes in this revision

Viewing changes to gozerbot/plugs/choice.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2009-09-14 09:00:29 UTC
  • mfrom: (1.1.4 upstream) (3.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090914090029-uval0ekt72kmklxw
Tags: 0.9.1.3-3
Changed dependency on python-setuptools to python-pkg-resources
(Closes: #546435) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# plugs/choice.py
 
2
#
 
3
#
 
4
 
 
5
""" the choice command can be used with a string or in a pipeline """
 
6
 
 
7
__copyright__ = 'this file is in the public domain'
 
8
 
 
9
# gozerbot imports
 
10
from gozerbot.utils.generic import waitforqueue
 
11
from gozerbot.commands import cmnds
 
12
from gozerbot.examples import examples
 
13
from gozerbot.plughelp import plughelp
 
14
from gozerbot.tests import tests
 
15
 
 
16
# basic imports
 
17
import random
 
18
 
 
19
plughelp.add('choice', 'make a random choice')
 
20
 
 
21
def handle_choice(bot, ievent):
 
22
 
 
23
    """ make a random choice out of different words or list elements. """ 
 
24
 
 
25
    result = []
 
26
 
 
27
    if ievent.inqueue:
 
28
        result = waitforqueue(ievent.inqueue, 5)
 
29
    elif not ievent.args:
 
30
        ievent.missing('<space seperated list>')
 
31
        return
 
32
    else:
 
33
        result = ievent.args 
 
34
 
 
35
    if result:
 
36
        ievent.reply(random.choice(result))
 
37
    else:
 
38
        ievent.reply('nothing to choose from')
 
39
 
 
40
cmnds.add('choice', handle_choice, ['USER', 'WEB', 'CLOUD'], threaded=True)
 
41
examples.add('choice', 'make a random choice', '1) choice a b c 2) list | choice')
 
42
tests.add('choice a ab ac', 'a')
 
43
tests.add('list | choice')