HOW-TO: Write your own bot = Lesson 5
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

HOW-TO: Write your own bot = Lesson 5

Started by pHaTTy, 10 November, 2003, 12:37:53

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

pHaTTy

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
Resistance is futile!

kepp

:D  thanks alot, i learned a few things here :)

keep it up ;)
Guarding    

pHaTTy

thx and yep will do :o)
Resistance is futile!

Tuben

-Dark-Mind-[ DOWNLOAD ]

pHaTTy

Resistance is futile!

kepp

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
Guarding    

pHaTTy

i suppose it depends on timer luck, but i wud never use a timer like that, not my script style
Resistance is futile!

raz

safe phatty, yo back on the scripts i see. keep up the good work. :D

pHaTTy

QuoteOriginally posted by raz
safe phatty, yo back on the scripts i see. keep up the good work. :D

haha thx dude :o)
Resistance is futile!

SaintSinner

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
   


SMF spam blocked by CleanTalk