python - 变量在消息中作为空白发送

标签 python xchat2

我正在尝试为(他)xchat 编写一个以前的消息机器人。保存和打印消息没问题,但发送时它不会保留消息。

我尝试了各种方法修复它,但它总是空白。

问题区域:

for y in range(0, 14):                
    if msg_last[y]:     
        msg_to_say = 'privmsg ' + triggernick + ' <previously_bot> '
        msg_to_say = msg_to_say + msg_last[y]
        print (msg_to_say)
        destination.command(msg_to_say)

在 Xchat 内部它会打印:

privmsg <nick> <previously_bot> $msg

但在发送后它将是:

privmsg <nick> <previously_bot> 

谁能发现我错过了什么?

完整代码:

from random import randint
import xchat

__module_name__ = "Blake's Previous Messages Bot"
__module_version__ = " v0.1 "
__module_description__ = "A previous message bot"

print("\0034", __module_name__, __module_version__, __module_description__, "!\003")

#---Globals---#000000#FFFFFF----------------------------------------------------
msg_last = [str()] * 15
msg_count = 0

def on_chat(word, word_eol, userdata):
    global msg_count
    global msg_last
    msg = word[1]

    if msg_count == 15:
        #print ("Full, reloading list")
        for x in range(0, 14):
             msg_last[x + 1] = msg_last[x]
        msg_last[0] = msg
        msg_count = 0

    msg_last[msg_count] = msg
    msg_count = msg_count + 1

def on_join(word, word_eol, userdata):
    global msg_last
    triggernick, triggerchannel, triggerhost = word    
    destination = xchat.get_context()

    print ("Someone joined, pulling up log")

    for y in range(0, 14):                
        if msg_last[y]:

          msg_to_say = 'privmsg ' + triggernick + ' <previously_bot> '
          msg_to_say = msg_to_say + msg_last[y]
          print (msg_to_say)
          destination.command(msg_to_say)
    return xchat.EAT_ALL

def on_kevin(word, word_eol, userdata):
    triggernick, triggerchannel, triggerhost = word    
    destination = xchat.get_context()

    if triggernick == 'TenEighths':
      rnd = randint(0,2)
      if rnd == 2:
        rnd2 = randint(0,3)
        rnd_str = heyYouKevin()
        destination.command("say " + rnd_str)

    #print ("Someone joined, pulling up log")

def heyYouKevin(x):
    return {
        '0': 'be quiet Kevin',
        '1': 'omg TenEighths, will you be quiet already?',
        '2': 'Kevin is spouting nonsense',
        '3': "be quiet, TenEighths.",
    }[x] 

xchat.hook_print('Join', on_join)
xchat.hook_print('Channel Message', on_chat)
xchat.hook_print('Channel Message', on_kevin)

最佳答案

如果您使用原始 irc 命令 PRIVMSG,语法要求任何带空格的字符串以 : 开头。所以 PRIVMSG nick :This is a long string。更好的解决方案是只使用 xchat.command('msg nick ...') 因为它知道该做什么。

关于python - 变量在消息中作为空白发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34259846/

相关文章:

python - 从 PyQt5 中的 QListWidget 中删除项目

python 解压零元素

Python Bokeh HoverTool 格式化程序错误 : "unexpected attribute ' formatters' to HoverTool"

python 打印与 __str__?

python - 沿 NumPy 数组的轴计算唯一元素

ubuntu - IRC 客户端连接到通用 channel