-1

我正在尝试将列表中的随机项目打印到我的 XCHAT 频道消息中。到目前为止,我只能单独打印列表中的随机项目,但不能打印任何特定文本。

示例用法是:“/ran blahblahblah”以产生所需的频道消息效果,例如“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")
4

1 回答 1

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 和更远的位置复制。

于 2010-09-10T08:55:06.627 回答