Phredd's IRC Twitch chat bot

### Phredd's IRC Twitch chat bot

import socket #imports module allowing connection to IRC
import threading #imports module allowing timing functions

#sets variables for connection to twitch chat
bot_owner = 'Phredd_'
nick = 'PhreddBot'
channel = '#oddmast'
server = 'phreddbot.jtvirc.com'
password = 'xxx'

queue = 0 #sets variable for anti-spam queue functionality

#sets variables for !add and !news commands
command = '!notset'
cmdmsg = 'This command is not set yet'
newsmsg = 'No news set'


irc = socket.socket()
irc.connect((server, 6667)) #connects to the server

#sends variables for connection to twitch chat
irc.send('PASS ' + password + '\r\n')
irc.send('USER ' + nick + ' 0 * :' + bot_owner + '\r\n')
irc.send('NICK ' + nick + '\r\n')
irc.send('JOIN ' + channel + '\r\n')

def queuetimer(): #function for resetting the queue every 30 seconds
    global queue
    print 'queue reset'
    queue = 0
    threading.Timer(30,queuetimer).start()
queuetimer()

while True:
   
    def message(msg): #function for sending messages to the IRC chat
        global queue
        queue = queue + 1
        print queue
        if queue < 20: #ensures does not send >20 msgs per 30 seconds.
            irc.send('PRIVMSG ' + channel + ' :' + msg + '\r\n')
        else:
            print 'Message deleted'
           
    def newstimer(): #function for announcing news every 3 minutes
        global newsmsg
        global ntimer
        themessage = '[NEWS]: ' + newsmsg
        message(themessage)
        print 'news on'
        ntimer = threading.Timer(180,newstimer)
        ntimer.start()
        if newsmsg == 'No news set': #stops announcing news if it has been canceled.
            ntimer.cancel()
        else:
            pass
   
    data = irc.recv(1204) #gets output from IRC server
    user = data.split(':')[1]
    user = user.split('!')[0] #determines the sender of the messages
    print data
   
    if data.find('PING') != -1:
        irc.send(data.replace('PING', 'PONG')) #responds to PINGS from the server
   
    if data.find('!test') != -1: #!test command
        message('Phredd_ is awesome! :D')      
    if data.find('!hello') != -1: #!hello command
        if user == 'phredd_':
            mymessage = 'Hi, ' + user + ', you are awesome! :D'
            message(mymessage)
        else:
            mymessage = 'Hi, ' + user + ', you are a Butt! :D'
            message(mymessage)
           
    if data.find('!cmd') != -1 and user == 'phredd_': #adds a new command
        addsplit = channel + ' :' + '!cmd'
        command = '!' + data.split(addsplit)[1].split(' ')[1] #determines new command
        cmdmsg = ' '.join(data.split(addsplit)[1].split(' ')[2:]) #determines command output
        message('Command set!')      
    if data.find(command) != -1: #responds to the new command with the output
        message(cmdmsg)
    if data.find('!delcmd') != -1 and user == 'phredd_': #!deladd command
        command = '!notset'
        cmdmsg = 'This command is not set yet'
        message('Command deleted!')
       
    if data.find('!addnews') != -1 and user == 'phredd_': #!addnews command
        newssplit = channel + ' :' + '!addnews'
        newsmsg = ' '.join(data.split(newssplit)[1].split(' ')[1:]) #determines new news message
        message('News set!')
        newstimer() #starts news timer function    
    if data.find('!news') != -1 and user == 'phredd_': #!news command
        themessage = '[NEWS]: ' + newsmsg
        message(themessage)
    if data.find('!delnews') !=-1 and user == 'phredd_': #deletes the news
        newsmsg = 'No news set'
        message('News deleted!')
        newstimer()
       
    elif data.split(' ')[1] == 'PRIVMSG' and data.split(':')[2].startswith(nick):
        message('Who said my name? SwiftRage')

No comments:

Post a Comment