Forgot Password / Register
Site Statistics
Total Members: 506
Total Tutorials: 242
Newsest User: Williamhawk1
Todays Unique Hits: 63
0 Users 1 Guests Online

[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:
1
2
3
4
5
6
CREATE TABLE `bot_items` (
`id` INT( 11 ) NOT NULL auto_increment,
`item_name` VARCHAR( 255 ) NOT NULL default '',
`item_creator` VARCHAR( 255 ) NOT NULL default '',
PRIMARY KEY (`id`)
);

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:
1
2
3
case '!botstat':
                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.
                break;

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


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

1
2
3
4
5
6
7
8
9
10
11
12
function vowelCheck($string){
$vowels = array("a","e","i","o","u","A","E","I","O","U"); //vowel characters
$i=0; //current vowel
while($i < count($vowels)) { //see how many there are
if($string==$vowels[$i]){ //there is a vowel :3
return true;
}else{ //there isnt
return false;
}
$i++; //lets move on :)
}
}//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
Author:
Views:
2260
Rating:
Posted on Monday 9th March 2009 at 03:16 PM
ShadowMage
ShadowMage
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