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