Author Topic: [TUTORIAL] A sample command processor  (Read 712 times)

Vector

  • Administrator
  • Hero Member
  • *****
  • Posts: 550
    • View Profile
    • http://clanmage.phost.ws
[TUTORIAL] A sample command processor
« on: October 20, 2008, 11:40:14 PM »
If you're designing a really long plugin, that's intended to have a lot of commands, you may have a rudimentary command processor like the following:

Code: [Select]
Sub example_Event_UserTalk(Username, Flags, Message, Ping)
  GetDBEntry Username, myAccess, myFlags

  If Left(Message, Len(BotVars.Trogger + 11)) = BotVars.Trigger & "eatmyshorts" Then
    If myAccess > 30 Then
      AddQ "blahhh!!!"
    End If
  End If

  If Left(Message, Len(BotVars.Trogger + 7)) = BotVars.Trigger & "dothis " Then
    If myAccess > 50 Then
      doThis = Right(Message, Len(Message) - (Len(BotVars.Trigger) + 8))
      Command Username, BotVars.Trigger & doThis, False
    End If
  End If

End Sub


Followed by other commands.

Well, it's much more simpler, and does not require a lot of text, if you simple convert it to a much simpler method. Using this method, you use a minimal amount of code, and yet are still able to use your commands with no real overhead.

The preceeding example can be re-coded in a much simpler form such as the following:

Code: [Select]
Sub example_Event_UserTalk(Username, Flags, Message, Ping)
  '// Make sure they did not use a trigger
  If Not Left(Message, Len(BotVars.Trigger)) = BotVars.Trigger Then Exit Sub

  GetDBEntry Username, myAccess, myFlags

  '// Split the message up, first removing spaces at either end
  cmd = Split(Mid(Lcase(Trim(Message)), Len(BotVars.Trigger) + 1), " ")

  '// Allows us to handle multiple commands!
  Select Case
    Case "eatmyshortts"
      '// If your access is less than 30, then you can't do this command
      If myAccess < 30 Then Exit Sub
      AddQ "blahhh!"
    Case "dothis"
      '// If your access is less than 50, then you can't do this command
      If myAccess < 50 Then Exit Sub
      doThis = Split(cmd(0) & " ")(1)
      Command Username, BotVars.Trigger & doThis, False
  End Select
End Sub

Now, say you want to include whispering, and the pressed enter event to the already existing code. You would set it up in the following way:

Code: [Select]
Sub example_Event_UserTalk(Username, Flags, Message, Ping)
  Call test_processInput(Username, Message, 1)
End Sub

Sub example_Event_WhisperFromUser(Username, Flags, Message)
  Call test_processInput(Username, Message, 2)
End Sub

Sub example_Event_pressedEnter(text)
  Call test_processInput(BotVars.Username, text, 4)
End Sub

Sub example_processInput(Username, Message, Origin)
  '// Make sure they did not use a trigger
  If Not Left(Message, Len(BotVars.Trigger)) = BotVars.Trigger And Not Left(Message, 1) = "/" Then Exit Sub

  GetDBEntry Username, myAccess, myFlags

  '// Split the message up, first removing spaces at either end
  cmd = Split(Mid(Lcase(Trim(Message)), Len(BotVars.Trigger) + 1), " ")

  '// Allows us to handle multiple commands!
  Select Case
    Case "eatmyshortts"
      '// If your access is less than 30, then you can't do this command
      If myAccess < 30 Then Exit Sub
      DSP Origin, "blahhh!", Username, vbWhite
    Case "dothis"
      '// If your access is less than 50, then you can't do this command
      If myAccess < 50 Then Exit Sub
      doThis = Split(cmd(0) & " ")(1)
      Command Username, BotVars.Trigger & doThis, False
  End Select
End Sub

So as you can see, this makes commands a lot easier to implement. This is efficient if you have a lot of commands, like more than 4 or 5.
« Last Edit: January 17, 2009, 01:17:59 AM by Vector »
I am an Unofficial StealthBot Technician, an Unofficial StealthBot scripter, among other things. See my about page for more info.

Quote from: ArticWolve
Sorry I went down. Had internet problems with a cracker-jack-licensed driver hitting my internet pole (about 8 of them fell) and drooping my phone and electricity. Sorry ><

Rev77.net Plugins Manager

Proud host of Vector.no-ip.info | VectorJBLS.no-ip.org Status: [img]http://rev77.net/ServerCheck/check.asp?query=check&server=VectorJBLS.no-ip.org_status.png\" border=\"0\" class=\"linked-image\" /]

Shito-Ryu Karate-Do Genbu-Kai Rank: 8th Kyu (White belt, one stripe)

Rev77.Net Remote Support (Double-Click my name only if you were told to)

If the above link doesn't work, or if you use Vista, try this alternate link.

Noob ~Vector