PtokaX forum

Development Section => HOW-TO's => Topic started by: pHaTTy on 10 November, 2003, 12:37:53

Title: HOW-TO: Write your own bot = Lesson 5
Post by: pHaTTy on 10 November, 2003, 12:37:53
ok from lesson 4 we had

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


the first part of this lesson we will be concentrating on getting more familar with the more advanced stuff, not to hard, but not the easy stuff

first of it we will start with randomising

adding a new part to the lesson

: Remember = whenever you see this word then remember the following phrase or word, as i may use it in another lesson, for a bit homework heheeh

now we are going to make 5 different random entrances for ops.....

function OpConnected(user)
thisisthevariable = random(5) --first the variable, then random(5) because random will randomise it of course then it needs how many random data there will be, there will be 5 entrances
if thisisthevariable = 1 then
SendToAll(Bot,"this is message 1")
end
if thisisthevariable = 2 then
SendToAll(Bot,"this is message 2")
end
if thisisthevariable = 3 then
SendToAll(Bot,"this is message 3")
end
if thisisthevariable = 4 then
SendToAll(Bot,"this is message 4")
end
if thisisthevariable = 5 then
SendToAll(Bot,"this is message 5")
end
end
[code]

ok i have on purposly made a few mistakes, so that when i fix them you will see, and learn more

ok first of all the first one i done was

[code]
if thisisthevariable = 1 then

this shud have 2 =

if thisisthevariable == 1 then

function OpConnected(user)
thisisthevariable = random(5)
if thisisthevariable == 1 then
SendToAll(Bot,"this is message 1")
end
if thisisthevariable == 2 then
SendToAll(Bot,"this is message 2")
end
if thisisthevariable == 3 then
SendToAll(Bot,"this is message 3")
end
if thisisthevariable == 4 then
SendToAll(Bot,"this is message 4")
end
if thisisthevariable == 5 then
SendToAll(Bot,"this is message 5")
end
end

now thats it, its fine it will work, but now, the worse thing i have done is if, if, if, to many ifs for same arguemnt, meaning uneeded ends and uneeded checks

elseif is needed

function OpConnected(user)
thisisthevariable = random(5)
if thisisthevariable == 1 then
SendToAll(Bot,"this is message 1")

elseif thisisthevariable == 2 then
SendToAll(Bot,"this is message 2")

elseif thisisthevariable == 3 then
SendToAll(Bot,"this is message 3")

elseif thisisthevariable == 4 then
SendToAll(Bot,"this is message 4")

elseif thisisthevariable == 5 then
SendToAll(Bot,"this is message 5")
end
end

this is better and more effiencent ;)

now we are also going to randomise the timer message

the old timer message we had was

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

this changed to

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


now we had a randomisation

function OnTimer() --This is what will be sent on the timer
randomtimer = random(2) --only 2 data's
if randomtimer == 1 then
SendToAll(Bot,"-------------------------------")
SendToAll(Bot,"--Write your own bot lesson 5--")
SendToAll(Bot,"-------------------------------")
elseif randomtimer == 2 then
SendToAll(Bot,"-----------------------------------------")
SendToAll(Bot,"--This lesson bot was created by Phatty--")
SendToAll(Bot,"-----------------------------------------")
end
end

now because there nr2 is that last you an just use else

function OnTimer() --This is what will be sent on the timer
randomtimer = random(2) --only 2 data's
if randomtimer == 1 then
SendToAll(Bot,"-------------------------------")
SendToAll(Bot,"--Write your own bot lesson 5--")
SendToAll(Bot,"-------------------------------")
else
SendToAll(Bot,"-----------------------------------------")
SendToAll(Bot,"--This lesson bot was created by Phatty--")
SendToAll(Bot,"-----------------------------------------")
end
end

but we will stick to elseif for noe as i may use it later on in l8r lessons

now, just a little more on the timer

SetTimer(60000)

1 minute, but now we want 2? whats the easiest way ;)

SetTimer(2*60000)


so we will make it better for editors

SetTimer(TimerMins*60000)

so now we have

Bot = "Keiko"

version = "0.5"
NEWCON = 1
prefix = "!"
TimeMins = 5  --set it to 5 mins


function Main()
frmHub:RegBot(Bot)
SetTimer(TimeMins*60000)
StartTimer()
end

function OnTimer() --This is what will be sent on the timer
randomtimer = random(2) --only 2 data's
if randomtimer == 1 then
SendToAll(Bot,"-------------------------------")
SendToAll(Bot,"--Write your own bot lesson 5--")
SendToAll(Bot,"-------------------------------")
elseif randomtimer == 2 then
SendToAll(Bot,"-----------------------------------------")
SendToAll(Bot,"--This lesson bot was created by Phatty--")
SendToAll(Bot,"-----------------------------------------")
end
end

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

function OpConnected(user)
thisisthevariable = random(5)
if thisisthevariable == 1 then
SendToAll(Bot,"This is message 1")

elseif thisisthevariable == 2 then
SendToAll(Bot,"This is message 2")

elseif thisisthevariable == 3 then
SendToAll(Bot,"This is message 3")

elseif thisisthevariable == 4 then
SendToAll(Bot,"This is message 4")

elseif thisisthevariable == 5 then
SendToAll(Bot,"This is message 5")
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.." by Phatty")
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 actually getting some where :o)

see you in the nest lesson ;)

-phatz
Title:
Post by: kepp on 10 November, 2003, 16:47:07
:D  thanks alot, i learned a few things here :)

keep it up ;)
Title:
Post by: pHaTTy on 10 November, 2003, 16:47:50
thx and yep will do :o)
Title:
Post by: Tuben on 10 November, 2003, 17:09:48
Nice.. ;))
Title:
Post by: pHaTTy on 10 November, 2003, 17:12:11
thx :o)
Title:
Post by: kepp on 10 November, 2003, 18:05:37
Sec = 1000
Min = 60*Sec
Hour = 60*Min
Day = 24*Hour

hubadvTimer = 2*Min
hubadv1 = "Another test with random timer 1"
hubadv2 = "Another test with random timer 2"

function Main()
SetTimer(hubadvTimer)
StartTimer()
end

function OnTimer()
lovethistimer = random(2)
if lovethistimer == 1 then
SendToAll(BotName, hubadv1)
elseif lovethistimer == 2 then
SendToAll(BotName, hubadv2)
end
end


How come the output of that is:

[18:43] <[P0lic?]> Another test with random timer 1
[18:45] <[P0lic?]> Another test with random timer 2
[18:47] <[P0lic?]> Another test with random timer 2
[18:49] <[P0lic?]> Another test with random timer 2
[18:51] <[P0lic?]> Another test with random timer 2
[18:53] <[P0lic?]> Another test with random timer 2
[18:55] <[P0lic?]> Another test with random timer 1
[18:57] <[P0lic?]> Another test with random timer 2
[18:59] <[P0lic?]> Another test with random timer 2
[19:01] <[P0lic?]> Another test with random timer 2
[19:03] <[P0lic?]> Another test with random timer 2
Title:
Post by: pHaTTy on 10 November, 2003, 18:18:27
i suppose it depends on timer luck, but i wud never use a timer like that, not my script style
Title:
Post by: raz on 10 November, 2003, 19:53:10
safe phatty, yo back on the scripts i see. keep up the good work. :D
Title:
Post by: pHaTTy on 10 November, 2003, 20:03:34
QuoteOriginally posted by raz
safe phatty, yo back on the scripts i see. keep up the good work. :D

haha thx dude :o)
Title:
Post by: SaintSinner on 14 November, 2003, 16:58:22
phatty,
just wanted to say thanks
im getting alot from these lessons
i gotta say i cannot write a bot (yet)
but i can at least look at one and partly see
what is going on.

i cannot memorize the functions so i am still
reading from the ptokax scripting page, and other
bots to see how they use the code,
but i can understand it now
thanx,
*awaits for the next lesson*
 :D  :D  :D  :D  :D  :D  :D  :D  :D