Advertisement
hobbyboy

Basic irc bot

Nov 19th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.33 KB | None | 0 0
  1. import socket
  2. import sys
  3. import time
  4. from sys import exit
  5.  
  6. server = "irc.esper.net"
  7. port = 6667
  8. channel = "#cajs"
  9. botnick = "Hobbybot"
  10.  
  11. def main():
  12.     irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  13.     print "Connecting to: "+server
  14.     irc.connect((server, port))
  15.     irc.send("NICK "+ botnick +"\r\n")
  16.     irc.send("USER "+ botnick +" 8 *" +" :This is a bot\r\n")
  17.     buf = ""
  18.    
  19.     while 1:
  20.         recvd = irc.recv(4096)
  21.         text = recvd.split('\n')
  22.         text[0] = buf + text[0]
  23.         last = len(text)
  24.         last -= 1
  25.         if not text[last].count('\r'):
  26.             buf = text.pop()
  27.         for line in text:
  28.             print line
  29.             msg = line.split(':')
  30.             if msg[0].count('PING'):
  31.                 irc.send('PONG :' + msg[1] + '\r\n')
  32.             elif len(msg) > 2 and msg[2].count("End of /MOTD command."):
  33.                 irc.send("JOIN "+ channel +"\r\n")
  34.            
  35.         if recvd.find(':.test') != -1:
  36.             irc.send("PRIVMSG "+ channel + " :It works! (just)\r\n")
  37.                
  38.         if recvd.find(':.info') != -1:
  39.             irc.send("PRIVMSG "+ channel + " :This bot is really buggy, and I have no idea what I am doing\r\n")
  40.                
  41.         if recvd.find(':.version') != -1:
  42.             irc.send("PRIVMSG "+ channel + " :Very alpha. I need to rewrite parts of it\r\n")
  43.            
  44.         if recvd.find(':.quit') != -1:
  45.             irc.send("PRIVMSG "+ channel + " :Goodbye!\r\n")
  46.             irc.send("QUIT :Leaving")
  47.             exit()
  48.                            
  49. if __name__ == "__main__":
  50.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement