Bot not sending data properly after a set time

Hello, i’ve trying to make the bot spam some messages each certain time but if i make this time long enough no messages are shown in the chat idk why, i’m sending pongs when i recieve the pings and even sending my own pings (and when i stop being able to talk to the chat i still get pings from server) so i’m a little lost…

I think that when i make this time a little bit longer than a minute it starts going wrong (and i dont want it spamming each minute!)

Thanks a lot

Hi,
Do you have an error in your console?
Aren’t you making too many requests on the API
As a basic reminder, you’re only allowed 30 requests per minute.

If you’re making this error, you’re making too many requests.
{
“error”: “Too Many Requests”,
“status”: 429,
“message”: “My ultimate is charging. (You have made too many requests. Please try again later.)”
}

429’s occur with the Helix API

Not chat.

With the oAuth changes about to come in thats 800 not 30

Chat bot’s don’t call the API to send chat messages. If you exceed the spam limit you just get disconnected.

If your bot is not a mod in the channel you will get a NOTICE with a relevant msg-id telling you you are sending too quickly, or the message is identical to the previous one, non mods must wait 30 seconds before re-sending the same message

You need to add logging for NOTICE messages and/or other Chat messaging types

I would like to answear both in the same message, but i dont know how to citate text as Barry did so i’ll try to explain (it goes for both of you :slightly_smiling_face:):

That “spam” has a very low frequency, one message each 10 minutes or so, very far away from the limit. The thing was that if the bot didnt send any message to the chat in a certain time (two secs), it became unable to send anymore (but it was still recieving pings). I was confused so i tried to force to keep that connection that seemed lost by sending an empty message each second, which worked, but its a very very unoptimal way to fix this. (instead of 1mesagge/ten minutes i send 600!) so i looked for something else but i couldnt…

If anyone has any idea where could be the mistake i made pls tell. Thank you!

Highlight the text and hit the quote button

So it’s not spam? When you say spam, we assume you mean to hammer the chat and spam it. 10 minutes or so is not spam.

Are you reply to the pings with a pong?

Sounds like you might have network conditions messing about with the connection, or you have something else going on that is stopping your code from working.

You may want to post your code so we can help debug (with secrets removed)

I meant spam, like add’s, but not exactly that, sorry for the confusion. Thanks in advance!

I think so…ill paste the code (ill try to comment the code so it is a little less messy)

import socket 
import time
import threading
import logging

TMI_TOKEN="xxxx"
CLIENT_ID="xxx"
BOT_NICK="xxx"
BOT_PREFIX="!"
CHANNEL="xxx"
continue=True
def recving(name,server):

#this function was supposed to read the chat and, in some time, make it so it could read commands and act acordingly
    
    readbuffer = ""
    logging.info("Thread %s: starting", name)
    print(server.recv(1024))

    while continue:
     
        readbuffer=str(server.recv(1024))
        
        if ("PING" in readbuffer):   # the answer to the ping if recieved
            server.send(bytes("PONG :tmi.twitch.tv",'utf-8'))

        else:
            pass  # this used to write comments the chat sent into the pythons command line, but 
                     # was buggy and i just commented it
            """readbuffer=readbuffer.split("PRIVMSG")
            if readbuffer[0].find("!")!=-1:
                name=readbuffer[0].split("!")[1]
                name=name.split("@")[0]
                
                msg=readbuffer[1].split(":")[1]
                print(name+" dijo '"+msg[:-5]+"'") """            
                      
        

        #print(readbuffer[0]+readbuffer[1])
    logging.info("Thread %s: finishing", name)

def spam_each_certaintime(name,server):  # this is the thing
     cont=0
     times=900*3     # to set the freq, (in this case, whenever cont is a multiple of 300 a 
                             # message is sent)
     mensaje3="Somethin"   
     mensaje="Another something"
     mensaje2="The last something "
     while continuar:
        if cont%times==0:
            
            server.send(bytes("PRIVMSG #"+ channel +" :" +mensaje2+ "\r\n",'utf-8'))
        if (cont-(900))%times==0:
    

            server.send(bytes("PRIVMSG #"+ channel +" :" +mensaje+ "\r\n",'utf-8'))
        if (cont-(1800))%veces==0:

            
            server.send(bytes("PRIVMSG #"+ channel +" :" +mensaje3+ "\r\n",'utf-8'))

        if cont%60==0:  # ping sent each 60 seconds
            server.send(bytes("PING :tmi.twitch.tv",'utf-8'))
            print("Send PING")
        else:
            if cont%2==0: # empty message sent to the chat to keep conn alive
                server.send(bytes("PRIVMSG #"+ channel +" :" + "\r\n",'utf-8'))
        cont=cont+1
        print(cont)
        time.sleep(1)
    
    print("stopped")
    
   

    # this down here arent suppose to mather in the error but anyway

stop_threads=False
conn_data=('irc.chat.twitch.tv',6667)
token=TMI_TOKEN
user=BOT_NICK
channel=CHANNEL
prefix=BOT_PREFIX
buffer=""

    if __name__ == "__main__":
    
    # create connection
    server=socket.socket()
    server.connect(conn_data)
    server.send(bytes('PASS '+token+' \r\n','utf-8'))
    server.send(bytes('NICK '+user+' \r\n','utf-8'))
    server.send(bytes('JOIN #'+channel+' \r\n','utf-8'))
    
    print(server)
    
    
    format = "%(asctime)s: %(message)s"
    logging.basicConfig(format=format, level=logging.INFO,datefmt="%H:%M:%S")
   
    # start threads
    logging.info("Main    : before creating thread")
    x = threading.Thread(target=recving, args=(1,server,))
    y = threading.Thread(target=spam_each_certaintime, args=(2,server))
    logging.info("Main    : before running thread")
    x.start()
    y.start()
    logging.info("Main    : wait for the thread to finish")
    # x.join()
    
    
    while 1:
        pass

Ah I think the issue here is you are not terminating your lines with a CR + LF (\r\n) per the IRC spec.

So you are not ping/ponging or sending messages right.

You terminated the PASS/NICK/JOIN but not your PONG’s

1 Like

Sorry for my wrong answers.
Thanks for correcting me @BarryCarlyon I’ll know more now.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.