Twitch IRC 机器人不发送消息

1 投票
1 回答
3196 浏览
提问于 2025-04-18 14:53

我正在为我朋友的直播制作一个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

1 个回答

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>

所以如果你想找其他用户发送的包含 "!test" 的消息,在 message received 部分。最有可能发生的情况是,当你 string.split(line) 时,message received 部分的第一个单词是 ":!test",而不是单纯的 "!test"。如果你想要保留整个IRC私聊消息的内容,就需要找到冒号并提取后面的所有内容,也就是:

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:] 看起来有点复杂,但其实就是在找这一行中第一个出现的 ":",然后把这个冒号去掉。

撰写回答