HOW-TO : Write your own Bot = Lesson 2
 

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 2

Started by pHaTTy, 12 October, 2003, 15:51:45

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

pHaTTy

From lesson 1 we have

Bot = "J???"


function NewUserConnected(user)
SendToAll(Bot,"Welcome our new guest "..user.sName..", enyot your stay")
end

function OpConnected(user)
SendToAll(Bot,"WElcome our Op "..user.sName" hope he enjoys his stay")
end

--now to add some more functions

to add the bot on the userlist is frmHub:RegBot(Bot)

so
function Main()
frmHub:RegBot(Bot)
end

then for a version command, we will need data arrival, the data arrival is the incoming data hence arrrival

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+)") -- this converts it into cmd (the incoming data)

   if cmd == "version" then
         user:SendData(Bot,"This bot is Learn to write a bot version: "..version)
         return 1   
      end
   end
end


--now we have the data arrival we need the variable ie ..version

so we add :

version = "o.2 by Phatty"

now we have so far


Bot = "J???"
version = "o.2 by Phatty"


function Main()
frmHub:RegBot(Bot)
end

function NewUserConnected(user)
SendToAll(Bot,"Welcome our new guest "..user.sName..", enyot your stay")
end

function OpConnected(user)
SendToAll(Bot,"WElcome our Op "..user.sName" hope he enjoys his stay")
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 == "!version" then
			user:SendData(Bot,"This bot is Learn to write a bot version: "..version)
			return 1	
		end
	end
end


more to come as i get more time :o)

-phatty
Resistance is futile!

RiPOFF

wow i undersantd this is geting cool umm could you for your next lesson teach disable raw functions such as kick or mainchat or etc

pHaTTy

You mean stop !ban etc etc?
Resistance is futile!

SaintSinner

#3
is this

function DataArrival(user,data
if 
strsub(data11) == "<" then 
data
=strsub(data,1,strlen(data)-1
s,e,cmd strfind(data,"%b<>%s+(%S+)") -- this converts it into cmd (the incoming data



always come before a command
or is one time at the begining of the script?

so
function DataArrival(user,data)
blah blah blah
and then the command ...!ban

function DataArrival(user,data)
blah blah blah
and then the command ...!kick

function DataArrival(user,data)
blah blah blah
and then the command ...!version

is this correct
   


pHaTTy

heheh well you will see on the next lesson ;)
Resistance is futile!

Guibs

Hi there,

You cannot use several DataArrival functions into 1 single script,... you can,, lol,... but it won't work properly,... ;)

Once the code defined about the way to find a command (there, 'cmd',,), use ' elseif ',... by example:
---------
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 == "!version" then
			user:SendData(Bot,"This bot is Learn to write a bot version: "..version)
			return 1	
		end

	elseif cmd == "!yourcmd1" then
			-- Do your code there
			return 1 -- use 'return 1' if you don't want the command showed on the main	
		end

	elseif cmd == "!yourcmd2" then
			-- Do your code there
			-- none 'return 1',... cmd is visible to all
		end
	end
end
---------
Good luck, & good scripts,,, :)

l8tr,, ;)
-- Please,... don\'t ask help in Pm,...Forums are made for that, to help everyone & my Inbox pm will be safe,... Thks,,  :))  --
CB forum     /     CB Home page

pHaTTy

Guibs dont worry its all in the next few lessons, i dont see why peeps ask questions b4 they ahve looked all the way thru them ;)
Resistance is futile!

SaintSinner

QuoteOriginally posted by (uk-kingdom)pH?tt?
Guibs dont worry its all in the next few lessons, i dont see why peeps ask questions b4 they ahve looked all the way thru them ;)


just curiosity i guess

 :]
   


pHaTTy

Resistance is futile!

Intel

I dont understand what this is
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+)") -- this converts it into cmd (the incoming data)


should this just be copy paste or could u give an small explanation on it ?? Actually i can just copy paste but i like to understand it...if possible :P
Punnakku

Cain

In lesson 1, the script worked without registering the bot, does this work for every kind of script or is registering the bot required for the script to run? [excluding scripts like opchat where it's necessarry to reg the bot]

Also what is dataarrival shown later in the script?

Thanx,

|Cain|

Psycho_Chihuahua

Ok more than 1 Data Arrival doesnt work hmmm

but the how would i combine
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+)")
		end
		if (cmd=="!help") then 
			if user.iProfile == nil then 
				SendUserhelp(user) 
				return 1 
			elseif user.iProfile == 0 then 
				SendMasterhelp(user) 
				return 1 
			elseif user.iProfile == 1 then 
				SendOpshelp(user) 
				return 1 
			elseif user.iProfile == 2 then 
				SendViphelp(user) 
				return 1 
			elseif user.iProfile == 3 then 
				SendReghelp(user) 
				return 1 
			elseif user.iProfile == 4 then 
				SendModeratorhelp(user) 
				return 1 
			end 
		elseif (cmd=="!lol") then 
			SendLol(user) 
			return 1 
		elseif (cmd=="!rules") then 
			SendRules(user) 
			return 1 
		elseif (cmd=="!network") then 
			SendNetwork(user) 
			return 1 
		elseif (cmd=="!faq") then 
			SendFaq(user) 
			return 1 
		elseif (cmd=="!description") then 
			SendDescription(user) 
			return 1 
		elseif (cmd=="!showreg") then
			showreg(user)
			return 1
		elseif (cmd == "!myip" ) then
			user:SendData(BOTName,"Deine Ip lautet: "..user.sIP)
			return 1
		elseif (cmd == "!myversion" ) then
			user:SendData(BOTName,"Deine Client Version ist: "..user.iVersion)
			return 1
		elseif (cmd == "!addreguser") then 
			if user.iProfile == 1 or user.iProfile == 0 or user.iProfile == 4 then 
				return 0 
			elseif user.iProfile == nil then 
				user:SendPM(BOTName, "du bist nicht befugt diesen Befehl auszuf?hren") 
			end 
		elseif (cmd == "!me") then 
			AddAChatter(user) 
			return 0 
		end
			local s, e, cmd, args = strfind(data, "^%b<> %!(%a+)%s*(.*)%|$")
			if s and ptokaxcommands[cmd] then
			if user.iProfile == 1 or user.iProfile == 0 or user.iProfile == 4 then 
			return 0
			elseif user.iProfile == nil then
			SendPM(BOTName, "du bist nicht befugt diesen Befehl auszuf?hren") 
		end
			local s, e, cmd, args = strfind(data, "^%b<> %!(%a+)%s*(.*)%|$")
			if s and ptokax2commands[cmd] then
			if user.iProfile == 1 or user.iProfile == 4 then 
			return 0
			elseif user.iProfile == nil then
			SendPM(BOTName, "du bist nicht befugt diesen Befehl auszuf?hren") 
		end
		elseif (cmd == "!restartscripts") and user.iProfile == 0 then 
			return 0 
		elseif (cmd == "!restart") and user.iProfile == 0 then 
			return 0 
		end
	end 
end
with this
function DataArrival( sUser , sData )
	if strsub( sData , 1 , 7 ) == "$MyINFO" then
		local _,_,openhubs = strfind( sData , ".+H:(%d+)" );
		if ( openhubs ~= "0" ) then
			SendToNick( sUser.sName , DisconnectMessage )
			sUser:Disconnect()
		end
	end
end
and why? U see i'm still a noobie but my interest is getting stronger by the day :)
PtokaxWiki ?PtokaX Mirror + latest Libs

01100001011011000111001101101111001000000110101101101110011011110111011101101110001000000110000101110011001000000101010001101111011010110110111101101100011011110111001101101000

nErBoS

#12
Hi,

The first step is to see that the contion that you want to implement has different user and data variable name

function DataArrival([b]user[/b] ,[b]data[/b])

function DataArrival([b]sUser[/b] ,[b]sData[/b])

Try to change to the same.

Best regards, nErBoS
--## nErBoS Spot ##--

Psycho_Chihuahua

ok, well the easiest to change was this one
function DataArrival( user , data )
	if strsub( data , 1 , 7 ) == "$MyINFO" then
		local _,_,openhubs = strfind( data , ".+H:(%d+)" );
		if ( openhubs ~= "0" ) then
			SendToNick( user.name , DisconnectMessage )
			user:Disconnect()
		end
	end
end
and now for the next step :)
PtokaxWiki ?PtokaX Mirror + latest Libs

01100001011011000111001101101111001000000110101101101110011011110111011101101110001000000110000101110011001000000101010001101111011010110110111101101100011011110111001101101000

nErBoS

Hi,

Now add this...

if strsub( data , 1 , 7 ) == "$MyINFO" then
		local _,_,openhubs = strfind( data , ".+H:(%d+)" );
		if ( openhubs ~= "0" ) then
			SendToNick( user.name , DisconnectMessage )
			user:Disconnect()
		end
	end

...to the other DataArrival.

Best regards, nErBoS
--## nErBoS Spot ##--

Psycho_Chihuahua

.....

Is it really THAT easy?? ?(  hehe

thnx alot nerbos
PtokaxWiki ?PtokaX Mirror + latest Libs

01100001011011000111001101101111001000000110101101101110011011110111011101101110001000000110000101110011001000000101010001101111011010110110111101101100011011110111001101101000

NightLitch

well to do it a little bit complicated you need when u add more if's in the DataArrival to do it like this with elseif's

function DataArrival( user , data )
    if strsub(data, 1,1) == "<" then
      -- do mainchat code here
    elseif strsub(data, 1,7) == "$MyINFO" then
      -- do myinfostring code here
    elseif strsub(data, 1,4) == "$To:" then
      -- do private message code here
    end
end

hope u get the point...

/NL
//NL

Psycho_Chihuahua

well i think i got the point...

If this is right that is...
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+)")
		end
		if (cmd=="!help") then 
			if user.iProfile == nil then 
				SendUserhelp(user) 
				return 1 
			elseif user.iProfile == 0 then 
				SendMasterhelp(user) 
				return 1 
			elseif user.iProfile == 1 then 
				SendOpshelp(user) 
				return 1 
			elseif user.iProfile == 2 then 
				SendViphelp(user) 
				return 1 
			elseif user.iProfile == 3 then 
				SendReghelp(user) 
				return 1 
			elseif user.iProfile == 4 then 
				SendModeratorhelp(user) 
				return 1 
			end 
		elseif (cmd=="!lol") then 
			SendLol(user) 
			return 1 
		elseif (cmd=="!rules") then 
			SendRules(user) 
			return 1 
		elseif (cmd=="!network") then 
			SendNetwork(user) 
			return 1 
		elseif (cmd=="!faq") then 
			SendFaq(user) 
			return 1 
		elseif (cmd=="!description") then 
			SendDescription(user) 
			return 1 
		elseif (cmd=="!showreg") then
			showreg(user)
			return 1
		elseif (cmd == "!myip" ) then
			user:SendData(BOTName,"Deine Ip lautet: "..user.sIP)
			return 1
		elseif (cmd == "!myversion" ) then
			user:SendData(BOTName,"Deine Client Version ist: "..user.iVersion)
			return 1
		elseif (cmd == "!addreguser") then 
			if user.iProfile == 1 or user.iProfile == 0 or user.iProfile == 4 then 
				return 0 
			elseif user.iProfile == nil then 
				user:SendPM(BOTName, "du bist nicht befugt diesen Befehl auszuf?hren") 
			end 
		elseif (cmd == "!me") then 
			AddAChatter(user) 
			return 0 
		end
			local s, e, cmd, args = strfind(data, "^%b<> %!(%a+)%s*(.*)%|$")
			if s and ptokaxcommands[cmd] then
			if user.iProfile == 1 or user.iProfile == 0 or user.iProfile == 4 then 
			return 0
			elseif user.iProfile == nil then
			SendPM(BOTName, "du bist nicht befugt diesen Befehl auszuf?hren") 
		end
			local s, e, cmd, args = strfind(data, "^%b<> %!(%a+)%s*(.*)%|$")
			if s and ptokax2commands[cmd] then
			if user.iProfile == 1 or user.iProfile == 4 then 
			return 0
			elseif user.iProfile == nil then
			SendPM(BOTName, "du bist nicht befugt diesen Befehl auszuf?hren") 
		end
		elseif (cmd == "!restartscripts") and user.iProfile == 0 then 
			return 0 
		elseif (cmd == "!restart") and user.iProfile == 0 then 
			return 0 
		end
    		elseif strsub(data, 1,7) == "$MyINFO" then
			if strsub( data , 1 , 7 ) == "$MyINFO" then
				local _,_,openhubs = strfind( data , ".+H:(%d+)" );
				if ( openhubs ~= "0" ) then
				SendToNick( user.name , DisconnectMessage )
			user:Disconnect()
		end
	end
end
    		elseif strsub(data, 1,4) == "$To:" then
		DisconnectMessage = "Nachricht kommt hier rein"
 		   end
end
PtokaxWiki ?PtokaX Mirror + latest Libs

01100001011011000111001101101111001000000110101101101110011011110111011101101110001000000110000101110011001000000101010001101111011010110110111101101100011011110111001101101000

NightLitch

Not right you close the if so the elseif can not be checked...

I sorted it out for ya, look at your code close so you see what I have done.

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=="!help") then 
			if user.iProfile == nil then 
				SendUserhelp(user) 
				return 1 
			elseif user.iProfile == 0 then 
				SendMasterhelp(user) 
				return 1 
			elseif user.iProfile == 1 then 
				SendOpshelp(user) 
				return 1 
			elseif user.iProfile == 2 then 
				SendViphelp(user) 
				return 1 
			elseif user.iProfile == 3 then 
				SendReghelp(user) 
				return 1 
			elseif user.iProfile == 4 then 
				SendModeratorhelp(user) 
				return 1 
			end 
		elseif (cmd=="!lol") then 
			SendLol(user) 
			return 1 
		elseif (cmd=="!rules") then 
			SendRules(user) 
			return 1 
		elseif (cmd=="!network") then 
			SendNetwork(user) 
			return 1 
		elseif (cmd=="!faq") then 
			SendFaq(user) 
			return 1 
		elseif (cmd=="!description") then 
			SendDescription(user) 
			return 1 
		elseif (cmd=="!showreg") then
			showreg(user)
			return 1
		elseif (cmd == "!myip" ) then
			user:SendData(BOTName,"Deine Ip lautet: "..user.sIP)
			return 1
		elseif (cmd == "!myversion" ) then
			user:SendData(BOTName,"Deine Client Version ist: "..user.iVersion)
			return 1
		elseif (cmd == "!addreguser") then 
			if user.iProfile == 1 or user.iProfile == 0 or user.iProfile == 4 then 
				return 0 
			elseif user.iProfile == nil then 
				user:SendPM(BOTName, "du bist nicht befugt diesen Befehl auszuf?hren") 
			end 
		elseif (cmd == "!me") then 
			AddAChatter(user) 
			return 0 
		end
		local s, e, cmd, args = strfind(data, "^%b<> %!(%a+)%s*(.*)%|$")
		if s and ptokaxcommands[cmd] then
			if user.iProfile == 1 or user.iProfile == 0 or user.iProfile == 4 then 
				return 0
			elseif user.iProfile == nil then
				SendPM(BOTName, "du bist nicht befugt diesen Befehl auszuf?hren") 
			end
			local s, e, cmd, args = strfind(data, "^%b<> %!(%a+)%s*(.*)%|$")
			if s and ptokax2commands[cmd] then
				if user.iProfile == 1 or user.iProfile == 4 then 
					return 0
				elseif user.iProfile == nil then
					SendPM(BOTName, "du bist nicht befugt diesen Befehl auszuf?hren") 
				end
			elseif (cmd == "!restartscripts") and user.iProfile == 0 then 
				return 0 
			elseif (cmd == "!restart") and user.iProfile == 0 then 
				return 0 
			end
		end

	elseif strsub(data, 1,7) == "$MyINFO" then
		local _,_,openhubs = strfind( data , ".+H:(%d+)" );
		if ( openhubs ~= "0" ) then
			SendToNick( user.name , DisconnectMessage )
			user:Disconnect()
		end

	elseif strsub(data, 1,4) == "$To:" then
		DisconnectMessage = "Nachricht kommt hier rein"
	end
end

I hope I did make it all correct with the commands but the strsub's is right...

/NL
//NL

Psycho_Chihuahua

ok i wasn't quit sure about that, thnx for the info

you've been a great help you guys
PtokaxWiki ?PtokaX Mirror + latest Libs

01100001011011000111001101101111001000000110101101101110011011110111011101101110001000000110000101110011001000000101010001101111011010110110111101101100011011110111001101101000

Dj_OcTaGoN

Im confused about the:
 if strsub (data,1,1) etc.... what does this line do? Or what does it mean? string...something?
And the (data,) is only there to know that data will arrive? (data is for commands?) I got some more questions though lol  :D


NightLitch

some style n00b answers:


if strsub(data,1,1) == "<" then


data is the data that comes in DataArrival

with (data,1,1) == "<" you search for a particular string

and it is only ONE "<" char. this will catch MainChat commands

and put "$To:" that is 4 char. That will look with strsub

strsub(data, 1,4) == "$To:" this will catch all PM messages

hope this helps some don't really have the time answering now, Plop or anyone that is willing to explain better than me plz do...  :D

Cheers / NightLitch
//NL

Herodes

strsub(string, start, end)

this lua function s taking resulting in a copy of the 'string'
under the restrictions of 'start' and 'end' both of which are numbers ...
These two ( 'start' , 'end' ) signify the position of the characters in the string. For Example :
string = "what are you talking about" 

strsub(string, 1, 1) --- will produce "w"
strsub(string, 2, 6) --- will produce "hat a"
strsub(string, 6, strlen(string)) --- will produce "re you talking about"


As you noticed strlen(string) gives us the 'length' of a string.
in other words it is the characters count in the string ...

Basically  in the first strsub example we told lua to say to us what is the string between the first character and the first character (I know it's tricky to understand how)

In the second, we asked lua to give us the string of characters between the second(2) character and the sixth(6) character.

In the third we told lua to give us the string of characters between the sixth(6) character and the product of strlen which effectively is a number that signifies what is the number/position of the last character in the string in question(26) ...

Dj_OcTaGoN

lol well, i think i need some time to think all this through  :D SOmedays i dont understand anything, and somedays i understand everything....L0L
thx for info and cheers! // Dj_OcTaGoN


BottledHate

y0 dJ... check out plops howto's... i learned alot about
pattern matching(strfind) and strsub from there...

http://www.plop.nl/howto/howto.php

-BH
Homepage: www.bottledhate.webhop.org

Compiling  Lua scripts is LAME!!!!!

SMF spam blocked by CleanTalk