Member Avatar for powerade661

Hello Everyone,

I am having issues with this current script. I am trying to create a Twitch bot in Python. Any help with this would be much appreciated. The issue that I am having is it is not replying back with the message Hello there, when I type in "Hello"

import string

from Socket import openSocket, sendMessage
from Initialize import joinRoom
from Read import getUser, getMessage

s = openSocket()
joinRoom(s)
readbuffer = ""

while True: 
    readbuffer = readbuffer + s.recv(1024)
    temp = string.split(readbuffer, "\n")
    readbuffer = temp.pop()

    for line in temp: 
            print(line)
            if "PING" in line:
                s.send(line.replace("PING", "PONG"))
                break
            user = getUser(line)
            message = getMessage(line)
            print user + " typed :" + message
            if "Hello" in message:
                sendMessage(s, "Hello there")

Does the socket actually receive the hello data ? You could log things

log = log_to_file('bot', 'bot.log')

while True:
    r = s.recv(1024)
    log.info('socket received: ' + repr(r))
    readbuffer = readbuffer + r
    # etc ...

You can use this function as a black box

def log_to_file(theLogger, fileOrFileobj, timefmt=""):
    """Connects a logger to a file with a default formatter.
    Passing timefmt = None discards the time in the output
    """
    import logging
    basestr = str if sys.version_info >= (3,) else basestring
    if isinstance(theLogger, basestr):
        theLogger = logging.getLogger(theLogger)
    theLogger .setLevel (logging .DEBUG )
    if fileOrFileobj is None:
        theHandler = logging.StreamHandler() # console
    elif hasattr(fileOrFileobj, "write") and hasattr(fileOrFileobj, "flush"):
        theHandler = logging.StreamHandler(fileOrFileobj)
    elif isinstance(fileOrFileobj, basestr):
        theHandler =logging .FileHandler (fileOrFileobj ,'w')
    else:
        raise ValueError("Expected file object or system path")
    prefix = "" if timefmt is None else "%(asctime)s"
    fmt = prefix + "%(levelname)s %(filename)s [%(lineno)d] %(message)s"
    theFormatter =logging .Formatter (fmt, 
        "%H:%M:%S " if timefmt == "" else ("" if timefmt is None else timefmt))
    theHandler .setFormatter (theFormatter )
    theLogger .addHandler (theHandler )
    return theLogger
Member Avatar for powerade661

Yeah it does. It is not working because of Twitch's CAP req. Specifically the CAP req twitch.tv/commands. I can't just remove this, because this is how the bot knows who joins and who leaves and also determines if they are moderators or not. When I remove CAP req twitch.tv/commands everything works fine. This issue is weird. I hate that Twitch switched to this, because now everything is thrown off...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.