Author Topic: How would I add more commands/responses to my chatbot?  (Read 2648 times)

Taking the current code:
//This is made by the glorious elephant hugger, Johnny Blockhead 26
package chatbot {
function
clientCmdChatMessage(%a,%b,%c,%fmsg,%cp,%name,%cs,%msg)
{
parent::clientCmdChatMessage(%a,%b,%c,%fmsg,%cp,%name,%cs,%msg);
if (%msg $= "I have a chatbot. Her name is Carl.")
if (%name $= "Johnny Blockhead 26") {
      commandToServer('messagesent',"CarlBot: My name is Carl!");
   }
}

};
activatepackage(chatbot);


Let's see what we can do, shall we?

We have two options here, using switches, or a ton of if and then conditional statements.  The easier method is a switch, so let's learn that

A switch allows you to check a static variable easily without using if/then statements

it goes like this:

function test(%var)
{
    switch$(%var)
    {
       case "hello bot":
       commandToServer('messagesent',"Hi there!");

       case "goodbye bot":
       commandToServer('messagesent',"Goodbye!");
    }
}


Obviously, that's not going to do jack stuff unless you type test("hello bot"); in the console
So let's insert this into your code:

//This is made by the glorious elephant hugger, Johnny Blockhead 26
package chatbot
{
   function clientCmdChatMessage(%a,%b,%c,%fmsg,%cp,%name,%cs,%msg)
   {

      parent::clientCmdChatMessage(%a,%b,%c,%fmsg,%cp,%name,%cs,%msg);
      if (%name $= "Johnny Blockhead 26") //First let's check your name
      {
         switch$(%msg) //let's add the switch now
         {
            case "hello":  //The first case
            commandToServer('messagesent',"CarlBot: Hello!");  //This is our command for the first switch

            case "goodbye":  //The second case
            commandToServer('messagesent',"CarlBot: Goodbye!");  //This is our command for the second switch
            
            //Add as many cases as you want but remember each case can only have one command
         }  //Cases need their own definition parenthesis so close the case
      }
   }

};
activatepackage(chatbot);


Yes I edited your format because later on in large scripts it's going to hurt my eyes

-snip-
I'll try it out.
But for future reference, the 'suggested' format would be a indent for every new line of code?


if(!isFile("config/Chatbot/prefs.cs")){ // Checking if the file exists
   $Chatbot::Pref::BotName="Chatbot"; //Creating a variable for the chatbot's name
   $Chatbot::Pref::Caller="chatbot,"; //creating a variable for the chatbot's caller
   $Chatbot::Pref::StartScan=1; //creating a variable to scan when the bot loads
}
export("$Chatbot::Pref*","config/Chatbot/prefs.cs"); //exporting all chatbot prefs to a file
exec("config/Chatbot/prefs.cs"); //executing the prefs file
package ChatBotCore //creating a new package
{
   function NMH_Type::Send(%this) //function when you send a chat message
   {
      if(firstWord(%this.getValue()) $= $Chatbot::Pref::Caller@",") //checking if the first word is the caller of you chatbot
      {
         %msg=%this.getValue(); //creating a message variable from the value of the chat bar
         ChatbotCore_Send(%msg); //sending the message through chat (see below)
         ChatbotCore_Command(getWord(%msg,1),$Pref::Player::netName,restWords(restWords(%msg)),1); //Calling a function, keep in mind that 1 at the end
         canvas.popDialog(NewMessageHud); //closing the chat window
         return; //ending here
      }
      Parent::Send(%this); //doing what this function was originally meant to do.. (IT HAS TO BE IN A PACKAGE!!!, Otherwise you will get some annoying bugs)
   }
    function clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg) //function for when you get a chat message
    {
          parent::clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg); //calling original chat function
          if(firstWord(%msg)$=$Chatbot::Pref::Caller@",") //checking if first word of the message is the caller
            ChatBotCore_Command(getWord(%msg,1),%name,restWords(restWords(%msg))); //calling a command with specific information
    }
};
activatepackage(ChatBotCore); //activating the package so the parented functions will run their new modified code
function ChatBotCore_Command(%command,%name,%arguments,%safe) //function for calling chatbot code
{
   if(%name $= $Pref::Player::netName && !%safe) //making sure that there is no one imposing as you
      return; //ending because someone is messing with your chat
   if(isFunction("ChatBot_"@%command)) //checking if a function exists
      call("ChatBot_"@%command,%name,%arguments,%safe); //calling that function with specific information
}
function ChatBotCore_Send(%m) //creating a function to make chatting easier
{
   commandtoserver('messagesent',%m); //sending a chat message
}
function ChatBotCore_Print(%s) //creating a function to make adding lines to the screen easier
{
   NewChatSO.addLine("\c5"@%s); //adding a chat line
}
function ChatbotCore_scan() //a function for scanning all files in blockland folder
{
   %p="./ChatBot_*.cs"; //a new file pattern with a wild card (*) in it
   %f=findFirstFile(%p); //finding the first file with the pattern
   while(%f!$="") //until there is no file to be found, loop
   {
      if(compile(%f)) //if the file will compile
      {
         echo("ChatBot: Executing "@fileBase(%f)); //echo that we're executing the file
         exec(%f); //execute it
         %l++; //increment a variable
      }
      else //otherwise..
      {
         echo("ChatBot: Error in "@%f); //tell us that we have an error
         %e++; //increment a variable
      }
      %t++; //increment a variable
      %f=findNextFile(%p); //find the next file using the pattern
   }
   echo("ChatBot: Loaded Plugins="@%l); //echo a variable
   echo("ChatBot: Bugged Plugins="@%e); //echo another variable
   echo("Chatbot: Total Plugins="@%t); //and another variable
}
if($Chatbot::Pref::StartScan) //checking if we have that scan pref on
{
   ChatbotCore_scan(); //commence scanning
   export("$Chatbot::Pref*","config/Chatbot/prefs.cs"); //export prefs
}

My private chatbot code :/

It allows for adding commands as simple as this:

function Chatbot_Hi(%name,%args,%safe) //creating a new function
{
   ChatBotCore_Send($Chatbot::Pref::BotName@": Hello, "@%name); //sending a chat message
}

That one function allows you to type "Chatbot, Hi" in the chat and have it respond as "Chatbot: Hello, MARBLE MAN"


Edit:
Forgot to mention that it scans your entire blockland folder for files named "Chatbot_~"
It will execute them and run them

Edit:
Forgot to mention some extra functionality:
It stops you from responding to servercmdmessagesent(%YourClient,"chatbot, Hi");

Edit:
Commented everything so eepos is happy
« Last Edit: June 22, 2013, 01:27:22 AM by MARBLE MAN »

I'll try it out.
But for future reference, the 'suggested' format would be a indent for every new line of code?

Most people in this community (and in general) use either K&R or Allman style, you can read more about indent styling here



I don't think that you should have posted that code in this thread, you have to understand that this code is fairly complex for someone with his current knowledge of scripting, unless you plan on going in-depth about how each line works, he's not going to learn from it thus, it's not going to help him solve the problem he's asking for help on.
« Last Edit: June 21, 2013, 11:33:02 PM by Kadon »

I don't think that you should have posted that code in this thread, you have to understand that this code is fairly complex for someone with his current knowledge of scripting, unless you plan on going in-depth about how each line works, he's not going to learn from it thus, it's not going to help him solve the problem he's asking for help on.
fixed

spoon feeding really pulls your strings :/

if it's only strings you want you could just use something like a dictionary, parsing spaces as "SPC" or underscores, maybe even using a parser for special commands like so:

Code: [Select]
$cBotMsg["Hello_chatbot!"] = "Hi.";
$cBotMsg["a"] = "b";
$cBotMsg["calc*"] = "SPCMD:runCalc 1 2"; //cba to figure out the code for this, * indicates wildcard rest of string and SPCMD: indicates run a command instead of chatting, 1
                                                             //and 2 are words to use in args
//etc etc
$cBotMCount = 3;

function thisGrabsInputFromChat(%text, %isDirectInput) { //isDirectInput refers to if the target has said the chatbot's name or if it's just a random input from conversation
 %text = %text.replace(" ", "_");
 if($cBotMsg[%text] $= "") {
  if(%isDirectInput) commandToServer('messageSent', "Sorry, what?");
 } else {
   commandToServer('messageSent', $cBotMsg[%text]);
 }
}