Search code examples
pythonirctwitch

Python IRC Find status messages


Made for Twitch chat
I would like to search the irc chat for a certain message. The problem with my current code is that the message is actually a status and I have no clue on how to detect the status.

Code:

while True:
try:
    data = data+con.recv(1024)
    data_split = re.split(r"[~\r\n]+", data)
    data = data_split.pop()

    for line in data_split:
        print(line)
        line = str.rstrip(line)
        line = str.split(line)


        if len(line) >= 1:
            if line[0] == 'PING':
                send_pong(con, line[1])

            if line[1] == 'PRIVMSG':
                sender = get_sender(line[0])
                message = get_message(line)
                channel = line[2]
                print(sender + ": " + message)

                if (re.match(':jtv MODE \w* +o \w*', message)):
                    mods.append(channel)
                    mods[channel].append(msg[4])

The original status looks like this:
:jtv MODE #CHAN +o/-o #nick

I would also like to use the mods array in another file (if that is possible) to check if they are a mod or not. Otherwise I can write it to a .txt file.


Solution

  • Thanks for your help Antti Haapala. Found a solution for my structure:

     message = ' '.join(line)
     x = re.findall('^:jtv MODE (.*?) \+o (.*)$', message)
     if (len(x) > 0):
         #print('DEBUG: Regex')
         channel = x[0][0] 
         if (channel not in mods):
             mods[channel] = []
             list = mods.get(channel)
             list.append(x[0][1])
             print(mods)