在XCHAT IRC脚本中使用Python随机模块

-1 投票
1 回答
824 浏览
提问于 2025-04-16 03:57

我想在我的XCHAT频道消息中打印一些随机的列表项。目前我只能单独打印列表中的随机项,但无法和特定的文字一起打印。

举个例子,如果我输入:"/ran 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")

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] 是命令本身,所以只需要从第一个单词开始复制。

撰写回答