Advertisement
Guest User

Phredd's IRC Twitch chat bot

a guest
Sep 7th, 2013
2,870
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.82 KB | None | 0 0
  1. ### Phredd's IRC Twitch chat bot
  2.  
  3. import socket #imports module allowing connection to IRC
  4. import threading #imports module allowing timing functions
  5.  
  6. #sets variables for connection to twitch chat
  7. bot_owner = 'Phredd_'
  8. nick = 'PhreddBot'
  9. channel = '#oddmast'
  10. server = 'phreddbot.jtvirc.com'
  11. password = 'xxx'
  12.  
  13. queue = 0 #sets variable for anti-spam queue functionality
  14.  
  15. #sets variables for !add and !news commands
  16. command = '!notset'
  17. cmdmsg = 'This command is not set yet'
  18. newsmsg = 'No news set'
  19.  
  20.  
  21. irc = socket.socket()
  22. irc.connect((server, 6667)) #connects to the server
  23.  
  24. #sends variables for connection to twitch chat
  25. irc.send('PASS ' + password + '\r\n')
  26. irc.send('USER ' + nick + ' 0 * :' + bot_owner + '\r\n')
  27. irc.send('NICK ' + nick + '\r\n')
  28. irc.send('JOIN ' + channel + '\r\n')
  29.  
  30. def queuetimer(): #function for resetting the queue every 30 seconds
  31.     global queue
  32.     print 'queue reset'
  33.     queue = 0
  34.     threading.Timer(30,queuetimer).start()
  35. queuetimer()
  36.  
  37. while True:
  38.    
  39.     def message(msg): #function for sending messages to the IRC chat
  40.         global queue
  41.         queue = queue + 1
  42.         print queue
  43.         if queue < 20: #ensures does not send >20 msgs per 30 seconds.
  44.             irc.send('PRIVMSG ' + channel + ' :' + msg + '\r\n')
  45.         else:
  46.             print 'Message deleted'
  47.            
  48.     def newstimer(): #function for announcing news every 3 minutes
  49.         global newsmsg
  50.         global ntimer
  51.         themessage = '[NEWS]: ' + newsmsg
  52.         message(themessage)
  53.         print 'news on'
  54.         ntimer = threading.Timer(180,newstimer)
  55.         ntimer.start()
  56.         if newsmsg == 'No news set': #stops announcing news if it has been canceled.
  57.             ntimer.cancel()
  58.         else:
  59.             pass
  60.    
  61.     data = irc.recv(1204) #gets output from IRC server
  62.     user = data.split(':')[1]
  63.     user = user.split('!')[0] #determines the sender of the messages
  64.     print data
  65.    
  66.     if data.find('PING') != -1:
  67.         irc.send(data.replace('PING', 'PONG')) #responds to PINGS from the server
  68.    
  69.     if data.find('!test') != -1: #!test command
  70.         message('Phredd_ is awesome! :D')        
  71.     if data.find('!hello') != -1: #!hello command
  72.         if user == 'phredd_':
  73.             mymessage = 'Hi, ' + user + ', you are awesome! :D'
  74.             message(mymessage)
  75.         else:
  76.             mymessage = 'Hi, ' + user + ', you are a Butt! :D'
  77.             message(mymessage)
  78.            
  79.     if data.find('!cmd') != -1 and user == 'phredd_': #adds a new command
  80.         addsplit = channel + ' :' + '!cmd'
  81.         command = '!' + data.split(addsplit)[1].split(' ')[1] #determines new command
  82.         cmdmsg = ' '.join(data.split(addsplit)[1].split(' ')[2:]) #determines command output
  83.         message('Command set!')        
  84.     if data.find(command) != -1: #responds to the new command with the output
  85.         message(cmdmsg)
  86.     if data.find('!delcmd') != -1 and user == 'phredd_': #!deladd command
  87.         command = '!notset'
  88.         cmdmsg = 'This command is not set yet'
  89.         message('Command deleted!')
  90.        
  91.     if data.find('!addnews') != -1 and user == 'phredd_': #!addnews command
  92.         newssplit = channel + ' :' + '!addnews'
  93.         newsmsg = ' '.join(data.split(newssplit)[1].split(' ')[1:]) #determines new news message
  94.         message('News set!')
  95.         newstimer() #starts news timer function      
  96.     if data.find('!news') != -1 and user == 'phredd_': #!news command
  97.         themessage = '[NEWS]: ' + newsmsg
  98.         message(themessage)
  99.     if data.find('!delnews') !=-1 and user == 'phredd_': #deletes the news
  100.         newsmsg = 'No news set'
  101.         message('News deleted!')
  102.         newstimer()
  103.        
  104.     elif data.split(' ')[1] == 'PRIVMSG' and data.split(':')[2].startswith(nick):
  105.         message('Who said my name? SwiftRage')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement