Identify sending user, Python IRC

1.4k views Asked by At

So I made a Twitch.tv bot for my own channel, after having fun with it a little bit, I wanted to have some command restricted to some users, and some commands that can say the users name, for example:

Username reply example:

Person1: !tea
PythonBot: Would you like some tea, Person1?

Admin restriction example:

Person1: !ban Person2
PythonBot: I'm sorry, Person1, This command is restricted to admins only.

Ok, So here is the code I'm using (I will be modifying it soon to make it my own)

import socket
import threading



bot_owner = '~Not Today~'
nick = '~Not Today~'
channel = '~Not Today~'
server = 'irc.twitch.tv'
password = '~Not Today~'

queue = 13

irc = socket.socket()
irc.connect((server, 6667))

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 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:
    botdata = irc.recv(1204)
    botuser = botdata.split(':')[1]
    botuser = botuser.split('!')[0]
    print botdata

    if botdata.find('PING') != -1:
        irc.send(botdata.replace('PING', 'PONG'))
    if botdata.find('!res') != -1:
        irc.send(botdata.replace('!res', '1600x900'))
1

There are 1 answers

0
Jkm On BEST ANSWER

The twitch IRC raw message is like

:[email protected] PRIVMSG #trumpsc :needs Kappa

for above msg, it actually means userjkm at channel trumpsc saying needs Kappa

for your code, the method to get botuser is right, but you don't have the message the user sent, add following code should get the message

botmsg = botdata.split(':')[2]

so you get the message and username, the next step would be handling them. Here would be some example for your need. For me, I will prepared a adminuserList and commmandList for other command, but I will simplify it here.

def commmanHandler(botuser, botmsg):         # botmsg = '!ban user1'  
    command = botmsg.split(' ')[0]           # command = '!ban'
    command = command[1:]                    # command = 'ban'
    argu = message.split(' ')[1]             # argu = 'user1' 

    if command not in commmandList:         
        raise Exception("command not support")

    if command == 'ban':                     # ban command, or using switch
        # check if admin
        if botuser not in adminList:
            raise Exception("I'm sorry, " + botuser + ", This command is restricted to admins only.")
        # admin, able to ban
        irc.send('PRIVMSG' + channel + ' :' + '.ban ' + argu)

then call this function in your while loop to handle all message you get

try:
    commmanHandler(botuser, botmsg)
except Exception, e:
    print e
    irc.send('PRIVMSG' + channel + ' :' + e)

here would be my solution, and also, don't forget to give the bot moderator privilege.