2 Replies - 3390 Views - Last Post: 21 January 2011 - 06:56 PM Rate Topic: -----

#1 EnvXOwner  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 357
  • View blog
  • Posts: 2,319
  • Joined: 10-August 09

IRC Bot Made In Python

Posted 17 January 2011 - 09:38 PM

Hello, I'm trying to make an IRC Bot for #c0dingf0x on irc.freenode.net. I was following this tutorial, which is out dated and changed some of the code to be compatible with Python 3.2. Here is the code I have so far:
'''
Created on Jan 17, 2011

@author: Terry June
'''
import sys
import socket
import string
import os

host = "irc.freenod.net" # the irc server
port = 6667 # port number
nick = "TerryBot" # nickname
ident = "TerryBot" # identity
realname = "EnvBot" # the name of the bot
owner = "Terry June" # who made it
channel = "#c0dingf0x"
server_msg = "" # messages from the server and client

s = socket.socket() # open socket
s.connect((host, port)) # connect to server
s.send("Nick: "+nick+"\n") # we send the nickname to the server
s.send("User: "+ident+" "+host+" Name: "+realname+"\n")

while 1:
    line = s.recv(500) # recieve the server messages
    print(line)
    if line.find("Welcome to an Env X Software Server!") != -1: # join #c0dingf0x 
        s.send("JOIN "+channel+"\n")
        if line.find("PRIVMSG") != -1:
            parsemsg(line)
            line = line.rstrip()
            line = line.split()
            if(line[0] == "PING"):
                s.send("PONG "+line[1]+"\n")
                
def parsemsg(msg):
    complete = msg[1: ].split(":", 1) # Parse the message into useful data
    info = complete[0].split(" ")
    msgpart = complete[1]
    sender = info[0].split("!")
    if msgpart[0] == "$" and sender[0] == owner: # treat messge starting with $ as a command
        cmd = msgpart[1: ].split(" ")
        if cmd[0] == "op":
            s.send("MODE "+info[2]+" +o "+cmd[1]+"\n")
        if cmd[0] == "deop":
            s.send("MODE "+info[2]+" -o "+cmd[1]+"\n")
        if cmd[0] == "voice":
            s.send("MODE "+info[2]+" +v "+cmd[1]+"\n")
        if cmd[0] == "devoice":
            s.send("MODE "+info[2]+" -v "+cmd[1]+"\n")
        if cmd[0] == "sys":
            syscmd(msgpart[1: ], info[2])
            
    if msgpart[0] == "_" and sender[0] == owner: # treat messages with _ as explict command sent to the server
        cmd = msgpart[1: ]
        s.send(cmd+"\n")
        print("cmd = "+cmd)

def syscmd(commandline, channel):
    cmd = commandline.replace("sys ", "")
    cmd = cmd.rstrip()
    os.system(cmd+" >temp.txt")
    a = open("temp.txt")
    ot = a.read()
    ot.replace("\n", "|")
    a.close()
    s.send("PRIVMSG "+channel+" :"+ot+"\n")


My error, as simple as it is, is Undefined variable: parsemsg on line 31. I think that it may be because I have my spacing wrong? What do you guys think? I do plan on adding extra commands to this.

Is This A Good Question/Topic? 0
  • +

Replies To: IRC Bot Made In Python

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • Joined: 23-December 08

Re: IRC Bot Made In Python

Posted 17 January 2011 - 10:34 PM

This issue isn't serious, just move your functions above your global code.


'''
Created on Jan 17, 2011

@author: Terry June
'''
import sys
import socket
import string
import os

def parsemsg(msg):
    complete = msg[1: ].split(":", 1) # Parse the message into useful data
    info = complete[0].split(" ")
    msgpart = complete[1]
    sender = info[0].split("!")
    if msgpart[0] == "$" and sender[0] == owner: # treat messge starting with $ as a command
        cmd = msgpart[1: ].split(" ")
        if cmd[0] == "op":
            s.send("MODE "+info[2]+" +o "+cmd[1]+"\n")
        if cmd[0] == "deop":
            s.send("MODE "+info[2]+" -o "+cmd[1]+"\n")
        if cmd[0] == "voice":
            s.send("MODE "+info[2]+" +v "+cmd[1]+"\n")
        if cmd[0] == "devoice":
            s.send("MODE "+info[2]+" -v "+cmd[1]+"\n")
        if cmd[0] == "sys":
            syscmd(msgpart[1: ], info[2])
            
    if msgpart[0] == "_" and sender[0] == owner: # treat messages with _ as explict command sent to the server
        cmd = msgpart[1: ]
        s.send(cmd+"\n")
        print("cmd = "+cmd)

def syscmd(commandline, channel):
    cmd = commandline.replace("sys ", "")
    cmd = cmd.rstrip()
    os.system(cmd+" >temp.txt")
    a = open("temp.txt")
    ot = a.read()
    ot.replace("\n", "|")
    a.close()
    s.send("PRIVMSG "+channel+" :"+ot+"\n")

host = "irc.freenod.net" # the irc server
port = 6667 # port number
nick = "TerryBot" # nickname
ident = "TerryBot" # identity
realname = "EnvBot" # the name of the bot
owner = "Terry June" # who made it
channel = "#c0dingf0x"
server_msg = "" # messages from the server and client

s = socket.socket() # open socket
s.connect((host, port)) # connect to server
s.send("Nick: "+nick+"\n") # we send the nickname to the server
s.send("User: "+ident+" "+host+" Name: "+realname+"\n")

while True: 
    line = s.recv(500) # recieve the server messages
    print(line)
    if line.find("Welcome to an Env X Software Server!") != -1: # join #c0dingf0x 
        s.send("JOIN "+channel+"\n")
        if line.find("PRIVMSG") != -1:
            parsemsg(line)
            line = line.rstrip()
            line = line.split()
            if(line[0] == "PING"):
                s.send("PONG "+line[1]+"\n")
               




I personally prefer that everything be inside of functions though, so I would change it like this:

'''
Created on Jan 17, 2011

@author: Terry June
'''
import sys
import socket
import string
import os

def main():
    host = "irc.freenod.net" # the irc server
    port = 6667 # port number
    nick = "TerryBot" # nickname
    ident = "TerryBot" # identity
    realname = "EnvBot" # the name of the bot
    owner = "Terry June" # who made it
    channel = "#c0dingf0x"
    server_msg = "" # messages from the server and client

    s = socket.socket() # open socket
    s.connect((host, port)) # connect to server
    s.send("Nick: "+nick+"\n") # we send the nickname to the server
    s.send("User: "+ident+" "+host+" Name: "+realname+"\n")

    while True:
        line = s.recv(500) # recieve the server messages
        print(line)
        if line.find("Welcome to an Env X Software Server!") != -1: # join #c0dingf0x 
            s.send("JOIN "+channel+"\n")
            if line.find("PRIVMSG") != -1:
                parsemsg(line)
                line = line.rstrip()
                line = line.split()
                if(line[0] == "PING"):
                    s.send("PONG "+line[1]+"\n")
    return 0 #unreachable?
                
def parsemsg(msg):
    complete = msg[1: ].split(":", 1) # Parse the message into useful data
    info = complete[0].split(" ")
    msgpart = complete[1]
    sender = info[0].split("!")
    if msgpart[0] == "$" and sender[0] == owner: # treat messge starting with $ as a command
        cmd = msgpart[1: ].split(" ")
        if cmd[0] == "op":
            s.send("MODE "+info[2]+" +o "+cmd[1]+"\n")
        if cmd[0] == "deop":
            s.send("MODE "+info[2]+" -o "+cmd[1]+"\n")
        if cmd[0] == "voice":
            s.send("MODE "+info[2]+" +v "+cmd[1]+"\n")
        if cmd[0] == "devoice":
            s.send("MODE "+info[2]+" -v "+cmd[1]+"\n")
        if cmd[0] == "sys":
            syscmd(msgpart[1: ], info[2])
            
    if msgpart[0] == "_" and sender[0] == owner: # treat messages with _ as explict command sent to the server
        cmd = msgpart[1: ]
        s.send(cmd+"\n")
        print("cmd = "+cmd)

def syscmd(commandline, channel):
    cmd = commandline.replace("sys ", "")
    cmd = cmd.rstrip()
    os.system(cmd+" >temp.txt")
    a = open("temp.txt")
    ot = a.read()
    ot.replace("\n", "|")
    a.close()
    s.send("PRIVMSG "+channel+" :"+ot+"\n")
    

if __name__=="__main__":
    sys.exit(main())


This post has been edited by atraub: 17 January 2011 - 10:42 PM

Was This Post Helpful? 3
  • +
  • -

#3 BlueMelon  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 187
  • Joined: 27-April 10

Re: IRC Bot Made In Python

Posted 21 January 2011 - 06:56 PM

Check this out, might help:
http://project-melon...on-irc-connect/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1