setting $values and repeating them

I’m in the process of making a bot for a chat and I’m having some difficulties. What I did in it was this:

                    case '!automember':
            	    	if((($data['id'])==$botowner) && ($command[1]=="on")) {
            	    	    $automem = "on";
            	    	    $this->sendMessage("Auto Membering is on.");
            	    	}
            	        elseif((($data['id'])==$botowner) && ($command[1]=="off")) {
            	            $automem = "off";
            	    	    $this->sendMessage("Auto Membering is off.");
            	    	}
                    break;

The chat command is
!automember on
!automember off

where ! represents a bot command, the word after tells it which case, and anything after is the same as $command[1]

so basically,
!casecommand $command[1]
(If that makes sense)

The problem I’m having is with setting the $automem and then basically echoing it again later in the script.

To set it I do this
$automem = “on”; (or off)

And saying the value later i just do
$automem
but it never shows.

I need to figure out how to set a value (with $automem or whatever else i would use) and then say what that value is again.
Also, I need the part that says $botowner to get fixed too but once i figure it out i should be able to fix that. the $automem will be text and $botowner will be numeric (if they need to be different)

Sorry for the long post I was just explaining how the other things work with it.

[table]
[tr]
[td][php] case ‘!automember’:
if((($data[‘id’])==$botowner) && ($command[1]==“on”)) {
$automem = “on”;
$this->sendMessage(“Auto Membering is on.”);
}
elseif((($data[‘id’])==$botowner) && ($command[1]==“off”)) {
$automem = “off”;
$this->sendMessage(“Auto Membering is off.”);
}
break;[/php][/td]
[/tr]
[/table]

If that’s better to read

first thing lets clean this code up a little:
[php]
case ‘!automember’:
if($data[‘id’]==$botowner && $command[1]==“on”) {
$automem = “on”;
$this->sendMessage(“Auto Membering is on.”);
}
elseif($data[‘id’]==$botowner && $command[1]==“off”) {
$automem = “off”;
$this->sendMessage(“Auto Membering is off.”);
}
break;
[/php]
did it ever send the message and just left $automem empty or did the code just completely fail?
have you tried using “continue;” instead of break so that you would continue the execution, which would allow the variable to continue throughout the rest of the script?(just guessing 8))

just food for thought, if you are using case switches you do not have to break; I am sure a lot of people only use case and break together because that is how they see it written everywhere but if you are only sending one case value to the script, and you have more to execute on that script you can continue; I hope this thought helps a few more people out in other situations!

i did clean it up with the extra () that were there. i also tried using ‘continue;’ instead of ‘break;’ but it still wont work. when i issue the command in the chat it responds with the message that its on/off but it still wont do the action.

Here’s what i put for the membering part (in case its that)

[php]if("$info[rank]"==“guest” && “$automem”==“on”)
$this->member($data[‘id’]);[/php]

also, i need to know how to set $botowner as a couple numbers (user ID’s) and then i can use $botowner in the script for certain commands so only certain users can use the commands. i dont want to have to put all the ID’s on every command when i could put all of them in one set and put that everywhere and then only have to change the one for them all to change (if you got that)

so something like
$botowner = (“123”, “124”); (if these users are the ones that are wanted to be an owner of the bot)

and thenim not sure with saying it again in the script if its $botowner or ‘$botowner’ or “$botowner”

also, whats the difference between ‘’ and “”

sorry for so many questions im a noob learning php and i just have a lot of questions :stuck_out_tongue:
im pretty good with coding i just dont know any of the languages except batch so im tryin to step up and work my way up to C++ and all that good stuff :stuck_out_tongue:

ok i verified that $automem is being set by changing

[php]$this->sendMessage(“Auto Membering is on.”);[/php]
to
[php]$this->sendMessage(“Auto Membering is “.$automem.”.”);[/php]

the thing its not doing is carrying it on through the whole script. i added a second command underneath it

[php]case ‘!autommemberstatus’:
$this->sendMessage("Auto membering is currently ".$automem);
break;[/php]

This just showed in the chat as
"Auto membering is currently " without on or off

so I’ve came to the conclusion that it Isn’t carrying the set value through the whole script (and i did change break to continue)

oh I think I see what is going on you have the one case that sets autmem, but you are expecting it to be carried through to the other cases? why dont you get rid of the case since you need it for other parts of the script and just have :
[php]
if($data[‘id’]==$botowner && $command[1]==“on”) {
$automem = “on”;
$this->sendMessage(“Auto Membering is on.”);
}
elseif($data[‘id’]==$botowner && $command[1]==“off”) {
$automem = “off”;
$this->sendMessage(“Auto Membering is off.”);
}[/php]
up near the top of the script?
as for $botowner use array:

[php]
$botowner=array(‘1’, ‘2’, ‘3’, ‘4’);
if (in_array($userid, $botowner)) {
//do something
}
[/php]

i have the case there because the very first line tells the bot that a command is being said in the chat
example:

case ‘!command’:
yadda
yadda
yadda
break;

that tells the bot that whenever the message !command is said in the chat, do yadda yadda yadda and stop with that

for the botowner i was using the if command because i cant just put the case ‘!command’: anywhere i have to put it in one of the sections

if this helps, the source code is on pastebin if you want to look at it. its very basic and i added most of the commands by tinkering around with it but if it will help (im sure it will) then its located here:
http://pastebin.com/HqCA2CLP

I got my $botowner problem fixed too, just thrown that out there

well at least i thought it did…
ill keep messin around with it and bring it up again after i get the switch fixed if it needs fixed at all

ok I see what you mean here but I do not see the script where you are trying to use:
[php]
case ‘!automember’:
if($data[‘id’]==$botowner && $command[1]==“on”) {
$automem = “on”;
$this->sendMessage(“Auto Membering is on.”);
}
elseif($data[‘id’]==$botowner && $command[1]==“off”) {
$automem = “off”;
$this->sendMessage(“Auto Membering is off.”);
}
break;
[/php]
I would like to see the script where and how you are trying to use that so I can give you another view of how to write it.
I am just curious where and how you are trying to implement it but I dont see that in the example script you sent

yeah i added a lot of it in

heres the top half of mine
[php]<?php

/**

  • @author 3nvisi0n
  • @copyright 2011
  • This is just a basic version of my Xat Bot you can build upon to add the feature you want.

*/
set_time_limit(0);//No time out
$bot = new BasicXatBot();
$botowner = array(‘320467393’);
$roomID = 150355757;//Don’t know how to get this ID? goto xat.com/ROOM_YOU_WANT_BOT_IN and View Source(Firefox: Ctrl+U, Internet Explorer: View>Source) Look for "flashvars=“id=#########&…” the ID is the number you want
$bot->connect(“174.36.242.26”,“10024”); //Connect to XAT…this IP will change as necessary automatically depending on the room you want to join.
$bot->join($roomID);

class BasicXatBot {
private $soc; //Socket for bot
private $debug = false; //Used to toggle debugging output.
private $packet; //Stores information about the last recieved packet of each kind
private $userInfo; //Stores User information

//If you don't know where to get these you should probably not be trying to make your own bot.
//Go download WPE Pro and check out the packets XAT sends and learn about how XAT works.
//The UserID and K Value are the source or the 'old falsh' error(if you get it) make sure you get these values from a REGISTERED account

private $userID = "351599155";  //The Bot's UserID
private $k = "3607438505";      //The Bot's K value

//Bot Account Settings
private $name = "DuBot";         //The display name the bot will use
private $avatar = "http://www.instablogsimages.com/images/2009/01/08/joe-bot-inflatable-robot_NBz64_6648.jpg";           //The Avatar value for the bot, this can be an id number or an image url.
private $homepage = "";         //The bot's homepage link.
private $roomID;                //This gets set in code, don't touch it here. just used ->join(ID)


/**
 * This is where everything the bot does needs to happen, this is the only default function you should need to edit
 * @param $event The event that just occured(and thus needs to be handled) 
 * @param $data Any data relating to this event.
 */
function handleEvent($event,$data) {
   $info = $this->getUserArray($data['id']);
   switch($event) {
   case 'userJoined': {
         /* $data['id']             Has the ID of the user who just joined
            $data['old']            Sometimes XAT sends packets that are not current(for example when you join a room, old ==true for all users who are already in the room when you join, but still this event is handled as thought they just joined */
            
            //Do whever you want with users joining here...
            $this->sendPrivateMessage("Welcome to the chat! Please follow the rules and enjoy your stay. :)",$data['id']);
            if("$info[rank]"=="guest" && "$automem"=="on")
                $this->member($data['id']);
        	if($info['name']=='')
        	    $this->kick("NULL",$data['id']);
            echo ((trim($info['registeredName'])!='')?$info['registeredName']:$info['name'])."($info[rank]) has just joined.\n";
        break;
   }
        case 'userLeft':
            /* $data['id']     The ID of the user that just left. */                
            echo ((trim($info['registeredName'])!='')?$info['registeredName']:$info['name'])."($info[rank]) has just left.\n";
        break;
        case 'privateMessage':
            /* $data['id']      The ID of the user that just left.
               $data['message'] The message sent to you as a PM */
            echo "[PM] ".((trim($info['registeredName'])!='')?$info['registeredName']:$info['name'])."($info[rank]) -> $data[message]\n";
            
            //Example of a private message command
            $command = explode(' ',$data['message'],2); //First parse the first word out see if it is a command...
            //[0] has first word [1] has everything else
            if($command[0]{0}=='!'){//I am use ! as the character to signify a command, so check if the first character is right.
                switch($command[0]) {
                }
            }
        break;
        case 'privateChat':
            /* $data['id']      The ID of the user that just left.
               $data['message'] The message sent to you as a PC */
            echo "[PC] ".((trim($info['registeredName'])!='')?$info['registeredName']:$info['name'])."($info[rank]) -> $data[message]\n";
            
            //Example of a private chat command
            $command = explode(' ',$data['message'],2); //First parse the first word out see if it is a command...
            //[0] has first word [1] has everything else
            if($command[0]{0}=='!'){//I am use ! as the character to signify a command, so check if the first character is right.
                switch($command[0]) {
                    case '!unbanme':
                        if(($data['id'])==$botowner)
                            $this->unban($data['id']);
                    break;
                }
            }
        break;
        case 'message':
            /* $data['id']      The ID of the user
               $data['old']     See $data['old'] under userJoined
               $data['message'] The message sent to main chat */
            echo ((trim($info['registeredName'])!='')?$info['registeredName']:$info['name'])."($info[rank]) -> $data[message]\n";
            
            //How to do main chat commands: 
            if($data['old'])   return; //Old message
            $command = explode(' ',trim($data['message']),2); //First parse the first word out see if it is a command...
            //[0] has first word [1] has everything else
            if($command[0]{0}=='!'){//I am use ! as the character to signify a command, so check if the first character is right.
                switch($command[0]) {
                	case '!commands':
                	    $this->sendPrivateMessage("!guestme  |  !memberme  |  !kickme  |  !banme [seconds]  |  !info",$data['id']);
                	break;
            	case '!say':
            	    if(($data['id'])==$botowner)
            	    	$this->sendMessage($command[1]);
            	    else
            	    	$this->sendMessage("Don't tell me what to say (D)",$data['id']);
                    break;
            	case '!automember':
            	    if($data['id']==$botowner && $command[1]=="on") {
            	    	$automem = "on";
            	    	$this->sendMessage("Auto Membering is ".$automem.".");
            	    }
            	    elseif($data['id']==$botowner && $command[1]=="off") {
            	    	 $automem = "off";
            	    	 $this->sendMessage("Auto Membering is ".$automem.".");
            	    }
                    continue;
            	case '!ams':
            	    $this->sendMessage("Auto membering is currently ".$automem);
            	break;
            	case '!guest':
            	    if(($data['id'])==$botowner)
            	    	$this->guest($command[1]);
                    break;
            	case '!member':
            	    if(($data['id'])==$botowner)
            	    	$this->member($command[1]);
                    break;
            	case '!mod':
            	    if(($data['id'])==$botowner)
            	    	$this->mod($command[1]);
                    break;
            	case '!kick':
            	    if(($data['id'])==$botowner)
            	    	$this->kick("Dubz told me to... (WARY)",$command[1]);
                    break;
            	case '!ban':
            	    if(($data['id'])==$botowner)
            	    	$this->ban("Dubz told me to... (WARY)",$command[1],600);
                    break;
            	case '!unban':
            	    if(($data['id'])==$botowner)
            	    	$this->unban($command[1]);
                    break;
                    case '!guestme':
                        $this->guest($data['id']);
                    break;
                    case '!memberme':
                        $this->member($data['id']);
                    break;
                    case '!modme':
            	    if(($data['id'])==$botowner)
            	    	$this->mod($data['id']);
            	    else
            	    	$this->sendMessage("Yeah, right. (SMIRK)",$data['id']);
                    break;
                    case '!moderatorme':
            	    if(($data['id'])==$botowner)
            	    	$this->mod($data['id']);
            	    else
            	    	$this->sendMessage("Yeah, right. (SMIRK)",$data['id']);
                    break;
                    case '!ownerme':
            	    if(($data['id'])==$botowner)
            	    	$this->owner($data['id']);
            	    else
            	    	$this->sendMessage("You gotta be kidding me! (XD)",$data['id']);
                    break;
                    case '!mainownerme':
            	    	$this->sendMessage("What on Earth are you smoking? (O_O)",$data['id']);
                    break;
                    case '!kickme':
                        $this->kick("You asked for it...",$data['id']);
                    break;
                    case '!banme':
                        $this->ban("You asked for it...",$data['id'],$command[1]);
                    break;
                    case '!protect':
                        if(($data['id'])==$botowner)
                            $this->sendMessage("/p");
                    break;
                    case '!define':
            	    if(($data['id'])==$botowner)
            	    	$this->sendMessage("http://www.urbandictionary.com/define.php?term=".$command[1],$data['id']);
                    break;
                    case '!info':
                        $this->sendMessage("Dubz is my master.",$data['id']);
                    break;
            	case '!killbot':
            	    if(($data['id'])==$botowner)
            	    	$this->die();
            	    else
            	    	$this->sendMessage("Yeah? YOU AND WHAT ARMY! (SMIRK)",$data['id']);
                    break;
                }
            }
        break;
    
   }        
}

/* ****************************************************** */
/* *YOU SHOULD NOT NEED TO EDIT ANYTHING AFTER TH
IS LINE* */
/* ****************************************************** */[/php]

I couldn’t post all of it because of the character limit but the rest is the same as the one on pastebin except for the guest command that was added since it wasn’t on the pastebin script

I thought of changing break; to continue; on the entire case ‘message’ part but it still didnt work. i dont know where its getting cut off at thats all theproblem is. plus it should show if you do the !ams command since its right after it but im not an expert on this like you :wink:

it was missing this in the top part too (connection part)

[php]set_time_limit(0);//No time out
$bot = new BasicXatBot();
$botowner = array(‘320467393’);
$roomID = 150355757;//Don’t know how to get this ID? goto xat.com/ROOM_YOU_WANT_BOT_IN and View Source(Firefox: Ctrl+U, Internet Explorer: View>Source) Look for "flashvars=“id=#########&…” the ID is the number you want
$bot->connect(“174.36.242.26”,“10024”); //Connect to XAT…this IP will change as necessary automatically depending on the room you want to join.
$bot->join($roomID);
while(true){
if($bot->read()==‘DIED’) {
$bot->connect(“174.36.242.26”,“10024”);
$bot->join($roomID);
}
}[/php]

ok I got an idea it is worth a shot, so lets set an empty $autmem variable as global and see if it changes after the case has happened:
[php]
$autmem=’’;
function handleEvent($event,$data) {
global $automem;
[/php]

that still didnt work. im not sure if its supposed to go in a certain place or if it just didnt work right or what. also, i couldnt get the $botowner working

this should make the variable global throughout the rest of the cases (crossed fingers :D)

where should i put this in the script? could you add it in part of the area that it would go or something? like i said, im a noob ???

I just noticed that you have botowner as array with only one id, then you are saying if $data[‘id’]==$botowner and command 1 =on, you are not setting qutomember because of this, at first give botowner a single id
$botowner = ‘320467393’;
then give it a shot
this should work next when you do an if statement like if $data[‘id’]==$botowner and you set $botowner as an array, it will always comeback false because botowner==ARRAY!
you would have to do something like the if statement I showed you earlier:
[php]

if (in_array($data[‘id’], $botowner) && $command[1]==“on”) {
[/php]

i tried adding a second id and using the if array function and it crapped out. i tried only one id and went back to the $data[‘id’]==$botowner and it wouldnt work

The $botowner value isnt going to the other parts of the chat im thinking since its doing the else statement (since if didnt work)

Sponsor our Newsletter | Privacy Policy | Terms of Service