Top

Basic PHP IRC Bot


PHP Code
+ 3 likes
Please Register to submit score.
Bookmark and Share
Average Score  7.8 (of 5 scores)
Date Added  Nov 08, 2008
Last Updated  Apr 05, 2009
Tags  bot  codes  furby  irc  irc bot  irc server  php  php irc bot  server  xstatic  xstatic codes 

Introduction

a basic IRC Bot with a few commands.

Commands are:

!sayit <Your message here> - The bot just repeats your message.
!md5 <your message here> - 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.

Grab the Code

<?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

  (29)  RSS
Soulkeeper
Comments: 80
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Nov 8, 2008 6:04 pm
I love it. ;D
F*U*R*B*Y*
Comments: 637
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Nov 8, 2008 6:46 pm
me loves my other one more =)

36 php pages does everything you could wish for =)
Hawkee
Comments: 1,039
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Nov 9, 2008 7:53 pm
This would make a very nice foundation for a much more advanced PHP IRC bot.
F*U*R*B*Y*
Comments: 637
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Nov 10, 2008 5:23 am
on my other one, the one with 36 php pages, it has a !hawkee function that gets last 10 comments (Or what it used to be ;)

was fun making them =)
sean
Comments: 109
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Nov 11, 2008 9:56 am
Quote:
double click it on windows, or on linux type php filename.php...
you can execute them both ways in either operating system via terminal or not
F*U*R*B*Y*
Comments: 637
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Nov 11, 2008 7:08 pm
true ;)
popdawg22
Comments: 1
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Nov 24, 2008 2:45 pm
Wow, extremely helpful. Thanks.
ODD
Comments: 11
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Jan 6, 2009 11:07 am
Furby how abt posting your 35 pages one :) if not maybe personally sned it 2 me
find me: AdA9 @ quakenet
curiousity of a PhP nub:D
F*U*R*B*Y*
Comments: 637
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Jan 7, 2009 12:17 am
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
ODD
Comments: 11
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Jan 7, 2009 5:34 pm
thanks furby goodluck with rewriting and waiting that script:)
fraguk
Comments: 20
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Jan 8, 2009 8:07 am
it connects to the irc server i put used quakenet, but it dont join channel ..
ODD
Comments: 11
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Jan 9, 2009 5:27 am
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
Quote:

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 :
Quote:
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:)
fraguk
Comments: 20
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Jan 9, 2009 6:59 am
thanks i'll give it a try now.
fraguk
Comments: 20
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Jan 9, 2009 1:12 pm
Thanks ODD that fixed it :)
ODD
Comments: 11
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Jan 11, 2009 4:02 pm
Glad its fixed:)
LucSatise
Comments: 113
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Jan 12, 2009 10:52 am
um when i double click it opens notepad for editing.. help?
F*U*R*B*Y*
Comments: 637
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Jan 13, 2009 12:30 am
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
bone282
Comments: 31
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Mar 5, 2009 10:23 am
Quote:

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
Hawkee
Comments: 1,039
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Mar 5, 2009 10:28 am
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.
bone282
Comments: 31
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Mar 5, 2009 12:34 pm
edited
killwithme
Comments: 20
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Apr 5, 2009 4:17 pm
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
killwithme
Comments: 20
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Apr 18, 2009 2:14 pm
wkdpstr
Comments: 1
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Apr 29, 2009 7:23 am
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
bone282
Comments: 31
 
PHP Snippet:  Basic PHP IRC Bot
Posted on May 2, 2009 2:13 pm
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);
killwithme
Comments: 20
 
PHP Snippet:  Basic PHP IRC Bot
Posted on May 3, 2009 5:16 am
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
bone282
Comments: 31
 
PHP Snippet:  Basic PHP IRC Bot
Posted on May 3, 2009 12:41 pm
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.
killwithme
Comments: 20
 
PHP Snippet:  Basic PHP IRC Bot
Posted on May 3, 2009 6:53 pm
I'm already doing this bone282,
but yeah I have to get a closer look to the socket functions
mr_president
Comments: 26
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Jul 15, 2009 8:02 pm
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
Gemster
Comments: 82
 
PHP Snippet:  Basic PHP IRC Bot
Posted on Aug 20, 2009 3:38 pm
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

Commenting Options

Register or Login to Hawkee.com or use your Facebook or Twitter account by clicking the corresponding button below.

  
Bottom