Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Stack Overflow - Python IRCbot

By: a guest on Mar 3rd, 2012  |  syntax: Python  |  size: 2.37 KB  |  views: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import socket
  2. import urllib2
  3. import httplib
  4. import mechanize
  5.  
  6. channel = '#testbot'
  7. botnick = 'tastybot'
  8. network = 'irc.freenode.net'
  9. port = 6667
  10. irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
  11. irc.connect ( ( network, port ) )
  12. print irc.recv ( 4096 )
  13. irc.send ( 'NICK' + botnick + ':\r\n' )
  14. irc.send ( 'USER tastybot tastybot tastybot :Python IRC\r\n' )
  15. irc.send ( 'JOIN' + channel + ':\r\n' )
  16. irc.send ( 'PRIVMSG' + channel + ':What's up?\r\n' )
  17. running = True
  18.  
  19. # def Bored():
  20.         # UrlDict = {'1':'http://www.notdoppler.com/', '2':'http://thereifixedit.failblog.org/',}
  21.         # print UrlDict
  22.         # irc.send ('PRIVMSG' + channel + RandomBored + '\r' )
  23.  
  24. def WikiFact():
  25.         host = 'en.wikipedia.org/wiki'                          #Setting the site I want to go to
  26.         random = '/Special:Random'
  27.         url = 'http://' + host + random
  28.         httplib.HTTPConnection.debuglevel = 1   # found this on stackoverflow.
  29.         request = mechanize.Request(url, headers={'User-Agent': 'Windows NT 7.1'})
  30.         response = mechanize.urlopen(request)
  31.         ResultUrl = response.geturl()
  32.         irc.send ('PRIVMSG' + ResultUrl + 'Here, learn something!')
  33.  
  34. # def Dice():
  35.         # random_number = random.randint(1, 6)
  36.         # if data.find ( '!roll a dice' ) != -1:
  37.                 # irc.send ( 'PRIVMSG' + channel + ':The roll is ' + random_number + '!\r')
  38.        
  39. while running:
  40.         data = irc.recv ( 4096 )
  41.         if data.find ( 'PING' ) != -1:
  42.                 irc.send ( 'PONG ' + data.split() [ 1 ] + '\r\n' )
  43.         if data.find ( '!' + botnick + 'quit' ) != -1:
  44.                 if data.find ( 'reallyrose' ) != -1:
  45.                         irc.send ( 'PRIVMSG' + channel + ':Fine, if you don\'t want me. ;_;\r\n' )
  46.                         irc.send ( 'QUIT\r\n' )
  47.                         running = False
  48.                 else:
  49.                         irc.send ( 'PRIVMSG' + channel + ':Only reallyrose can tell me to do that.\r\n' )
  50.         if data.find ( 'hi ' + botnick) != -1:
  51.                 irc.send ( 'PRIVMSG' + channel + ':I already said hi...\r\n' )
  52.         if data.find ( 'hello ' + botnick) != -1:
  53.                 irc.send ( 'PRIVMSG' + channel + ':I already said hi...\r\n' )
  54.         if data.find ( 'KICK' ) != -1:
  55.                 irc.send ( 'JOIN' + channel + '\r\n' )
  56.         if data.find ( 'cheese' ) != -1:
  57.                 irc.send ( 'PRIVMSG' + channel + ':OMG WHERE?!?!\r' )
  58.         if data.find ( 'slaps ' + botnick ) != -1:
  59.                 irc.send ( 'PRIVMSG' + channel + ':Hey! That\'s mean. Why would you do that? ;_;\r\n' )
  60. #       if data.find ( 'PRIVMSG' + channel + ':!roll a dice' ) != -1:
  61.                 Dice()
  62.         if data.find ( 'PRIVMSG' + channel + ':!kittenbot teach' ) != -1:
  63.                 WikiFact()
clone this paste RAW Paste Data