-1

I'm trying to print random items from a list into my XCHAT channel messages. So far I've only been able to print the random items from my list alone, but not with any specific text.

Example usage would be: "/ran blahblahblah" to produce the desired effect of a channel message such as "blahblahblah [random item]"

__module_name__ = "ran.py"
__module_version__ = "1.0"
__module_description__ = "script to add random text to channel messages"

import xchat
import random

def ran(message):
    message = random.choice(['test1', 'test2', 'test3', 'test4', 'test5'])
    return(message)

def ran_cb(word, word_eol, userdata):
    message = ''
    message = ran(message)
    xchat.command("msg %s %s"%(xchat.get_info('channel'), message))
    return xchat.EAT_ALL

xchat.hook_command("ran", ran_cb, help="/ran to use")
isfigd
  • 1

1 Answers1

0
  1. You don't allow the caller to specify the arguments to choose from.

    def ran(choices=None):
        if not choices:
            choices = ('test1', 'test2', 'test3', 'test4', 'test5')
        return random.choice(choices)
    
  2. You need to get the choices from the command.

    def ran_cb(word, word_eol, userdata):
        message = ran(word[1:])
        xchat.command("msg %s %s"%(xchat.get_info('channel'), message))
        return xchat.EAT_ALL
    

    word is a list of the words sent through the command, word[0] is the command itself so only copy from 1 and further.

Ward Muylaert
  • 545
  • 4
  • 27
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • I'm still getting roughly the same effect, still unsure on how to add text with a random item in my channel messages. – isfigd Sep 10 '10 at 09:30
  • Ignacio's was correct except for it saying `word_eol` instead of `word`. `word_eol` provides from the `i`th word till the end of the line instead of individual words. – Ward Muylaert Jan 26 '12 at 19:04