Iwant: Add @random.

master
Valentin Lorentz 2011-11-18 19:02:24 +01:00
parent 50061552b3
commit e6c430105b
2 changed files with 24 additions and 0 deletions

View File

@ -28,6 +28,8 @@
###
import random
import supybot.utils as utils
from supybot.commands import *
import supybot.plugins as plugins
@ -75,6 +77,9 @@ class Iwant(callbacks.Plugin):
Returns the list of wanted things for the <channel>. <channel> defaults
to the current channel."""
wishlist = unserialize(self.registryValue('wishlist', channel))
if list(wishlist) == 0:
irc.error(_('No wish for the moment.'))
return
indexes = range(1, len(wishlist) + 1)
wishlist_with_index = zip(indexes, wishlist)
formatted_wishlist = [_('#%i: %s') % x for x in wishlist_with_index]
@ -94,6 +99,22 @@ class Iwant(callbacks.Plugin):
irc.reply(_('Wish #%i is %s.') % (id, wishlist[id - 1]))
get = wrap(get, ['channel', 'id'])
@internationalizeDocstring
def random(self, irc, msg, args, channel):
"""[<channel>]
Tell you a random thing. <channel> is only needed if you
don't send the message on the channel itself."""
wishlist = unserialize(self.registryValue('wishlist', channel))
if list(wishlist) == 0:
irc.error(_('No wish for the moment.'))
return
indexes = range(1, len(wishlist) + 1)
wishlist_with_index = zip(indexes, wishlist)
wish = random.sample(wishlist_with_index, 1)[0]
irc.reply(_('Wish #%i is %s.') % wish)
random = wrap(random, ['channel'])
Class = Iwant

View File

@ -34,6 +34,8 @@ class IwantTestCase(ChannelPluginTestCase):
plugins = ('Iwant',)
def testIwant(self):
self.assertError('iwant random')
self.assertError('iwant list')
self.assertNotError('iwant you')
self.assertNotError('iwant "a working plugin"')
self.assertResponse('iwant list', '#1: you and #2: a working plugin')
@ -42,6 +44,7 @@ class IwantTestCase(ChannelPluginTestCase):
self.assertResponse('iwant list', '#1: you, #2: a working plugin, and '
'#3: be cool')
self.assertResponse('iwant get 2', 'Wish #2 is a working plugin.')
self.assertNotError('iwant random')
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: