PtokaX forum

Development Section => HOW-TO's => Topic started by: pHaTTy on 25 October, 2003, 14:55:06

Title: HOW-TO: Write your own bot = Lesson 4
Post by: pHaTTy on 25 October, 2003, 14:55:06
Using timers
---------------

First of all here is the code we had from lesson 3

Code: [Select]

Bot = "Keiko"

version = "0.4"
NEWCON = 1
prefix = "!"

function Main()
frmHub:RegBot(Bot)
end

function NewUserConnected(user)
if NEWCON == 1 then
user:SendData (Bot,"A User has connected")
end
end

function OpConnected(user)
if user.iProfile == 0 then
user:SendData(Bot,"Hello Master "..user.sName.." How are you")
return 1
else
SendToAll(Bot,"A Op has entered")
end
end

function DataArrival(user,data)
if strsub(data, 1, 1) == "<" then
data=strsub(data,1,strlen(data)-1)
s,e,cmd = strfind(data,"%b<>%s+(%S+)")
if cmd == prefix.."version" then
user:SendData(Bot,"This bot is Learn to write a bot version: "..version)
return 1
elseif cmd == prefix.."help" then
user:SendData(Bot,"This is where the help text wud go, bt im not gonna waste my time :P")
return 1
end
end
end

Now we are going to add a timed advert, its very simple

You first of all have to set the timer
Code: [Select]

SetTimer(60000)

but also have something to show on the timer

we are going to use:

Code: [Select]
function OnTimer()
SendToAll(Bot,"-------------------------------")
SendToAll(Bot,"--Write your own bot lesson 4--")
SendToAll(Bot,"-------------------------------")
end

and now you need to also start the timer:

Code: [Select]
StartTimer()

so now we have so far :

Code: [Select]
Bot = "Keiko"

version = "0.4"
NEWCON = 1
prefix = "!"

function Main()
frmHub:RegBot(Bot)
SetTimer(60000)
StartTimer() --In the main function because it starts the timer on hub start or restart
end

function OnTimer() --This is what will be sent on the timer
SendToAll(Bot,"-------------------------------")
SendToAll(Bot,"--Write your own bot lesson 4--")
SendToAll(Bot,"-------------------------------")
end

function NewUserConnected(user)
if NEWCON == 1 then
user:SendData (Bot,"A User has connected")
end
end

function OpConnected(user)
if user.iProfile == 0 then
user:SendData(Bot,"Hello Master "..user.sName.." How are you")
return 1
else
SendToAll(Bot,"A Op has entered")
end
end

function DataArrival(user,data)
if strsub(data, 1, 1) == "<" then
data=strsub(data,1,strlen(data)-1)
s,e,cmd = strfind(data,"%b<>%s+(%S+)")
if cmd == prefix.."version" then
user:SendData(Bot,"This bot is Learn to write a bot version: "..version)
return 1
elseif cmd == prefix.."help" then
user:SendData(Bot,"This is where the help text wud go, bt im not gonna waste my time :P")
return 1
end
end
end

now also dont forget to use tabs, scripters always use tabs when scripting, it helps then find errors, in there script for example:

Code: [Select]
Bot = "Keiko"

version = "0.4"
NEWCON = 1
prefix = "!"

function Main()
frmHub:RegBot(Bot)
SetTimer(60000)
StartTimer() --In the main function because it starts the timer on hub start or restart
end

function OnTimer() --This is what will be sent on the timer
SendToAll(Bot,"-------------------------------")
SendToAll(Bot,"--Write your own bot lesson 4--")
SendToAll(Bot,"-------------------------------")
end

function NewUserConnected(user)
if NEWCON == 1 then
user:SendData (Bot,"A User has connected")
end
end

function OpConnected(user)
if user.iProfile == 0 then
user:SendData(Bot,"Hello Master "..user.sName.." How are you")
return 1
else
SendToAll(Bot,"A Op has entered")
end
end

function DataArrival(user,data)
if strsub(data, 1, 1) == "<" then
data=strsub(data,1,strlen(data)-1)
s,e,cmd = strfind(data,"%b<>%s+(%S+)")
if cmd == prefix.."version" then
user:SendData(Bot,"This bot is Learn to write a bot version: "..version)
return 1
elseif cmd == prefix.."help" then
user:SendData(Bot,"This is where the help text wud go, bt im not gonna waste my time :P")
return 1
end
end
end
end

in this example you will not really notice the problem
but if you use tabs:

Code: [Select]
Bot = "Keiko"

version = "0.4"
NEWCON = 1
prefix = "!"

function Main()
frmHub:RegBot(Bot)
SetTimer(60000)
StartTimer() --In the main function because it starts the timer on hub start or restart
end

function OnTimer() --This is what will be sent on the timer
SendToAll(Bot,"-------------------------------")
SendToAll(Bot,"--Write your own bot lesson 4--")
SendToAll(Bot,"-------------------------------")
end

function NewUserConnected(user)
if NEWCON == 1 then
user:SendData (Bot,"A User has connected")
end
end

function OpConnected(user)
if user.iProfile == 0 then
user:SendData(Bot,"Hello Master "..user.sName.." How are you")
return 1
else
SendToAll(Bot,"A Op has entered")
end
end

function DataArrival(user,data)
if strsub(data, 1, 1) == "<" then
data=strsub(data,1,strlen(data)-1)
s,e,cmd = strfind(data,"%b<>%s+(%S+)")
if cmd == prefix.."version" then
user:SendData(Bot,"This bot is Learn to write a bot version: "..version)
return 1
elseif cmd == prefix.."help" then
user:SendData(Bot,"This is where the help text wud go, bt im not gonna waste my time :P")
return 1
end
end
end
end -- now we can see there is 1 to many ends


so for each if there is an end

so this is the working version, this lesson has tought you to use tabs when scripting, why, and a little on timers, remember this is still the basic lessons, advanced will come when you have the basic bot

Code: [Select]
Bot = "Keiko"

version = "0.4"
NEWCON = 1
prefix = "!"

function Main()
frmHub:RegBot(Bot)
SetTimer(60000)
StartTimer() --In the main function because it starts the timer on hub start or restart
end

function OnTimer() --This is what will be sent on the timer
SendToAll(Bot,"-------------------------------")
SendToAll(Bot,"--Write your own bot lesson 4--")
SendToAll(Bot,"-------------------------------")
end

function NewUserConnected(user)
if NEWCON == 1 then
user:SendData (Bot,"A User has connected")
end
end

function OpConnected(user)
if user.iProfile == 0 then
user:SendData(Bot,"Hello Master "..user.sName.." How are you")
return 1
else
SendToAll(Bot,"A Op has entered")
end
end

function DataArrival(user,data)
if strsub(data, 1, 1) == "<" then
data=strsub(data,1,strlen(data)-1)
s,e,cmd = strfind(data,"%b<>%s+(%S+)")
if cmd == prefix.."version" then
user:SendData(Bot,"This bot is Learn to write a bot version: "..version)
return 1
elseif cmd == prefix.."help" then
user:SendData(Bot,"This is where the help text wud go, bt im not gonna waste my time :P")
return 1
end
end
end

l8rr,,

-phatty
Title:
Post by: raz on 25 October, 2003, 15:03:39
so what is the lesson. as u r explaining this. ?(
Title:
Post by: pHaTTy on 25 October, 2003, 15:05:59
Quote
Originally posted by raz
so what is the lesson. as u r explaining this. ?(

i dont understand what you mean?
Title:
Post by: raz on 25 October, 2003, 15:10:03
now that we have learnt about tabs and how to put a timer in to a bot. what is the next lesson? this lesson seems very easy to do. maybe u could put a harder 1 up. :D
Title:
Post by: pHaTTy on 25 October, 2003, 15:16:03
No im writing the lessons, so ill write them easy to hard, im not jumping ahead, to quick, because newbies might not be able to keep up, or need to ask questions, then as i decide what next, then i will put up the next lesson ;)
And you are not supposed to just copy, its not as simple as it seems..........

you are supposed to use this data to help you think of ideas to use in a timer this is to help you understadn the basic timer
Title:
Post by: raz on 25 October, 2003, 15:28:19
sorry, but i thought there was only 2 of us last time trying lesson 2. its ok if u want 2 wait 4 other ppl. i have new ideas like you do the timer thing u can have it so information about the hub comes up every 30 mins and and few commands dey can do like !news etc... :D i don't mind waiting, just i wnat 2 make my bot sooooon. :P
Title:
Post by: pHaTTy on 25 October, 2003, 15:33:20
Well i will try and split my time up and mabbe try and get another few lessons done today, but like i sqaid earlier i got linux to sort out ;)
Title:
Post by: raz on 25 October, 2003, 15:39:44
actuaaly take your time as we went some good lessons. don't do it in a hurry. if u wnat u can get ur linux done first and den come on to the lessons. :D
Title:
Post by: NightLitch on 26 October, 2003, 19:26:51
Nice going here... When do we get some advanced lessons.... :-D
Title:
Post by: RiPOFF on 02 November, 2003, 07:27:51
what about functions that generally are used?

like if an account is idle for a while its automaticlly backed up into a txt and then deleted

or
like other hub function...
Title:
Post by: pHaTTy on 02 November, 2003, 11:13:13
Not quite sure what you mean, i will start on lesson 5 soon, altho i am taking it easy, i am going thru all the beginners lessons, then go thru them agen, but in advanced, i also have gekko to work on too, so not to much time on my hands, if you have a requests for next lesson post em here, n can you explain what you mean, see i am not writing you a bot, i am just teaching how to write things,

so if you mean to backup something, i will randomly choose something to backup, then you can learn from it ;)

-phatty
Title:
Post by: lazyj189 on 10 November, 2003, 02:42:10
I dont know about anyone else, but these lessons are sure helping me.  For the most part the basics, I've got them figured out.  Hell, I can even pull certian code out of other scripts that others have done to make smallers ones.  I am glad that phatty has taken this nice and slow because to someone who has never really understood the language, it takes a little time to absorb all this info.  I know I am ready whenever you are for lesson 5.  Otherwise some good practice is taking scripts that others have made and go through them, trying to understand al the functions, what does what, and what the results are of each.  When I get a script I try to start at the top and follow it through to see how it jumps all over the place.  Just a little insight as to how i've learned a little more about scripting.
Title:
Post by: pHaTTy on 10 November, 2003, 12:13:04
i am really happy that these are helping people, i might cancel some of my projects to do some more lessons :o)
Title: nice one !
Post by: Masterload on 16 November, 2003, 17:23:17
like these lessons ,
helping to understand the basics
of scripting like to learn more about this :P
good job !!
Title: timer length
Post by: Nemesis on 17 May, 2004, 14:53:04
Nice lessons you have here, just right for complete newbies who ar'nt afraid to mess aorund and learn, just one question....    SetTimer(60000) is 60000 set in seconds?? so i could set say 300 for 5 mins ??  

PS not yet set this last lesson up as off to work in a jiffy and dont have time to play :( so not sure how long value of 60000 will last in actual time value
 hope you understand what im asking !

i know these lessons have been up a while but im a newbie, please be patient for me  :)

 8)
Title:
Post by: pHaTTy on 17 May, 2004, 15:59:24
60000 = 1 min ;)
Title:
Post by: Nemesis on 18 May, 2004, 13:54:05
Noticed you use
function NewUserConnected(user) - and
function OpConnected(user) for sending short message
but what if you wanted to send diff message to reg or vip too?   8o  :D  8)
Title:
Post by: pHaTTy on 18 May, 2004, 14:06:09
if user.iProfile == (profilenumberhere) then

vip < is newuser

op > is op of course ;)
Title:
Post by: LiqUiD~TrolL on 12 November, 2004, 19:22:10
well hi ppl i was looking at these lessons but i think i saw them very late,the lessons are really nice ones
but i don t need something so complicated

what if i just wana fix a simple bot that is shows a txt file?? only this io want
can you help me????
Title:
Post by: Herodes on 12 November, 2004, 20:03:00
for that kind of bot there is a series of posts in the :Scripting:Utilities:Central: thread located in this forum ..

also , this particular type of bots has its king ... Texter by plop..
look in http://www.plop.nl to find the complete series of this bot ...
it will give u example of what you need to do .. watch the way the script develops throught the versions .. and always expiriment .. :)
Title:
Post by: LiqUiD~TrolL on 14 November, 2004, 14:24:03
hello herodes ,well my firend i tried plops bot
the bot it is shown on the users list but i can t see the txt
i tried +text !text and +,! fdfolder but nothin happened
what the hell is going on with this bot ?????????
Title: Re: HOW-TO: Write your own bot = Lesson 4
Post by: MetalPrincess on 02 July, 2006, 18:24:55
Coolness you rule (uk-kingdom)pH?tt? :D


MP
Title: Re: HOW-TO: Write your own bot = Lesson 4
Post by: speedX on 13 November, 2006, 16:24:48
If I want the bot to show the +help command on main chat every 1 min thn wat shud I add in the script and where??
Title: Re: HOW-TO: Write your own bot = Lesson 4
Post by: Naithif on 13 November, 2006, 16:35:32
You mean to show up the command itself?

Code: [Select]
Clock = 60000 --(millisec)
Bot = frmHub:GetHubBotName() --or write a nick for it in quotation marks

function Main()
SetTimer(Clock)
StartTimer()
end

OnTimer = function()
SendToAll(Bot, "Type +help for help!")
end

On Main function (upon starting the script) the timer starts ticking and on every tick (60000ms = 60 s = 1min) it sends a message to all
Title: Re: HOW-TO: Write your own bot = Lesson 4
Post by: speedX on 13 November, 2006, 17:31:08
if I want to show up a text file??
I mean if I have a txt file with command +bla
And I want to display the data in tht txt file in main, thn wat to do??

Is it possible?? Please reply even if it not possible
Title: Re: HOW-TO: Write your own bot = Lesson 4
Post by: Naithif on 13 November, 2006, 17:37:09
Try this
Haven't tested

Code: [Select]
Clock = 60000 --(millisec)
Bot = frmHub:GetHubBotName() --or write a nick for it in quotation marks
Fileloc = "scripts/anything.txt" --the name of the file to post (inside the PtokaX directory)

function Main()
SetTimer(Clock)
StartTimer()
end

OnTimer = function()

file = io.open(frmHub:GetPtokaXLocation()..Fileloc, "r")
if file then
local info = string.gsub(file:read("*a"),string.char(10), "\r\n")
SendToAll(Bot,info)
file:close()
else
SendToOps(Bot,"Error, specified file ("..Fileloc..") can't be found!")
end

end

A little modification for the file to display if it isn't exist
Title: Re: HOW-TO: Write your own bot = Lesson 4
Post by: Herodes on 13 November, 2006, 17:43:07
if I want to show up a text file??
I mean if I have a txt file with command +bla
And I want to display the data in tht txt file in main, thn wat to do??

have you checked the Options/Text Files/Enable Text files support in the GUI?
Title: Re: HOW-TO: Write your own bot = Lesson 4
Post by: speedX on 13 November, 2006, 17:47:17
Yup working, thx for the help Naithif (again)

But if you have time thn could you plzz explain tht OnTimer function??
Posted on: 13 November 2006, 16:46:02
have you checked the Options/Text Files/Enable Text files support in the GUI?

hey I'm not tht a big newbie :P
I just want the text file data to come in Main at constant intervals ;)
Title: Re: HOW-TO: Write your own bot = Lesson 4
Post by: Naithif on 13 November, 2006, 18:07:47
Ontimer can be described as tutorial as the following:

First we need to set the clock
Setting the clock
Code: [Select]
SetTimer(X)Where X can be a variable defined previously or an integer.
If you want it to be definable use a variable here like
Code: [Select]
SetTimer(Clock)
'Clock' is the variable it can be given at the start of the script.

We need an event to start the clock (until then it will not give any timed events)
Starting
Code: [Select]
StartTimer()
And we need a "timer tick" option that happens when the timer has all criterias, started, and the given time passed since start (or the previous tick)
(It's a function notice that)
Timer tick
Code: [Select]
OnTimer = function()
If you want the timer to set & start immediately when you start the script you can add the start command to 'Main' function (that runs at script start)

Code: [Select]
function Main()
SetTimer(Clock)
StartTimer()
end

Now, you need to define the events at each tick
Event
Code: [Select]
OnTimer = function()

file = io.open(frmHub:GetPtokaXLocation()..Fileloc, "r")
if file then
local info = string.gsub(file:read("*a"),string.char(10), "\r\n")
SendToAll(Bot,info)
file:close()
else
SendToOps(Bot,"Error, specified file ("..Fileloc..") can't be found!")
end

end

This is a separate function.
Anything between the first line 'OnTimer = function()' and the 'end' word gets executed when the timer's started at each tick repeatedly until stopped by 'StopTimer()'

This can be used for repeated things, timed events, or giving a delay to an event
For example, when you start the clock and stop it in the repeated action then the performed event only gets a delay
Delaying
Code: [Select]
Clock = 60000 --(millisec)
Bot = frmHub:GetHubBotName()

function Main()
SetTimer(Clock)
StartTimer()
end

OnTimer = function()

SendToAll(Bot, "This is a delayed message. 60 seconds passed since hub start")
StopTimer()

end

We've stopped it in 'OnTimer' function, so it'll only execute once, delayed with the 'Clock' variable
You can do this to delay something when someone types a command or from the script's start, or any other event happens

Shortly LOL
Cheers
Title: Re: HOW-TO: Write your own bot = Lesson 4
Post by: speedX on 13 November, 2006, 18:15:40
Hey thx Naithif now tht time thing would get out of my mind ;) , this tutorials has been a gr8 help for me

Wat does this little code do? could u explain plzz
Code: [Select]
file = io.open(frmHub:GetPtokaXLocation()..Fileloc, "r")
if file then
local info = string.gsub(file:read("*a"),string.char(10), "\r\n")
SendToAll(Bot,info)
file:close()
[code]
[/code]
Title: Re: HOW-TO: Write your own bot = Lesson 4
Post by: Naithif on 13 November, 2006, 18:39:48
Hey thx Naithif now tht time thing would get out of my mind ;) , this tutorials has been a gr8 help for me

Wat does this little code do? could u explain plzz
Code: [Select]
file = io.open(frmHub:GetPtokaXLocation()..Fileloc, "r")
if file then
local info = string.gsub(file:read("*a"),string.char(10), "\r\n")
SendToAll(Bot,info)
file:close()
[code]
[/code]

It's --part of-- opening a text file ;)

Code: [Select]
file = io.open(frmHub:GetPtokaXLocation()..Fileloc, "r")
if file then
local info = string.gsub(file:read("*a"),string.char(10), "\r\n")
SendToAll(Bot,info)
file:close()
else
SendToOps(Bot,"Error, specified file ("..Fileloc..") can't be found!")
end

File reading
We give a variable 'file' equal to opening a file at PtokaX's location followed by scripts folder and filename for reading

file = io.open(frmHub:GetPtokaXLocation()..Fileloc, "r")
        /\                     /\                           /\     /\
        /                      /                            /        \  for reading
------                 ----                        ----           ------------               
open the file  -> Your hub's location   +  Folder+file

So it would come out as
Example
'H:\Hub\PtokaX\' (Hub's location) + 'scripts\anything.txt' (defined 'Fileloc' variable - folder + file)
Becomes
'H:\Hub\PtokaX\scripts\anything.txt'

Gathering the data and replacements
Then gather the read data for one local variable 'info'
Code: [Select]
local info = string.gsub(file:read("*a"),string.char(10), "\r\n")
Gather data to a local variable which becomes everything in the file except 'enter' character which will be output as "\r\n"
"\r\n" is the linebreaking inside scripts "\t" inserts a tab into the message
Example
Code: [Select]
info="\r\n\t\t\t Item1 \r\n\t\t\t Item2"Notice that "\r\n\t" are INSIDE the " " -s

Sending and closing
Code: [Select]
SendToAll(Bot,info)Send the gathered data by 'Bot' to everyone

Code: [Select]
file:close()Close the file

Existence of the file
The 'if file'/'else' is given because the file can be an error source when you rename it or something, and you can make the script to report it

'If file then' means if the file exists (file doesn't equal nil - a non existing file's output is nil)
'else' other case - file doesn't exists

A solution was to report it to give the Fileloc variable because it describes the folder and the file, making it easy to filter this error