Volkova

Python IRC Bot

Jul 11th, 2011
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. import socket
  2. from time import gmtime, strftime
  3.  
  4. server = "irc.rizon.net"
  5. channel = "#channel"
  6. botnick = "TheNickname"
  7.  
  8.  
  9. def ping():
  10.     ircsock.send("PONG :pingis\r\n")
  11.  
  12. def kick(channel, nick):
  13.     ircsock.send('KICK %s %s :Goodbye <3\r\n' % (channel,nick))
  14.  
  15. def joinchan(chan):
  16.     ircsock.send("JOIN "+ chan +"\r\n")
  17.  
  18. def hello(nick):
  19.     ircsock.send("PRIVMSG %s :Hello %s! \r\n" % (channel,nick))
  20.  
  21. def commands(nick,channel,message):
  22.     if message.find(botnick+': 5')!=-1:
  23.         ircsock.send('PRIVMSG %s :%s: Mr.Derple \r\n' % (channel,nick))
  24.     elif message.find(botnick+': help')!=-1:
  25.         ircsock.send('PRIVMSG %s :%s: You don\'t need any help nigga.\r\n' % (channel,nick))
  26.     elif message.find(botnick+': die')!=-1:
  27.         ircsock.send("PART %s :okay.jpg\r\n" % (channel))
  28.         exit()
  29.     elif ircmsg.find(":!k") != -1:
  30.         nick=ircmsg.split('!k')[1][1:]
  31.         channel=ircmsg.split(' PRIVMSG ')[-1].split(' :')[0]
  32.         kick(channel,nick)
  33.     elif ircmsg.find(":Hello "+ botnick) != -1 or ircmsg.find(":Hi "+ botnick) != -1:
  34.         nick=ircmsg.split('!')[0][1:]
  35.         hello(nick)
  36.  
  37. ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  38. ircsock.connect((server, 6667))
  39. ircsock.send("NICK %s \r\n" % (botnick))
  40. ircsock.send("USER %s %s %s :Mr.Derple\r\n" % (botnick,botnick,botnick))
  41.  
  42. joinchan(channel)
  43.  
  44. while True:
  45.     ircmsg = ircsock.recv(2048)
  46.     ircmsg = ircmsg.strip('\n\r')
  47.  
  48.     if ircmsg.find("PING :") != -1:
  49.         ping()
  50.    
  51.     #uncomment this to greet people when they join
  52.     #if ircmsg.find(" JOIN ") != -1:
  53.     #   nick=ircmsg.split('!')[0][1:]
  54.     #   hello(nick)
  55.  
  56.     if ircmsg.find(' PRIVMSG ')!=-1:
  57.         nick=ircmsg.split('!')[0][1:]
  58.         channel=ircmsg.split(' PRIVMSG ')[-1].split(' :')[0]
  59.         commands(nick,channel,ircmsg)
  60.  
  61.     if ircmsg.find(' PRIVMSG ')!=-1:
  62.         ircmsg = ircmsg.replace("<","&lt;")
  63.         message = ircmsg.split(channel)
  64.         nick=ircmsg.split('!')[0][1:]
  65.         try:
  66.             message = message[1][2:]
  67.         except:
  68.             continue;
  69.         open("index.html","a+b").write("%s < %s > %s <br>\n" % (strftime("%H:%M:%S", gmtime()),nick,message))
Advertisement
Add Comment
Please, Sign In to add comment