Search code examples
pythonstringtwitchchatbot

Convert Data Pulled from Server To String


I have been working on a TwitchTV Python chat bot for a while now, but I'm still getting to grips with Python.

It may seem simple, but this has confused me so I decided I would ask:

I'm currently pulling messages from Twitch Chat using data = irc.recv

What I want to do is use the data pulled and turn it into a string, so that I can then check for capitals in the messages using str.isupper()

I've already tried a few ways;

data = irc.recv (4096)
msg = data()
capsTime = "30s"
str = msg
if str.isupper():
   message("[-] Woah! Hold back on the caps! (Timeout " + capsTime + ")")
   message("/timeout " + user + capsTime)
# variable "user" already defined

This is just one, that unfortunately didn't work.


EDIT:

This is my new code, It runs without error messages, but It doesn't function as I want it to;

while True:
   data = irc.recv (4096)
   user = data.split(':')[1]
   user = user.split('!')[0]
   caps = data.split(':')[0]
   capsmsg = str(data)

   print data
   if data.find('PING') != -1: 
       irc.send('PONG ' + data.split()[1] + '\r\n')

   if capsmsg.isupper():
       message("[-] Woah! Hold back on the caps, " + user + "! (Timeout 30s)")
       message("/timeout " + user + " 30s")

EDIT 2:

Expected output: If a message is found in ALL caps, it will print this message and time the user out: message("[-] Woah! Hold back on the caps, " + user + "! (Timeout 30s)")

Current output: The bot does not pick the message up or run the scripted code.


Solution

  • Try this:

    data = irc.recv (4096)
    # msg = data()
    capsTime = "30s"
    mystr = repr(data)
    if mystr.isupper():
       message("[-] Woah! Hold back on the caps! (Timeout " + capsTime + ")")
       message("/timeout " + user + capsTime)
    # variable "user" already defined
    

    Don't use reserved keyword.