【问题标题】:Twitch IRC bot not sending messagesTwitch IRC 机器人不发送消息
【发布时间】:2014-09-18 08:39:19
【问题描述】:

我正在尝试为我朋友的直播制作一个 Twitch 机器人,但在让它发送消息(或识别命令)时遇到了一些问题。

我的代码:

import socket,string
HOST = "irc.twitch.tv"
PORT = 6667
NICK = "Axiom"
IDENT = "Axiom"
REALNAME = "Axiom"
CHANNEL = "#itwreckz"
PASSWORD = "oauth:PASSHERE"
readbuffer = ""
print "Connecting.."
t = socket.socket()
t.connect((HOST, PORT))
print "Connected! Logging in"
t.send("PASS %s\r\n" % PASSWORD)
t.send("NICK %s\r\n" % NICK)
t.send("USER %s %s BOT :%s\r\n" % (IDENT, HOST, REALNAME))
print "Logged in. Joining Channel"
t.send("JOIN %s\r\n" % CHANNEL)
print "JOINED!"

while 2>1:
    readbuffer = readbuffer+t.recv(1024)
    taco = string.split(readbuffer, "\n")
    readbuffer = taco.pop()
    for line in taco:
        line=string.rstrip(line)
        line=string.split(line)
        if len(line) > 3:
            print line
            command = line
            if "!test" in command:
                print "If you see this, it recognizes command." #Doesn't show
                t.send("PRIVMSG %s :IT WORKS!\r\n" % CHANNEL) #This either
        if(line[0]=="PING"):
            t.send("PONG %s\r\n" % line[1])

有谁知道我在哪里搞砸了?

if len(line) >3: 有效,因为它显示在聊天中发送的消息(仅当它们超过 3 个字符时)。所以我猜我已经把这些东西设置好了。它打印消息等。

我似乎无法让命令工作。


编辑: 这是一篇很老的帖子,但我想指出,我最终重新制作了整个 IRC 机器人以更好地工作。

对于任何可能感兴趣的人:

import socket,time
network = 'irc.example.net'
port = 6667
channels = ['#Channel1','#Channel2'] #Add as many as you want
nick = 'myIRCBot'
identify = True
password = 'superAwesomePassword'
irc = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
print "Connecting to "+network+" ..."
irc.connect ((network, port))
print "Changing nick to "+nick+"..."
irc.send ('NICK '+nick+'\r\n')
if identify == True:
        print "Verifying password..."
        irc.send("PASS %s\n" % (password))
print "Setting login data..."
irc.send ('USER '+nick+' '+nick+' '+nick+' :'+nick+' IRC\r\n')
time.sleep(1)
for channel in channels:
        print "Joining "+channel+"..."
        irc.send ('JOIN '+channel+'\r\n')
        irc.send ('PRIVMSG '+channel+' :'+nick+' Started! Type .help for more\r\n')
        time.sleep(1)
print nick+" bot started."
while True:
        data = irc.recv(4096)
        if data.find('PING') != -1:
                irc.send('PONG '+data.split()[1]+'\r\n')
        try:
                user = data.split("!",1)[0].replace(":","",1)
                vhost = data.split(" ",1)[0].split("!",1)[1]
                dType = data.split(" ",1)[1].split(" ",1)[0]
                chan = data.split(" ",1)[1].split(" ",1)[1].split(" ",1)[0]
                msg = data.split(" ",1)[1].split(" ",1)[1].split(" ",1)[1].replace(":","",1).replace("\n","",1).replace("\r","",1)
                if msg == '.help':
                        irc.send ('PRIVMSG '+chan+' :This is the only command!\r\n')
                print user+" ("+vhost+") "+dType+" to "+chan+": "+msg
        except:
                pass

【问题讨论】:

    标签: python sockets bots irc twitch


    【解决方案1】:

    line=string.rstrip(line) 行,您将删除行中的尾随空格。在下一行,line=string.split(line),您正在将该行拆分为单词。在那之后,line 是消息中每个单词的列表。所以事实上,当您检查len(line) > 3 时,您正在检查该行是否包含三个以上的单词。

    当您想从 IRC 消息中提取命令时,最简单(尽管不完全可靠)的方法是只获取您现在拥有的单词列表的第一个元素,就像您在检查 if line[0] == "PING" 时所做的那样。在这种情况下,“PING”是命令。

    现在,如果您指的不是 IRC 协议命令,而是其他用户作为消息发送的命令,那就另当别论了。收到的所有消息(私人消息或频道中的消息)都遵循以下格式:

    PRIVMSG <message target> :<message received>
    

    因此,如果您要查找来自其他用户的消息,这些消息的 message received 部分包含 "!test"。最有可能发生的情况是当您 string.split(line) 时,message received 部分的第一个单词是 ":!test" 而不仅仅是 "!test"。如果您只想完整地保留整个 IRC privmsg 的消息部分,您真正需要做的是查找冒号并获取其后的所有内容,即

    words = line.split()
    if words[0] == "PRIVMSG":
        message = line[line.find(":"):][1:]
        message_words = message.split()
        if "!test" in message_words:
            print "If you see this, it recognizes the command"
    

    message = line[line.find(":"):][1:] 行有点乱,但那里发生的所有事情是我在行中找到":" 的第一次出现,然后将冒号切掉。

    【讨论】:

      猜你喜欢
      • 2019-06-13
      • 2012-09-21
      • 2018-12-31
      • 2012-07-28
      • 2015-05-05
      • 2021-08-19
      • 2021-05-08
      • 1970-01-01
      • 2015-02-09
      相关资源
      最近更新 更多