Jump to content

Python IRC (twitch) bot can send messages, view and respond to ping but not see messages

Joveice
Go to solution Solved by Dat Guy,

The OP seems to have decided to use PHP by now.

 

Hello,

 

I started creating a bot for Twitch, I'm currently facing a issue where the bot is unable to view messages and I have no idea how to fix it.

 

To start of, I'm in no way known with python as I'm focusing to learn php, c# and javascript.

This is my code, and it's not able to get any messages. I'm also posting on twitch's dev site for help about this.

CHAT_MSG = re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :")
chat(s, "Hello everyone!")

while True:
    response = s.recv(2048).decode("utf-8")
    print(response)
    if response == "PING :tmi.twitch.tv\r\n":
        s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8"))
    else:
        username = re.search(r"\w+", response).group(0)  # return the entire match
        message = CHAT_MSG.sub("", response)

        # Commands
        if message.strip() == "!what":
            chat(s, "sup it's me")
    sleep(1 / (20/30))

 

 

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

For a start, every packet you receive may contain multiple lines. You should be splitting the packets into lines. At the end of some packets, you might notice there's a bit "left over" which doesn't have a newline at the end. This "left over" belongs at the start of the next packet. Without this logic, you will have an unreliable and potentially vulnerable connection.

 

Additionally, your regex is wrong. \w+ is a greedy match for anything non-whitespace, so it will match the ! and everything following, too. I suggest starting with: "\:\w+ PRIVMSG #\w+ :" just to diagnose this issue. It's perfectly valid in IRC to send messages to nick!user@host; the server will split the username and hostname off for you.

Link to comment
Share on other sites

Link to post
Share on other sites

The OP seems to have decided to use PHP by now.

 

Write in C.

Link to comment
Share on other sites

Link to post
Share on other sites

53 minutes ago, Sebivor said:

For a start, every packet you receive may contain multiple lines. You should be splitting the packets into lines. At the end of some packets, you might notice there's a bit "left over" which doesn't have a newline at the end. This "left over" belongs at the start of the next packet. Without this logic, you will have an unreliable and potentially vulnerable connection.

 

Additionally, your regex is wrong. \w+ is a greedy match for anything non-whitespace, so it will match the ! and everything following, too. I suggest starting with: "\:\w+ PRIVMSG #\w+ :" just to diagnose this issue. It's perfectly valid in IRC to send messages to nick!user@host; the server will split the username and hostname off for you.

The issue was that I had forgotten a # in the join channel :P and yes this PHP part

For some reason did not get a notification from this.

5 minutes ago, Dat Guy said:

The OP seems to have decided to use PHP by now.

 

Yea I'm gonna do it there as long as it works :3 Forgot to update the post.

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×