Advertisement
berg

espeak_bot.py

Oct 15th, 2011
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.94 KB | None | 0 0
  1. import socket
  2. import os
  3. import time as t
  4. from time import time
  5. import sys
  6.  
  7.  
  8. #----------------------------------------------------------------------------
  9.  
  10.  
  11. #you need espeak installed in your system
  12.  
  13. #I made this bot for fun change the channel and the nick to suit your irc
  14.  
  15. #have fun and if it breaks your PC tell someone who cares!
  16.  
  17. #----------------------------------------------------------------------------
  18.  
  19.  
  20. path = "/home/dennis/bot/"
  21. t1=time()
  22. network = 'irc.freenode.net' #irc address to connect too
  23. port = 6667 #port for irc channel
  24. nick = 'espeak_bot' #bot name
  25. nick1 = 'espeak_bot'
  26. owner = 'name' #owners name used for quit command ..Make sure you put your irc name here or the quit command wont work
  27. password = 'Password' #bot reg name password
  28. channel = '#warzone2100' # set up our socket
  29. irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM ) #define how to send to irc with variable irc.send
  30. irc.connect ( ( network, port ) ) #log into irc
  31. irc.send ( 'NICK ' + nick + '\r\n' ) #send nickname
  32. irc.send ( 'USER espeak_bot espeak_bot espeak_bot :Python IRC\r\n' ) #send ident
  33. t.sleep(6) #wait till irc catches up to bot
  34. irc.send('PRIVMSG NICKSERV :identify ' + password + ' \r\n') #send identification to irc for reg bot name
  35. t.sleep(15) #wait till irc catches up
  36. irc.send ( 'JOIN ' + channel + '\r\n' ) #join channel
  37. #define functions
  38. def chatuser( input ): #get the user name of who is speaking
  39.     name = input.split("!")[0].replace(":", "")
  40.     return name
  41.  
  42.  
  43.  
  44.  
  45. i=0
  46. #t2=time()
  47. while time() - t1 < 30: #wait timer to try and wait till irc motd is done before bot speaks
  48.     data = irc.recv ( 4096 )#text as comes from irc
  49.     data = "" #empty data of motd
  50. while True:
  51.  
  52.  
  53.  
  54.     data = irc.recv ( 4096 )#text as comes from irc
  55.     data1 = data.upper()#text from irc in uppercase
  56.  
  57.  
  58. #espeak set up and test
  59.     if len(data.split("#warzone2100 :"))  > 1:#check that string length is over 1 char
  60.         text = data.split("#warzone2100 :")[1]#split string to have only message
  61.  
  62.         print text + "bug test"#just a bug catcher
  63.             s = "espeak '" + chatuser(data) + " said, " + text + "'." #create the string to send to espeak use name who spoke ...the message they sent
  64.         os.system(s) #send it to espeak
  65.  
  66.     if data.find ( nick ) != -1:
  67.         if data.find(channel) != -1 and data1.find('QUIT') != -1 and chatuser(data) == owner: #the quit command
  68.  
  69.             irc.send('PRIVMSG ' + channel + ' :If I must ' + owner + '.\r\n')
  70.             t.sleep(6)
  71.             irc.send('PRIVMSG ' + channel + ' :Ok fine Im outa here ' + owner + '.\r\n')
  72.             irc.send("QUIT :Stinky Berg\r\n")
  73.             print('PRIVMSG ' + channel + ' :Exit command issued from IRC '+ chatuser(data))
  74.             sys.exit()
  75.         elif data1.find('QUIT') != -1 and chatuser(data) != owner: #the quit command if not the owner reply
  76.             irc.send( 'PRIVMSG ' + channel + ':You are not my owner ' + chatuser(data))
  77.  
  78.     if data.find('PING') != -1 and data.find('freenode') != -1:
  79.         irc.send('PRIVMSG ' + nick + ' :PONG .\r\n')
  80.         print "pong"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement