0 Replies - 978 Views - Last Post: 19 February 2013 - 09:02 AM Rate Topic: -----

#1 mr.gr33n   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 07-January 14

python IrcBot not sure if I did it efficiently need advice!

Posted 19 February 2013 - 09:02 AM

I am writing a bot for fun and wanted some outsider opinions on what I could be doing better.
It doesn't do much at all just idle's in channel and responds backs to a ^help request and then shutsdown with a ^shutdown request.
I just don't think I have done it as efficienlty as I could but I am not sure how to improve it.
If you guys notice anything it would be greatly appreciated if you would let me know what to do to improve it/ broaden my knowledge.
so I just wipped this code up this afternoon I know it might look horible but it a time thing more than anything, so here is the code.:


import socket, time, sys, re

#search the buffer and if a PING is received return True else return False
def PingFunc(buff):
	i = regExpression.search(buff)
	if i:
		if i.group(1) == "PING":
			sock.send("JOIN #random\r\n")
			return i.group(2)
			return False
	else: return False
def Commands(sock,buff):
	i = regExpression.search(buff)
	if i:
		if i.group(9):
			#commands go here!

			#strip the command so you don't get any whitespace
			command = i.group(9).strip()
			if command == "^help":
				print "fdsf"
				sock.send("PRIVMSG #randomn : ^shutdown - dissconnects the bot from the server(might possibly add a recconnect after a certain time e.g '^shutdown 5' would reconnect after 5 seconds\r\n")
				return True

			if command == "^shutdown":
				return "shutdown"

#set the ip and port of the irc server
addr = (socket.gethostbyname("irc.freenode.net"), 6667)

#print the server ip just for fun
print "server " + str(addr[0])

#set the channel
channel = "##newboston"
username= "Gr33"

#create the tcp socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

#connect the socket to the server
print "Connecting to server\n"

#run through 4 times
count = 0
while count < 3:
	count += 1
	#on the 3rd run through send user data so the server accepts
	if count == 2:
		sock.send("NICK " + username +"\r\n")
		sock.send("USER Gr33nBot 0 * :Bot\r\n")

	data = sock.recv(2044)
	print data

#compile the regularExpression why here? I have no clue :P/>/>/>
regExpression = re.compile('(PING) (\:\w+\.\w+\.\w+)|(\:\w+\.\w+\.\w+|\:\w+\!(([a-zA-Z0-9_.`~!@#$%^&*()_+=-])+)) (\w+) (\w+|\#\#\w+|\#\w+) (\:)/>/>/>(.+)',re.DOTALL)
while 1:


	data = sock.recv(4096)
	#if the connection is closed break the loop
	if not data: break
	print data
	#if the server has finished sending you the MOTD		
	k = data.find("/MOTD")
	if k != -1:
		sock.send("JOIN "+channel+"\r\n")
		sock.send("PRIVMSG #random :whats up bitches, Gr33nBot in the house\r\n")
	ping = PingFunc(data)
	if ping:
		sock.send("PING "+ping+"\r\n")
		comand = Commands(sock,data)
	if comand == "shutdown": break

Thanks guys n gals, un-codable.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1