【问题标题】:Using the Python random module with XCHAT IRC scripts将 Python 随机模块与 XCHAT IRC 脚本一起使用
【发布时间】:2010-09-10 08:44:45
【问题描述】:

我正在尝试将列表中的随机项目打印到我的 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")

【问题讨论】:

    标签: python random irc


    【解决方案1】:
    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 开始复制。

    【讨论】:

    • 我仍然得到大致相同的效果,仍然不确定如何在我的频道消息中添加带有随机项目的文本。
    • Ignacio 是正确的,只是它说 word_eol 而不是 wordword_eol 提供从 ith 到行尾的单词,而不是单个单词。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 2017-11-01
    • 2015-04-26
    • 2010-09-21
    • 2017-06-19
    • 1970-01-01
    相关资源
    最近更新 更多