StealthBot.net: 2.7 Scripting - StealthBot.net

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

2.7 Scripting

#1 User is offline   The-Black-Ninja Icon

  • The Sex
  • Icon
  • Group: Scripting Moderators
  • Posts: 2,526
  • Joined: August-01 09

Posted December 02, 2009 - 01:06 AM

Scripting using the 2.7 methods

Creating commands for the Command Manager
This will show you how to create commands that will be placed in the command manager.

Using Event_Command
The Event_Command replaces writing all that code to account for dspID or making it so that you have to manually check if they're using proper syntax or not by taking up no more than 4 lines.

#2 User is offline   The-Black-Ninja Icon

  • The Sex
  • Icon
  • Group: Scripting Moderators
  • Posts: 2,526
  • Joined: August-01 09

Posted December 02, 2009 - 02:17 AM

Create commands for the Command Manager A quick note: Due to issues with the forum and an HTML enabled post, I have to use quote boxes. Please copy the code, put it into your Notepad++, and do some indenting so it all becomes clear. This method will show you how to create commands so they will appear in the Command Manager, under the appropriate script heading. Keep in mind that if you create a command that has the same name as an existing command, your bot may throw a commands.xml error when you reload your script. This error is simply saying that you have two or more commands with the same name and both are enabled. You can get around this by simply disabling all by one of the commands, so only one command is active and the rest are dormant. I like to make a separate sub for creating commands; it's clean and it looks good too. Our example command will be called "show" The command will be used as follows: .show time = Displays the current time .show date = Displays the current date .show my name = Displays the bot's username .show my owner = Displays the bot's owner

Quote

'// Try to open the command. If the response is blank, the command doesn't exist and proceed with creation. Set cmd = OpenCommand("show") If cmd Is Nothing Then '// Create the command Set cmd = CreateCommand("show") '// Give the command some properties and restrictions With cmd '// Add any aliases to the command. This step can be omitted if no aliases are to be given. .Aliases.Add "display" '// Add a description for the command. .Description = "Displays various information about the bot." '// Define the parameters required for the command '// First parameter; time and date '// The NwParameter is as follows: NewParater("Names of the parameters", False/True, "Word/Number/String") '// False = The command does not support optional arguments '// True = The command does support optional arguments '// Word = A single word '// Number = A number of any size without spaces '// String = A group of words, separated by spaces '// Depending on how your command is formed, you may require multiple parameters. The example we have here, we cannot have two parameters because only two commands have extra parameters and the other two do not, so we will need to manually script the extra parameters. '// If you had a command that went like: .give Stealthbot coke, then you would need to create two parameters because Stealthbot is the user you're giving something to and the coke is the item. Get it? This command REQUIRES two parameters to work, you cannot make parameters for optional stuff, ie: '// Something that shows your time in channel: .mychantime (user is optional, otherwise it gives your time in channel). You cannot make a parameter, otherwise the command will require a user to be specified and won't work if you just do the command alone. Set Parameter = .NewParameter("time/date/my", False, "Word") With Parameter '// Add a description for the parameter .Description = "time = Displays the current time" & vbNewLine & vbNewLine & _ "date = Displays the current date" & vbNewLine & vbNewLine & _ "my owner = Displays my owner" & vbNewLine & vbNewLine & _ "my name = Displays my name" '// Add a special note for the parameter. This step can be omitted. .SpecialNotes = "Any special notes can go here for the parameter." End With '// Add the parameter .Parameters.Add Parameter Set Parameter = .NewParameter("owner/name", True, "Word") With Parameter .Description = "owner = Owner of the bot" & vbNewLine & vbNewLine & _ "name = My name" '// Add a special note for the parameter. This step can be omitted. .SpecialNotes = "Any special notes can go here for the parameter." End With '// Add the parameter .Parameters.Add Parameter '// Set the required access for the command. 0 means anyone can use it, -1 is not used anymore. Max is 200. .RequiredRank = 30 '// Set the required flags, if any .RequiredFlags = "Y" '// Save the command .Save End With End If
Put this code in your Event_Load or have the Event_Load call your sub to create the command when the bot is loads/reload scripts and within your bot, type:

Quote

/help show
Pretty cool, eh? Now, let's script the command. The Event_Command sub will handle everything pertaining to a command that is registered for that script, ie. command name, command access/flags, syntax, how the command was issued, etc.

Quote

Sub Event_Command(Command) '// Which command are they using? Select Case Lcase(Command.Name) Case "show" '// Call the sub for that command. To make life super easy, bring down the Command object into the sub so we can use all the fun functions :) Call showCMD(Command) End SelectEnd SubPrivate Sub showCMD(Command) '// Check for Access/Flags If NOT Command.HasAccess Then Exit Sub '// Check for proper syntax If NOT Command.IsValid Then Exit Sub '// Just some basic info to tell you what's happening with the command Select Case Command.Source Case 1: AddChat vbGreen, "The command "" " & Command.Name & " "" is comming from all chat." Case 2: AddChat vbGreen, "The command "" " & Command.Name & " "" is comming from emote." Case 3: AddChat vbGreen, "The command "" " & Command.Name & " "" is comming from a whisper." Case 4: AddChat vbGreen, "The command "" " & Command.Name & " "" is comming from the bot console." End Select num = Command.Arguments.Count If num > 0 Then For i = 1 To num AddChat 234234, "Command argument " & i & ": " & Command.Arguments(i) Next End If '// Now we actually tell the script what to do with the arguments that were issued. The show command was used, so this sub is called, now we have to manually tell the script which arguments are the proper ones to do what we want. Select Case num '// 1 argument supplied, so obviously that limits us to either " time " or " date " Case 1 Select Case Command.Arguments(1) Case "time" '// Respond the same way the command was issued Command.Respond "It is currently " & Time Case "date" Command.Respond "Today is " & FormatDateTime(Date, 1) End Select '// 2 arguments supplied, so that leaves the " my " argument as being the 1st argument, and " owner " and " name " are the 2nd arguments Case 2 Select Case Command.Arguments(1) Case "my" Select Case Command.Arguments(2) Case "owner" Command.Respond "My name is Heather." Case "name" Command.Respond "My official name is " & BotVars.Username End Select End Select End SelectEnd Sub
As you can see, this method really cleans up your code and shortens it by a huge amount. When checking for access, if a user has met or exceeded the access for the command, they will be able to use this. You will need to use a GetDBEntry(Command.Username).Rank if you wish to make it so that only users with that amount of access can use the command. You'll have to give the bot a few seconds to create the command once the script loads and if you mess up when experimenting with creating commands, just go into your command manager, go to the command you want to remove and just click the Delete Command button. Hopefully this covers most of your bases, there's still alot to the Command object that hasn't been described here, but that information will be available soon in the wiki or if you need to know now, you can always post on the forums :)

#3 User is offline   The-Black-Ninja Icon

  • The Sex
  • Icon
  • Group: Scripting Moderators
  • Posts: 2,526
  • Joined: August-01 09

Posted December 02, 2009 - 02:18 AM

Using Event_Command

The Command object is super powerful when dealing with commands, so it's important to know all of its properties. Here's a brief list of the different properties belonging to the object (credit goes to Hdx for his post I stole in a forum that regular members can't see :P ):

Command

.Username As String 'The username of the person who executed the command'
.Name As String 'The name of the command (aliases are transformed)'
.Args As String 'The RAW srgument string'
.Arguments As Collection 'The argument string split up using 52s parser'
.IsLocal As Boolean 'If the command was issued by the console'
.PublicOutput As Boolean 'If the command should be forced to respond to the channel'
.PublicOutput(bln As Boolean) 'Set it'
.IsValid As Boolean 'If the command follows the synax formed in the XML (arguments)'
.HasAccess As Boolean 'If the calling person has access to use this command (will always be true, else it wont fire event_command)'
.WasWhispered As Boolean 'If the command was whispered to the bot'
.docs As clsCommandDocObj 'The documentation for this command same thing as OpenCommand returns.'
.Argument(sName As String) As String 'Public Function, Get the value for an argument from the XML'
.Respond(strResponse As Variant) 'Public Sub, Adds a line of response to the command'
.ClearResponse 'Public Sub, Clears the response queue'
.GetResponse As Collection 'Public Function, Gets the response to this command'


#4 User is offline   Hdx Icon

  • Hdx
  • Icon
  • Group: Administrators
  • Posts: 1,359
  • Joined: July-31 09

Posted December 02, 2009 - 05:02 PM

'// Try to open the command. If the response is Nothing, the command doesn t exist and proceed with creation.'
Set cmd = OpenCommand("show")
If (cmd Is Nothing) Then '//Parenthesis are a good thing'
   Set cmd = CreateCommand("show") '// Create the command'

   With cmd
      '// Add any aliases to the command. This step can be omitted if no aliases are to be given.'
      .Aliases.Add "display"
      
      '// Add a description for the command.'
      .Description = "Displays various information about the bot."
      
      '// Define the parameters required for the command'
      '// First parameter; time and date'
      '// The NewParameter is as follows: NewParater("Names of the parameter", IsOptional (True/False), "Word/Number/String")'
      '// Word = A single word seperated by a space'
      '// Number = Same thing as a word, but is only 0-9'
      '// String = A group of words, separated by spaces, can be wrapped in quotes'

      Set Parameter = .NewParameter("time/date/my", False, "Word")
      With Parameter
         '// Add a description for the parameter'
         .Description = "time = Displays the current time, date = Displays the current date."
         
         '// Add a special note for the parameter. This step can be omitted.'
         .SpecialNotes = "Any special notes can go here for the parameter."  

         '// All Reg Exp matching is optional '
         '// They can use Regular exp​ressions to limit the accepted input.'
         .MatchMessage = "^(time|date|my)$"
         '// You can force it to be case sensitive, by default its not.'
         .MatchCaseSensitive = False 'We dont want it to be'
         '// If it does NOT match this, we can display an error!'
         '//    %Value - What they tried to use'
         '//    %Rank  - The calling users rank'
         '//    %Flags - The calling users flags'
         .MatchError = "Input must be either 'Date', 'Time', Or 'My', not '%Value'"
      End With         
      '// Add the parameter'
      .Parameters.Add Parameter   
      .Parameters.Add .NewParameter("info", True, "Word") '// Add an optional parameter onto the end'
      
      '// Set the required access for the command. 0 means anyone can use it, -1 means only the internal bot can use it. Max is 200.'
      .RequiredRank = 30
      
      '// Set the required flags, if any, if the user has the required rank, flags are not checked.'
      .RequiredFlags = "Y"
      
      '// Save the command, VARY IMPORTANT'
      .Save
   End With
End If
Set cmd = Nothing '//Make sure to free mem!'


Sub Event_Command(Command)
   '// Which command are they using?'
   '// Note, .Name will ALWAYS be the real name of the command, not an alias'
   Select Case LCase(Command.Name)
      Case "show": Call Command_Show(Command)
   End Select
End Sub

Private Sub Command_Show(Command)
   '// Check for Access/Flags'
   '// The event WILL NOT be called if this is false, so no need to check'
   'If NOT Command.HasAccess Then Exit Sub'
   
   '// Check for proper syntax'
   '// The event WILL be called if this is false, so you can display any errors if you want'
   If (NOT Command.IsValid) Then Exit Sub
   
   Select Case LCase(Command.Argument("time/date/my"))
      Case "time": Command.Respond StringFormat("It is currently {0}", Time)
      Case "date": Command.Respond StringFormat("Today is {0}", FormatDateTime(Date, 1))
      Case "my":
         Select Case LCase(Command.Argument("info"))
            Case "owner": Command.Respond "My name is Heather."
            Case "name":  Command.Respond StringFormat("My official name is {0}", BotVars.Username)
         End Select
   End Select
End Sub


As you can see, this method really cleans up your code and shortens it by a huge amount. When checking for access, if a user has met or exceeded the access for the command, they will be able to use this. You will need to use a GetDBEntry(Command.Username).Rank if you wish to make it so that only users with that amount of access can use the command.

You'll have to give the bot a few seconds to create the command once the script loads and if you mess up when experimenting with creating commands, just go into your command manager, go to the command you want to remove and just click the Delete Command button. Hopefully this covers most of your bases, there's still alot to the Command object that hasn't been described here, but that information will be available soon in the wiki or if you need to know now, you can always post on the forums :)
[/quote]
'====================================================================
Heres a few examples of how we do real world commands in the bot:

Out dispatcher function (your Event_Command())
'This is the replacement for ExecuteCommand, Uses the new clsCommandObj, Should be cleaner.'
'Returns True if it is a recognized internal command'
Public Function DispatchCommand(Command As clsCommandObj)
    DispatchCommand = True
    Select Case LCase(Command.Name)
        '... other types of commands :P '
        'Misc Commands'
        Case "addquote":       Call modCommandsMisc.OnAddQuote(Command)
        Case "bmail":          Call modCommandsMisc.OnBMail(Command)
        Case "cancel":         Call modCommandsMisc.OnCancel(Command)
        Case "checkmail":      Call modCommandsMisc.OnCheckMail(Command)
        Case "exec":           Call modCommandsMisc.OnExec(Command)
        Case "flip":           Call modCommandsMisc.OnFlip(Command)
        Case "greet":          Call modCommandsMisc.OnGreet(Command)
        Case "idle":           Call modCommandsMisc.OnIdle(Command)
        Case "idletime":       Call modCommandsMisc.OnIdleTime(Command)
        Case "idletype":       Call modCommandsMisc.OnIdleType(Command)
        Case "inbox":          Call modCommandsMisc.OnInbox(Command)
        Case "math":           Call modCommandsMisc.OnMath(Command)
        Case "mmail":          Call modCommandsMisc.OnMMail(Command)
        Case "quote":          Call modCommandsMisc.OnQuote(Command)
        Case "roll":           Call modCommandsMisc.OnRoll(Command)
        Case "readfile":       Call modCommandsMisc.OnReadFile(Command)
        Case "setidle":        Call modCommandsMisc.OnSetIdle(Command)
        Case "tally":          Call modCommandsMisc.OnTally(Command)
        Case "vote":           Call modCommandsMisc.OnVote(Command)
        
        Case Else: DispatchCommand = False
    End Select
End Function

Examples of commands with: No arguments, Required Arguments, and Optional Arguments
Public Sub OnAddQuote(Command As clsCommandObj)
    If (g_Quotes Is Nothing) Then
        Set g_Quotes = New Collection
    End If
    If (Command.IsValid) Then
        g_Quotes.Add Command.Argument("Quote")
        Command.Respond "Quote added!"
    Else
        Command.Respond "You must provide a quote to add."
    End If
End Sub
Public Sub OnFlip(Command As clsCommandObj)
    Command.Respond IIf(((Rnd * 1000) Mod 2) = 0, "Heads.", "Tails.")
End Sub
Public Sub OnRoll(Command As clsCommandObj)
    Dim maxValue As Long
    Dim Number   As Long

    If (LenB(Command.Argument("Value")) > 0) Then
        maxValue = Abs(Val(Command.Argument("Value")))
    Else
        maxValue = 100
    End If
    
    Randomize
    Number = CLng(Rnd * maxValue)
        
    Command.Respond StringFormat("Random number (0-{0}): {1}", maxValue, Number)
End Sub

Erin Go Bragh!
Posted Image

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users