Reputation: 902
I'm trying to make a Twitch bot for my friend's live stream, and I'm having some issues getting it to send messages (or recognize commands).
My code:
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])
Does anyone know where I could've messed up?
The if len(line) >3:
works, because it shows messages that are sent in the chat (only if they're longer than 3 characters). So I'm guessing I have that stuff set up fine. It prints messages, etc.
I just can't seem to get commands to work.
EDIT: This is a really old post, but I'd like to point out that I ended up remaking the whole IRC bot to work better.
For anyone who's possibly interested:
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
Upvotes: 1
Views: 3254
Reputation: 141
At the line line=string.rstrip(line)
, you are removing trailing whitespace from the line. At the next line, line=string.split(line)
, you are splitting the line into words. After that point, line
is a list of each word in the message. So in fact when you check len(line) > 3
, you're checking if the line has more than three words.
When you want to extract the command from an IRC message, the easiest (although not entirely reliable) way is to just grab the first element of that list of words you now have, like you do where you check if line[0] == "PING"
. In this case, "PING" is the command.
Now, if you're referring not to IRC protocol commands but instead to commands sent as messages by other users, that's another story. All messages received -- private or in a channel -- follow this format:
PRIVMSG <message target> :<message received>
So if you're looking for messages from other users that contain "!test"
in the message received
portion of that. What's most likely happening is when you string.split(line)
, the first word of the message received
portion is ":!test"
instead of just "!test"
. What you really need to do if you want to have just the message portion of the entire IRC privmsg intact, is look for the colon and grab everything following that, i.e.
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"
The line message = line[line.find(":"):][1:]
is a little messy, but all that's happening there is that I'm finding the first occurrence of ":"
in the line and then chopping the colon off of that.
Upvotes: 1