我正在嘗試爲我的朋友的直播流製作一個Twitch機器人,並且在發送消息(或識別命令)時遇到了一些問題。Twitch IRC機器人不發送消息
我的代碼:
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 bot的更好的工作。
任何人誰是可能感興趣的:
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