StealthBot.net: Scripting Basics - StealthBot.net

Jump to content

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

Scripting Basics For beginners

#1 User is offline   The-Black-Ninja Icon

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

Posted October 16, 2009 - 05:03 AM

Scripting Basics
This is for beginner users who want to know the basics in learning how to script. This will teach you the general layout of a script and introduce you to the Event subs.

Starting out
This will show you how to properly format your script. Since this is the first thing you need to do, we're going to look at this first.

Event_ subs
This will explain what the events are and what happens when these are called. These events are essential for making your script work.

Creating a Timer
Since timers are part of the Event subs, we're going to get some instructions on how to create these.

Putting it all together
What your script should look like by the time we're all done here.

Splitting arguments from commands
Going from the command " .say Hello everyone, I am well " to displaying " Hello everyone, I am well "

#2 User is offline   The-Black-Ninja Icon

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

Posted October 16, 2009 - 12:21 PM

Starting out

Before you start coding, you have to tell the script system to recognize that whatever you're making is a script. The way you do this is by saying Script("whatever") = "whatever". This gives the script system some valuable information for determining who the author is, what version the script is, where in the scripts.ini to store the script settings you make, etc.

  • Script("Name")
    What you want to name your script, ie: crs, recruit, aa, AutoBan, etc. This value will be the scripts heading in the scripts.ini. If you name your script "crs", then the heading in the scripts.ini will read " [crs] ".
  • Script("Author")
    Pretty self-explanatory; the author of the script, mainly your name. Normally, this will be your name on the forums.
  • Script("Major"), Script("Minor"), Script("Revision")
    These are the major versions of the script. If you want your version of the script to be 1.21, 1 is the major version, 21 is the minor version. The revision isn't really required, but it's useful when you give one of your existing scripts a complete overhaul, so you would change the revision number to let people know that this script is a major update from previous ones. It's just so that you can keep the Major numbers down; it would make sense to make revision 1, version 1.0, than just version 12.0, get what I mean? :P
  • Script("Description")
    This is where you briefly explain what task(s) your script performs.


So, now that you guys know what these entries mean, here's how you put them all together at the very top of your script:

Quote

Script("Name") = "AutoBan"
Script("Author") = "The-Black-Ninja"
Script("Major") = 1
Script("Minor") = 0
Script("Revision") = 0
Script("Description") = "Automatically bans users that are on the text file when they join the channel."
Now that you've properly told the script system that what you're doing is actually a script, we can get down to the coding.


#3 User is offline   The-Black-Ninja Icon

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

Posted October 16, 2009 - 05:12 PM

Event_ subs

Every script needs a triggering event to run; something needs to happen before the script can begin to do anything. For instance, if you want to make a greet, you need to first detect someone joining. Someone joining will be the triggering event we're looking for, so we would want the Event_Userjoins sub to catch that person. Now, if you want something to happen when a user types something, specifically in the white chat (all chat), what event do you think we need? Something that catches someone talking, right? Event_Usertalk is what we'll look for.

Every event sub will have arguments that must be present, but you don't necessarily need to use all of them; you'll probably just want one argument, but you have to list all of them on the sub line or else the sub won't execute because the script is looking for an event that's doesn't exist because you left out an argument.

There are a large number of arguments, some of which you won't even use, but they're there if you need them. It will be overwhelming at first, but the subs are named in such a way that they generally describing what they're doing; Event_PressedEnter is happening when you press the Enter key from within the bot to send a message, whether it be a command or text, this event will fire.

Click for a list of Event_ subs

#4 User is offline   The-Black-Ninja Icon

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

Posted October 16, 2009 - 06:46 PM

<a name="timers"></a>Creating a Timer

A timer is an object that fires every so often. You can specify how many seconds or milliseconds you want the timer and each timer gets its own unique sub. Within that sub, you can put whatever code you like, whether it be messages or running through a loop, or even checking if people in the channel are AFK, the subs act just like any other sub but with some drawbacks.

The timer sub doesn't have any arguments, so if you were to use the Username variable, the variable would be blank within the timer because Username isn't defined in the sub line and no, you cannot put it in there in hopes that it will work. You will need to set it to a common variable throughout the script in a previous sub for this to work, but that's a bit advanced and we won't worry about that until later.

Creating the timer is relatively easy and managing your timers are just as easy. First, we need to create a timer in the Event_Load so the timer object will get created when the bot opens and remain open until you close the program:

Quote

Sub Event_Load()

'// Create the timer object using LongTimer so the timer operates in seconds and name it myTimer
Call CreateObj("LongTimer", "myTimer")
'// Enable it
myTimer.Enabled = True
'// Set the interval to 5 seconds
myTimer.Interval = 5
End Sub
All that's left for the timer is to put the stuff we want it to do inside the timer sub. Note that when we're calling the timer sub, we don't use an event, but rather the timer's name.

Quote

Sub myTimer_Timer()

'// Display our text inside the bot so only we can see it and no one else. We can use the AddChat function followed by a colour we want, then changing the colour mid way through the text.
AddChat vbGreen, "This green ", vbMagenta, "and purple text ", vbGreen, "are appearing every 5 seconds."
End Sub


#5 User is offline   The-Black-Ninja Icon

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

Posted October 16, 2009 - 06:58 PM

<a name="final"></a>Putting it all together</a>

A little note: I'm really sorry about the lack of indenting for this. The code tags don't want to work well with an HTML enabled post; instead of having code on different lines, it's all on one line, so I figure this setup was better than nothing. Hopefully you don't get confused too much. Use the colours in the subs to distinguish which End If belongs to which If statement.

--------

Let's make a simple script and let's make the bot's trigger something more than 1 character. Let's make it " ^^- " Something that will respond to a user's command " ^^-hello " through all chat, emote, and through whisper, as well as make a timer to display something in-bot every 5 seconds. Now, judging by what we need to accomplish, we need the following events:
  • Event_Usertalk(Username, Flags, Message, Ping)
  • Event_WhisperFromUser(Username, Flags, Message, Ping)
  • Event_UserEmote(Username, Flags, Message)
  • _Timer()
  • Event_Load()
In all the subs, except the timer, we'll need to determine if what the user is saying is actually the " hello " command, preceeded by the bot's trigger. Since the user's message is supposed to be:

Quote

^^-hello
We need to determine if the first part of the message is, in fact, the bot's trigger. We can do this by first using the Left() VB function to specify that we are interested in the left side of the message, then use the length of the bot's trigger to tell the Left function how many characters we want to return:

Quote

Left(Message, Len(BotVars.Trigger))
If their message was, "I want chocolate", that code will return, "I w". Notice that it's giving us the first 3 characters of the message because our bot's trigger is 3 characters.

Now that we have the code to separate the trigger from the message, we need to make sure they're typing the command now. This is a little bit trickier, but once you break it down, it makes perfect sense. We'll be using the Mid() VB function for this because we want to trim off the bot's trigger and get to the text after it.

Quote

Mid(Message, Len(BotVars.Trigger)+1)
If their message was, "I want chocolate", that code will return, "ant chocolate". Again, our bot's trigger is 3 characters long, so the piece of code has removed the first 3 characters of the message.

So, we've got the two important pieces of code to get the bot's trigger and the command. Let's start some coding.

Quote

'// If the characters returned by the Left function is equal to the bot's trigger
If Left(Message, Len(BotVars.Trigger)) = BotVars.Trigger Then
'// If the characters returned by the Mid function is equal to our command name
If Mid(Message, Len(BotVars.Trigger)+1) = "hello" Then
'// Respond to the user's command with a hello and their name. Since their name is a variable and not part of our text string, we need to separate our text and the variable with a & symbol
AddQ "Hello, " & Username
End If
End If
This same piece of code can be used for all three subs dealing with user's talking, changing how the bot responds, of course, because all three subs have the same Message variable and all subs are looking for the same thing; the bot's trigger and the command.

Gathering our knowledge from everything in this thread, let's put it all together and see what it looks like.

Quote

Script("Name") = "test"
Script("Author") = "The-Black-Ninja"
Script("Major") = 1
Script("Minor") = 0
Script("Revision") = 0
Script("Description") = "Responds to a command through emote, all chat, whisper, and displays a text in-bot every 5 seconds."


Sub Event_Load()

'// Create the timer object using LongTimer so the timer operates in seconds and name it myTimer
Call CreateObj("LongTimer", "myTimer")
'// Enable it
myTimer.Enabled = True
'// Set the interval to 5 seconds
myTimer.Interval = 5
End Sub


Sub Event_Usertalk(Username, Flags, Message, Ping)

'// If the characters returned by the Left function is equal to the bot's trigger
If Left(Message, Len(BotVars.Trigger)) = BotVars.Trigger Then
'// If the characters returned by the Mid function is equal to our command name
If Mid(Message, Len(BotVars.Trigger)+1) = "hello" Then
'// Respond to the user's command with a hello and their name. Since their name is a variable and not part of our text string, we need to separate our text and the variable with a & symbol
AddQ "Hello, " & Username
End If
End If
End Sub


Sub Event_WhisperFromUser(Username, Flags, Message, Ping)

'// If the characters returned by the Left function is equal to the bot's trigger
If Left(Message, Len(BotVars.Trigger)) = BotVars.Trigger Then
'// If the characters returned by the Mid function is equal to our command name
If Mid(Message, Len(BotVars.Trigger)+1) = "hello" Then
'// Respond to the user's command with a whisper, saying hello and their name. Since their name is a variable and not part of our text string, we need to separate our text and the variable with a & symbol
AddQ "/w " & Username & " Hello, " & Username
End If
End If
End Sub


Sub Event_UserEmote(Username, Flags, Message)

'// If the characters returned by the Left function is equal to the bot's trigger
If Left(Message, Len(BotVars.Trigger)) = BotVars.Trigger Then
'// If the characters returned by the Mid function is equal to our command name
If Mid(Message, Len(BotVars.Trigger)+1) = "hello" Then
'// Respond to the user's command in emote style with a hello and their name. Since their name is a variable and not part of our text string, we need to separate our text and the variable with a & symbol
AddQ "/me Hello, " & Username
End If
End If
End Sub


Sub myTimer_Timer()

'// Display our text inside the bot so only we can see it and no one else. We can use the AddChat function followed by a colour we want, then changing the colour mid way through the text.
AddChat vbGreen, "This green ", vbMagenta, "and purple text ", vbGreen, "are appearing every 5 seconds."
End Sub


#6 User is offline   The-Black-Ninja Icon

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

Posted October 19, 2009 - 04:15 AM

<a name="adv"></a>Splitting arguments from commands</a>

Well, you've gotten familiar with using some functions and if you've grasped how those work, you're in pretty good shape to take on this tutorial. I'm gonna throw a bit more stuff at you, so take your time and try to visualize what's going on instead of looking at words and numbers. I'm gonna be using some new terminology which is essential so that's a given, so you should really take your time on this one because this tutorial is going to teach you the essentials for pulling out specific pieces from commands. As in the post above, you should paste the final code into Notepad or Notepad++ and do some indenting to make it all clearer. I highly recommend using Notepad++ because it has built-in indenting guides for you. VERY helpful for beginners.

---------------------

Let's say we want to make a command that allows the person to " buy " something from the bot using a command. We know we need to first make sure they're using the command, but then we need to figure out what they're actually buying from the actual message.

In this process, we're going to make use of a new function called Split(). Split works by "splitting" the message at certain string character for the string. More information can be found here: http://www.csidata.c...docs/vbs197.htm

As in the previous post, we will make our trigger 3 characters long, " ^^- " and our command will be " buy ", so what the user will need to type is:

Quote

^^-buy sunglasses
Making use of our Split command, we can "split out" the " sunglasses " part from the command:

Quote

Split(Message, " ")(1)
Looking at the documentation from the link I posted, we have told the Split function to look at the Message string, split the message at the space characters, then we told the function to return the 1st item.

Whenever you split something and set it to a variable, that variable immediately turns into something called an Array. The thing about arrays that confuses most people is that arrays don't start from 1, they start from 0. Consider this string, "Hello, I am Heather." If we were to split the string into an array, we can isolate anything we want out of the string. These are called "items":

Quote

msg = "Hello, I am Heather."

msgArray = Split(msg)
Notice how I didn't specify to split at the spaces? This is because if you don't give the Split function something to look for, it will automatically assume you want to split the string at the spaces. Let's see what all the items will return when we split at the spaces:

Quote

msgArray(0) = "Hello,"
msgArray(1) = "I"
msgArray(2) = "am"
msgArray(3) = "Heather."
What happens if we decide we want to split the message at the comma and the space following the comma:

Quote

msg = "Hello, I am Heather."

msgArray = Split(msg, ", ")
Will return:

Quote

msgArray(0) = "Hello"
msgArray(1) = "I am Heather."
msgArray(2) = Error!!
Why does the 2nd array create an error? Because the Split function only found one occurrence of a comma and a space, therefore the string was split into two parts. While we're on the subject of the Split function, we can also tell it how many "substrings" we want the function to give us. Say we have the string. "bb aa aaa aaaa", and we want to separate the "bb" and return the rest of the message. We would do this:

Quote

Split("bb aa aaa aaaa", " ", 2)(1)

Notice that we're forced to tell it to split at the space this time when telling it how many substrings to return. Usually these are omitted when we aren't concerned with splitting a string with a character, but stop the splitting after the first character was found. We're basically telling to split the string at the space, return our 2 substrings, but immediately display the first item. Here's what it more/less looks like:

Quote

Split("bb aa aaa aaaa", " ", 2)(1)
Caught the first space at "bb", now we've split all the string at the spaces.
Ooops, gotta return only 2 substrings...okay, go back to the first space and make two strings: "bb" "aa aaa aaaa"
Now that picky person wants only the 1st item, which is the 2nd substring: "aa aaa aaaa"
And there ya go :)

Wait a second...what if people try to use the command without specifying anything to buy? For this, we need another function called UBound() (http://www.csidata.com/custserv/onlinehelp/VBSdocs/vbs214.htm). What this will do is give us the total amount of items in our array:

Quote

msg = Split("Hello, I am well.")
Ubound(msgArray)
Which will be, obviously, 3. Remember, this is automatically splitting at the spaces giving us a total of 3 items because arrays start at 0.


Let's put it all together and see what it will look like. For fun, let's set some access restrictions in there too:

Quote

Script("Name") = "buy"
Script("Author") = "The-Black-Ninja"
Script("Major") = 1
Script("Minor") = 0
Script("Revision") = 0
Script("Description") = "Allows the person to buy things from the bot."


'// Set the variables as private so this, and only this script will look at it.
Private RequiredAccess, RequiredCommand

Sub Event_Load()

'// Set the required access to something.
RequiredAccess = 5

'// Set our required command to something
RequiredCommand = "buy"
End Sub


Sub Event_Usertalk(Username, Flags, Message, Ping)

If Left(Message, Len(BotVars.Trigger)) = BotVars.Trigger Then
'// Since the command itself has spaces in it, let's split it into an array and see if the first item is the command name
If Split(Mid(Message, Len(BotVars.Trigger)+1))(0) = RequiredCommand Then
'// Check if they have enough access to use our command, if they don't, exit the sub before anything gets done.
If GetDBEntry(Username).Rank < RequiredAccess Then Exit Sub

'// Don't freak out; to avoid always having to type Mid(Message blah blah blah), we're just going to set it to a variable to make our life easier.
msg = Mid(Message, Len(BotVars.Trigger)+1)

'// Now, let's split our message containing the command and the object they're buying. Remember, we've already separated the trigger by doing the Mid(Message stuff.
'// BUT maybe we should take into consideration people buying stuff that contains spaces. Let's keep it as an array so we can check how many items it has later.
msgArray = Split(msg, " ", 2)

'// Let's check if they're buying something or not. Remember arrayed items start at 0, so anything greater than 0 means they've used the command AND they're buying something
If UBound(msgArray) > 0 Then
AddQ Username & ", you've bought: " & msgArray(1)
Else
'// They only used the command;; they haven't bought anything
AddQ Username & ", you must tell me what you want to buy."
End If
End If
End If
End Sub
This example was inspired by Aggravate's request :)

This post has been edited by The-Black-Ninja: October 29, 2009 - 04:06 AM
Reason for edit:: Code update.


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