[PHP IRC] Place item, get new random.


Alright this is just a simple game where you message the bot with an item to place then it will return a random item stored in the database to the channel you specify.

Commands Added:
!placeitem *channel* *object or person* *person name or item name*
!additem *itemname*
!delitem *itemname*

New MySQL Table:
SQL Query
  1. CREATE TABLE `bot_items` (
  2. `id` INT( 11 ) NOT NULL auto_increment,
  3. `item_name` VARCHAR( 255 ) NOT NULL default '',
  4. `item_creator` VARCHAR( 255 ) NOT NULL default '',
  5. PRIMARY KEY (`id`)
  6. );

This will be the table that holds all of the items you wish to add.

Now, for the actual code.

We will start with the !additem and !delitem.
So, open your botExt.php and find:
botExt.php
  1. case '!botstat':
  2. fputs($this->mIRC_Socket, "PRIVMSG $user :Oh hai! this bot was coded as a tutorial for the RMB-Scripting community at rmb-scripting.com It was posted by MOD-Shadow.\n"); //simple bot descripion.
  3. break;

Add After:
botExt.php
  1. case '!additem':
  2. if($this->checkIdentified($user)){ //check if identified
  3. $newItem = null; //set null the newItem
  4. $datavars = count($data); //lets count number of $data items
  5. for($i = 4; $i <= ($datavars - 1); $i++){ //start at 4 and continue on
  6. if($data['4'] == null){ //theres only 1 space
  7. $newItem = $data['4'];
  8. }else{ //more then one
  9. $newItem = $newItem." ".$data[$i]; //lets add them all on
  10. }//end check for more then 1 word
  11. } //end loo[
  12. $newItem = str_replace(array("\r","\n"), '', $newItem); //lets get rid of some things to clean it up.
  13. $newItem = parent::sqlProtect($newItem);//protect item from SQL Injections
  14. $newItem = ltrim($newItem); //get rid of an unwanted space
  15. $getItem = parent::sqlQuery("SELECT * FROM `bot_items` WHERE `item_name` = '$newItem';"); //see if item already exists
  16. if(parent::sqlCount($getItem) == 0){ //doesnt exist
  17. $insertNewItem = parent::sqlQuery("INSERT INTO `bot_items` (`item_name`,`item_creator`) VALUES ('$newItem','$user');"); //insert the new item
  18. fputs($this->mIRC_Socket, "PRIVMSG $user :The item '$newItem' has been added.\n"); //your item was added
  19. }else{ //already exists
  20. fputs($this->mIRC_Socket, "PRIVMSG $user :The item '$newItem' is already in the database.\n");//you got an error :3
  21. }//end item check
  22. }else{ //not identified
  23. fputs($this->mIRC_Socket, "PRIVMSG $user :You are not identified or do not have proper permissions.\n");
  24. }
  25. break; //end !additem command


Now you can add items to your database. but that won't be much help if you cant !delitem >/ So lets add some more!

After that break add the following code!
PHP Code
  1. case '!delitem':
  2. if($this->checkIdentified($user)){ //check if identified
  3. $newItem = null; //clear new item variable
  4. $datavars = count($data); //count data
  5. for($i = 4; $i <= ($datavars - 1); $i++){ //start at root 4
  6. if($data['4'] == null){
  7. $newItem = $data['4']; //only 4 is present
  8. }else{
  9. $newItem = $newItem." ".$data[$i]; //more then 1 space for the item such as "TV Remote"
  10. }
  11. }
  12. $newItem = str_replace(array("\r","\n"), '', $newItem); //get rid of nasty characters which could mess up the database
  13. $newItem = parent::sqlProtect($newItem);//protect new item from SQL Injections
  14. $getItem = parent::sqlQuery("SELECT * FROM `bot_items` WHERE `item_name` = '$newItem';"); //Check if item exists
  15. if(parent::sqlCount($getItem) == 0){ //it doesnt
  16. fputs($this->mIRC_Socket, "PRIVMSG $user :The item '$newItem' does not exist.\n");//non existing item
  17. }else{
  18. $deleteItem = parent::sqlQuery("DELTE FROM `bot_items` WHERE `item_name` = '$newItem';"); //delete the item
  19. fputs($this->mIRC_Socket, "PRIVMSG $user :The item '$newItem' has been deleted.\n"); //tell them it was deleted
  20. }//end item check
  21. }else{//not identified
  22. fputs($this->mIRC_Socket, "PRIVMSG $user :You are not identified or do not have proper permissions.\n");
  23. }
  24. break; //end !delitem


Now for the final command addition. the action place item command! =]
PHP Code
  1. case '!placeitem':
  2. $item_sql = parent::sqlQuery("SELECT * FROM `bot_items` ORDER BY RAND() LIMIT 1"); //get a random item from the database
  3. $item = parent::sqlFetch($item_sql); //array the info
  4. $itemPlaced = null; //the item or person someone sent
  5. $datavars = count($data); //check how many words
  6. for($i = 6; $i <= ($datavars - 1); $i++){
  7. if($itemPlaced == null){ //only one word
  8. $itemPlaced = $data[6];
  9. }else{ //more then one word
  10. $itemPlaced = $itemPlaced." ".$data[$i];
  11. }//end word check
  12. }//end loop
  13. if($this->vowelCheck($itemPlaced['0'])){ //is it needing a or an
  14. $prefix = "an"; //needs an
  15. }else{ //needs a
  16. $prefix = "a";
  17. }
  18. if($this->vowelCheck($item->item_name)){ //check if needing a or an again
  19. $prefix2 = "an";
  20. }else{
  21. $prefix2 = "a";
  22. }
  23. $itemPlaced = str_replace("\r\n", "", $itemPlaced); //remove characters that could mess upthe message
  24. switch($data['5']){ //see if it is an object or pseron
  25. default:
  26. fputs($this->mIRC_Socket, "PRIVMSG $user :Invalid type.\n"); //invalid!
  27. break;
  28. case 'person': //it was a person
  29. fputs($this->mIRC_Socket, "PRIVMSG $data[4] :$user placed $itemPlaced into the magic machine and got $prefix2 ".$item->item_name.".\n"); //you inserted a person o:
  30. break;
  31. case 'object':
  32. fputs($this->mIRC_Socket, "PRIVMSG $data[4] :$user placed $prefix $itemPlaced into the magic machine and got $prefix2 ".$item->item_name.".\n"); //you inserted an object.
  33. break; //end check
  34. }//end switch
  35. break;//end placeitem command


Now for a quick run down.

TO use the commands you would need to /msg BotName the following examples.
/msg YourBot !additem a monkey
/msg YourBot !delitem a monkey
/msg YourBot !placeitem #YourChannel person Pamela Anderson

This will cause the bot to add/remove/generate a random result.

For the final example it will out put YourName has placed George Bush in the magic machine and get a monkey(or other item if there is more then one)

The final thing that needs to be added is the vowelCheck. place this before the final } in the file.

PHP Code
  1. function vowelCheck($string){
  2. $vowels = array("a","e","i","o","u","A","E","I","O","U"); //vowel characters
  3. $i=0; //current vowel
  4. while($i < count($vowels)) { //see how many there are
  5. if($string==$vowels[$i]){ //there is a vowel :3
  6. return true;
  7. }else{ //there isnt
  8. return false;
  9. }
  10. $i++; //lets move on :)
  11. }
  12. }//end function


That's it. I have updated the ZIP file on [02-21-09 1:55 PM EST]

The bot in the zip file has a few extra things now. if you are using a localhost such as UnrealIRCd and want it to auto-rehash (automatically reconfigure) your server set the 0 to one and change the path to your ircd configuration file.
ShadowMage's Avatar
Author:
Views:
14,935
Rating:
Posted on Monday 9th March 2009 at 03:16 PM
ShadowMage
ShadowMage's Avatar
I've been working on a new bot which will allow for better channel control and a cleaner database. I have to finish off the commands and some other minor details then I will update the ZIP file.

Users will be forced to have an account before access any commands. The bot will run complete with the database to allow users to manage the channels they registered.

Note; The bot MUST be channel owner in order to manage it. so, after you create your account, set your password and identify to it. After you identify to your bot you will be able to !join #YourChannel then !registerchan #YourChannel a_password a_description