Basic PHP IRC Bot

By F*U*R*B*Y* on Nov 08, 2008

a basic IRC Bot with a few commands.

Commands are:

!sayit - The bot just repeats your message.
!md5 - The bot just says the md5 sum of your message.

How to run it, you change the variables to which i said to edit. then you save it as whatever you want.php then just double click it on windows, or on linux type php filename.php...

you must have php installed.

<?php
// Prevent PHP from stopping the script after 30 sec
set_time_limit(0);

// Edit these settings
$chan = "#php";
$server = "127.0.0.1";
$port = 6667;
$nick = "PHP_Bot";

// STOP EDITTING NOW.
$socket = fsockopen("$server", $port);
fputs($socket,"USER $nick $nick $nick $nick :$nick\n");
fputs($socket,"NICK $nick\n");
fputs($socket,"JOIN ".$chan."\n");

while(1) {
    while($data = fgets($socket)) {
            echo nl2br($data);
            flush();

            $ex = explode(' ', $data);
        $rawcmd = explode(':', $ex[3]);
        $oneword = explode('<br>', $rawcmd);
            $channel = $ex[2];
        $nicka = explode('@', $ex[0]);
        $nickb = explode('!', $nicka[0]);
        $nickc = explode(':', $nickb[0]);

        $host = $nicka[1];
        $nick = $nickc[1];
            if($ex[0] == "PING"){
                fputs($socket, "PONG ".$ex[1]."\n");
            }

        $args = NULL; for ($i = 4; $i < count($ex); $i++) { $args .= $ex[$i] . ' '; }

            if ($rawcmd[1] == "!sayit") {
                fputs($socket, "PRIVMSG ".$channel." :".$args." \n");
            }
        elseif ($rawcmd[1] == "!md5") {
            fputs($socket, "PRIVMSG ".$channel." :MD5 ".md5($args)."\n");
        }
    }
}
?>

Comments

Sign in to comment.
one   -  May 29, 2016

what if you give this bot a kick and banning functionality and have a specific owner to use the command.

 Respond  
F*U*R*B*Y*   -  Mar 05, 2014

chuck in a

elseif ($rawcmd[1] == "hi") { fputs($socket, "PRIVMSG ".$channel." :Your message goes here\n"); }

 Respond  
I_Is_Payton_   -  Mar 03, 2014

How would you make it say something if a user sends "Hi" in chat?

 Respond  
x1L22   -  Jun 06, 2011

drew my attention :)

 Respond  
Soulkeeper   -  Jun 03, 2011

People still comment on this? It's like 3 years old. Lol.

 Respond  
x1L22   -  May 20, 2011

How It Works ?

 Respond  
F*U*R*B*Y*   -  May 12, 2011

command prompt,

cd C:\Location\To\Bot\
php bot.php

 Respond  
tROOP4H   -  Apr 30, 2011

Can someone tell me how to start in in Windows? The thing is, that i cant just dubbleclick it, then I get the texteditor. And if i open it in the browser (I got Xampp), it connects to IRC (quakenet), but not to the chan, and it requires me to always keep my browser open. Not an option as you might figure :P

 Respond  
jackster35-1   -  Mar 21, 2010

I love it thanks dude its my first php bot but how do i make him join a channel?

 Respond  
Gemster   -  Aug 20, 2009

Hi, first off im new to php sockets and secondly i run this from my website and it works fine.

My question is how do i add more commands ?

I have tryed to copy the triggers and rewrite them with no sucess :/

Commands for an example a !join $chat trigger.

Thanks
Gemster

 Respond  
mr_president   -  Jul 15, 2009

WOW!!!
It's very nice man...
Lol just don't run it on a external webserver that uses linux and on a irc server that you dont own because then you will have to restart the webserver =) it just kept running the php for along time...
Lol

 Respond  
killwithme   -  May 03, 2009

I'm already doing this bone282,
but yeah I have to get a closer look to the socket functions

 Respond  
bone282   -  May 03, 2009

by closing the socket. try using socket_ functions in php. example here http://www.hawkee.com/snippet/6076/

by the way killwithme, if you launch the script with a .bat file it uses cmd.exe as the bot window.

 Respond  
killwithme   -  May 03, 2009

when run via command line, a CTRL + C also causes a "connection reset by peer" message,
I wonder how I can make a "Client exited" message...
mean peer.. always resets my connection :D

 Respond  
bone282   -  May 02, 2009

if you are getting "connection reset by peer". try running it NOT from your brower. just drop it in the same dir as php.exe and run from there. or add:

/ Prevent Script Stopping. /
ignore_user_abort(true);
set_time_limit(0);

 Respond  
wkdpstr   -  Apr 29, 2009

im quite noob so bare with me. when i start it , it will be on the channel few minutes and then it will quit. when i put it into empty channel it stays few hours :S

 Respond  
killwithme   -  Apr 18, 2009
 Respond  
killwithme   -  Apr 05, 2009

bone282:
when I use this, the MD5 will always be the same?!

did i copy something wrong?

/edit: oh, the $args string was in singlequotes
I did'nt see that

 Respond  
bone282   -  Mar 05, 2009

edited

 Respond  
Hawkee   -  Mar 05, 2009

I'm currently reworking the script section in order to allow for PHP script uploads. Once that's complete you'll be able to maintain a version there.

 Respond  
bone282   -  Mar 05, 2009

Hawkee:
This would make a very nice foundation for a much more advanced PHP IRC bot.

Would it be possible to make a forum topic or section for this bot. with download links for the complete stand alone versions? Because, i would like to help develop it into a huge beast :P

 Respond  
F*U*R*B*Y*   -  Jan 13, 2009

Windows: right click it, and go open as.. then select the PHP CGI or what ever it is....

Linux: Open up a terminal window, navigate to the dir where the php irc bot is, then type php filename.php

 Respond  
LucSatise   -  Jan 12, 2009

um when i double click it opens notepad for editing.. help?

 Respond  
ODD   -  Jan 11, 2009

Glad its fixed:)

 Respond  
fraguk   -  Jan 09, 2009

Thanks ODD that fixed it :)

 Respond  
fraguk   -  Jan 09, 2009

thanks i'll give it a try now.

 Respond  
ODD   -  Jan 09, 2009

some networks do not allow instant channel join so you would have to use a while loop before you join the channel with a sleep in it and some pong back inorder not to get disconnected from server

while($logincount < 10) {
$logincount++;
$data = fgets($socket, 128);
echo nl2br($data);

// Separate all data
$ex = explode(' ', $data);

// Send PONG back to the server
if($ex[0] == "PING"){
fputs($socket, "PONG ".$ex[1]."\n");
}
flush();
}
sleep(1);

this should be placed right under the :

fputs($socket,"NICK $nick\n");

NOTE to furby :I guess you should use some sleep commands inorder to save the pain from cpu leaks and server pressure a "sleep(1);" would be good but if you want you can use "usleep():" for milliseconds:)

 Respond  
fraguk   -  Jan 08, 2009

it connects to the irc server i put used quakenet, but it dont join channel ..

 Respond  
ODD   -  Jan 07, 2009

thanks furby goodluck with rewriting and waiting that script:)

 Respond  
F*U*R*B*Y*   -  Jan 07, 2009

ODD, atm, I'm rewriting it to make it so others can have it, and have it easy. but I'll be sure to let you have it once its rewritten

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.