Using timers
---------------
First of all here is the code we had from lesson 3
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
SetTimer(60000)
but also have something to show on the timer
we are going to use:
function OnTimer()
SendToAll(Bot,"-------------------------------")
SendToAll(Bot,"--Write your own bot lesson 4--")
SendToAll(Bot,"-------------------------------")
end
and now you need to also start the timer:
StartTimer()
so now we have so far :
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:
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:
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
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
so what is the lesson. as u r explaining this. ?(
QuoteOriginally posted by raz
so what is the lesson. as u r explaining this. ?(
i dont understand what you mean?
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
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
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
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 ;)
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
Nice going here... When do we get some advanced lessons.... :-D
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...
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
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.
i am really happy that these are helping people, i might cancel some of my projects to do some more lessons :o)
like these lessons ,
helping to understand the basics
of scripting like to learn more about this :P
good job !!
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)
60000 = 1 min ;)
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)
if user.iProfile == (profilenumberhere) then
vip < is newuser
op > is op of course ;)
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????
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 //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 .. :)
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 ?????????
Coolness you rule (uk-kingdom)pH?tt? :D
MP
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??
You mean to show up the command itself?
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
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
Try this
Haven't tested
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
Quote from: 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??
have you checked the Options/Text Files/Enable Text files support in the GUI?
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
Quote from: Herodes on 13 November, 2006, 17:43:07
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 ;)
Ontimer can be described as tutorial as the following:
First we need to set the clock
Setting the clock
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
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
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
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)
function Main()
SetTimer(Clock)
StartTimer()
end
Now, you need to define the events at each tick
Event
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
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
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
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]
Quote from: 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
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 ;)
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 readingWe 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 replacementsThen gather the read data for one local variable 'info'
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
Exampleinfo="\r\n\t\t\t Item1 \r\n\t\t\t Item2"
Notice that "\r\n\t" are
INSIDE the " " -s
Sending and closingSendToAll(Bot,info)
Send the gathered data by 'Bot' to everyone
file:close()
Close the file
Existence of the fileThe '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