-1

リストからランダムな項目を 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")
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 に答える