Building a bot to handle Raffles on your Twitch channel.

in #games7 years ago (edited)

     Recently I was talking with a Twitch streamer that is helping me develop a bot for Twitch (Working title to the bot is "tilthack") and they were telling they wanted to start having raffles on their channel for their viewers. With this in mind I started working towards adding this feature to the python bot I am creating. I wanted to share this portion of the bot with you guys. The main bot is still a work in progress and will be released here and on Github when finished.

Anyways lets get started.

First thing to do if you have not yet is create an account for your bot then go to https://twitchapps.com/tmi/ and generate an OAuth key to use with the python script below.

The only change needed is the following section:

channelname = 'channel here' #Set the channel name here 
nick = 'botaccountname'  # Place bot name here
password = 'bots oath goes here'



This is all that is needed to be change from the script below:

import socket,threading,random,os

#Want more hit up tilt at http://www.twitch.tv/tilt41

# Only change these settings
channelname = 'channel here' 
nick = 'botaccountname'  
password = 'bots oauth here' 
# Do Not change anything below unless you know what your doing

queue = 13 
channel = '#'+channelname
server = 'irc.twitch.tv'
irc = socket.socket()
irc.connect((server, 6667)) 
irc.send('PASS ' + password + '\r\n')
irc.send('NICK ' + nick + '\r\n')
irc.send('JOIN ' + channel + '\r\n')
rafflelist = []
beginraffle = "Entries for the raffle have started. Type !raffle  to join now!!"
print beginraffle
irc.send('PRIVMSG ' + channel + ' :' + beginraffle + '\r\n')
def rafflesave():
    rafflelist.append(user)
def run_raffle():
    print rafflelist
    winner = random.choice(rafflelist)
    rafflewinner = winner + " is the winner!! :)"
    irc.send('PRIVMSG ' + channel + ' :' + rafflewinner + '\r\n')
    print winner + ' won the raffle!!!'
    os._exit(0)
def message(msg):
    global queue
    queue = 5
    if queue < 20: 
        irc.send('PRIVMSG ' + channel + ' :' + msg + '\r\n')
    else:
        print 'Message deleted'
def queuetimer(): 
    global queue
    queue = 0
    threading.Timer(30,queuetimer).start()
queuetimer()
while True:
    tilthack = irc.recv(1204)
    user = tilthack.split(':')[1]
    user = user.split('!')[0] 
    if tilthack.find('PING') != -1:
        irc.send(tilthack.replace('PING', 'PONG')) 
    if tilthack.find('!raffle') != -1: 
            if any(word in user for word in rafflelist):
                message(user + ' has already entered :)')
            else:
                rafflesave()
                message(user + ' has been added to the raffle :) '+ str(len(rafflelist)) + ' user(s) have joined the raffle.')
                print rafflelist 
                print len(rafflelist), 'user(s) have joined'
    if tilthack.find('!runraffle') != -1:
        run_raffle()

When you run the python script the bot will start accepting entries to the raffle as soon as the bot connects to IRC.

Your viewers will use !raffle to join the raffle.

When you are ready to run the raffle you will use !runraffle

I hope you all find this useful.