2010-09-10 36 views
-1

我試圖從列表中將隨機項打印到我的XCHAT通道消息中。到目前爲止,我只能從列表中單獨打印隨機項目,但不能打印任何特定的文本。在XCHAT IRC腳本中使用Python隨機模塊

實施例的使用將是:「/跑blahblahblah」,以產生一信道消息的所期望的效果,例如「blahblahblah [隨機項]」

__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") 

回答

0
  1. 您沒有允許呼叫者指定可供選擇的參數。

    def ran(choices=None): 
        if not choices: 
         choices = ('test1', 'test2', 'test3', 'test4', 'test5') 
        return random.choice(choices) 
    
  2. 您需要從命令中獲得選擇。

    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是通過命令發送的字的列表,word[0]是命令本身,使得僅來自1和進一步複製。

+0

我仍然得到大致相同的效果,仍然不確定如何在我的頻道消息中添加隨機項目的文本。 – isfigd 2010-09-10 09:30:17

+0

Ignacio's是正確的,除了它說'word_eol'而不是'word'。 'word_eol'提供從'我'字直到行尾而不是單個單詞。 – 2012-01-26 19:04:09