IRC bot failing to connect or receive data

Hey, I’m trying to build a simple IRC bot withing the software GameMaker Studio, my eventual plan is to create a game around Twitch chat interactions.

However, I’m having problems either connecting or receiving data. The problem I’m having is that after sending PASS, NICK, and JOIN messages I’m not receiving any data back, no messages about failing to connect or anything like that.

I’ll post my code below, the language GameMaker uses is very simple so if you have any programming experience it should be relatively simple to understand, I’ve also commented it as well.

Create Event (ran once on game start)

//Create a TCP socket
irc_socket = network_create_socket(network_socket_tcp);

//Connects to a server using a previously created socket
irc = network_connect_raw(irc_socket, "irc.twitch.tv", 6667);

//If the connection returns < 0 then it did not successfully connect
if (irc < 0)
{
    trace("connection failed");
}
else
{
    trace("connection successful");
}

//Creates a buffer that can dynamically change size when needed
//Set the write position to the start of the buffer
//Write the following string into the buffer
//Send a raw packet to a socket with a buffer and buffer size (non raw connections use an extra 8 byte header for some custom GameMaker thing)
//Destroy the buffer afterwards

var buff = buffer_create(1024, buffer_grow, 1);
buffer_seek(buff, buffer_seek_start, 0);
buffer_write(buff, buffer_string, "PASS oauth:shhhh...it's a secret\r\n");
network_send_raw(irc_socket, buff, buffer_get_size(buff));
buffer_delete(buff);

var buff = buffer_create(1024, buffer_grow, 1);
buffer_seek(buff, buffer_seek_start, 0);
buffer_write(buff, buffer_string, "NICK aidanstwitchbot\r\n");
network_send_raw(irc_socket, buff, buffer_get_size(buff));
buffer_delete(buff);

var buff = buffer_create(1024, buffer_grow, 1);
buffer_seek(buff, buffer_seek_start, 0);
buffer_write(buff, buffer_string, "JOIN #aidan63\r\n");
network_send_raw(irc_socket, buff, buffer_get_size(buff));
buffer_delete(buff);

Network Async Event (Code in this event is triggered when GameMaker detects incoming data)

trace("triggered");

var data = ds_map_find_value(async_load, "buffer");
var str  = buffer_read(data, buffer_string);

trace(str);

irc returns a value greater than 0 indicating a successful connection since “connection successful” appears in the debug dialog however, the network async event is never triggered meaning no data is ever received.

If I change the connecting URL to another IRC network like “irc.slashnet.org” and add a USER message then I can connect to it and receive data back. From my understanding Twitch IRC doesn’t need a USER message. (adding one idn’t change anything anyway)

I’m also pretty sure the credentials are all entered correctly as I was able to make an equivalent IRC bot in Python and it successfully connected and received data back. One solution would be to create the bot in Python and save all relevant data to a file so GameMaker can read it, but I would like to do it all in GameMaker if possible.

I’ve asked in a few GameMaker communities and there doesn’t seem to be anything wrong with the code, they suggested it might be something on Twitch’s end, so now I’m here to ask you guys!

Any help would be greatly appreciated, cheers.

Have you tried a different port besides 6667 such as 80 or 443? There are 4 servers in the irc.twitch.tv DNS that do not allow connections via port 6667.

(See the list on http://twitchstatus.com/)

I’ve tried all three ports and nothing changes :confused:

This is actually quite a simple fix. I downloaded GameMaker Studio and began messing around. I quickly noticed that GameMaker Studio does not understand escaped characters. You must instead use the chr function to send CR and LF through the socket:

var buff = buffer_create(1024, buffer_grow, 1);
buffer_write(buff, buffer_string, "NICK justinfan423452345"+chr(13)+chr(10));
network_send_raw(irc_socket, buff, buffer_get_size(buff));
buffer_delete(buff);

Character 13 in base 10 is ASCII for carriage return (CR), and character 10 in base 10 is ASCII for line feed (LF).

1 Like

Thank you! That makes a lot of sense and it is now working!

I’ve never really used escaped characters that much so I never thought of checking them.

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