PtokaX forum

Development Section => HOW-TO's => Topic started by: pHaTTy on 12 October, 2003, 15:51:45

Title: HOW-TO : Write your own Bot = Lesson 2
Post by: pHaTTy on 12 October, 2003, 15:51:45
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
Title:
Post by: RiPOFF on 21 October, 2003, 23:58:06
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
Title:
Post by: pHaTTy on 22 October, 2003, 06:35:02
You mean stop !ban etc etc?
Title:
Post by: SaintSinner on 12 November, 2003, 19:20:28
is this

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)

 

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
Title:
Post by: pHaTTy on 12 November, 2003, 19:50:30
heheh well you will see on the next lesson ;)
Title:
Post by: Guibs on 12 November, 2003, 20:21:59
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,, ;)
Title:
Post by: pHaTTy on 12 November, 2003, 20:42:01
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 ;)
Title:
Post by: SaintSinner on 12 November, 2003, 21:08:02
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

 :]
Title:
Post by: pHaTTy on 14 November, 2003, 21:07:41
ghehehe ok :o)
Title: I dont understand
Post by: Intel on 05 January, 2004, 08:33:06
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
Title:
Post by: Cain on 23 August, 2004, 18:51:09
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|
Title:
Post by: Psycho_Chihuahua on 23 August, 2004, 19:07:27
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 :)
Title:
Post by: nErBoS on 23 August, 2004, 19:22:31
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
Title:
Post by: Psycho_Chihuahua on 23 August, 2004, 19:32:24
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 :)
Title:
Post by: nErBoS on 23 August, 2004, 19:38:49
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
Title:
Post by: Psycho_Chihuahua on 23 August, 2004, 20:33:03
.....

Is it really THAT easy?? ?(  hehe

thnx alot nerbos
Title:
Post by: NightLitch on 23 August, 2004, 20:38:49
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
Title:
Post by: Psycho_Chihuahua on 23 August, 2004, 21:07:59
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

Title:
Post by: NightLitch on 23 August, 2004, 21:20:15
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
Title:
Post by: Psycho_Chihuahua on 23 August, 2004, 22:06:46
ok i wasn't quit sure about that, thnx for the info

you've been a great help you guys
Title:
Post by: Dj_OcTaGoN on 04 September, 2004, 14:12:36
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
Title:
Post by: NightLitch on 04 September, 2004, 15:00:33
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
Title:
Post by: Herodes on 04 September, 2004, 15:49:12
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) ...
Title:
Post by: Dj_OcTaGoN on 04 September, 2004, 22:19:31
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
Title:
Post by: BottledHate on 04 September, 2004, 22:40:58
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 (http://www.plop.nl/howto/howto.php)

-BH
Title: Re: HOW-TO : Write your own Bot = Lesson 2
Post by: JueLz on 13 November, 2006, 14:48:45
The data arrival isnt working

Posted on: 13 November 2006, 13:45:07
Is it because i am using the last ptokax?
Title: Re: HOW-TO : Write your own Bot = Lesson 2
Post by: TTB on 13 November, 2006, 15:09:49
Data Arrival is LUA4... The current PtokaX uses LUA 5.1...
Title: Re: HOW-TO : Write your own Bot = Lesson 2
Post by: Naithif on 13 November, 2006, 15:14:36
Yip these posts are rather old :)
Try this

function ChatArrival(user, data)
local s,e,cmd = string.find(data, "%b<>%s+(%S+)(%S+)")
if cmd == "!command" then

--*define here what you want to happen when someone types the command, or filter it*

end
end


Of course !command can be changed to anything  ;)
This example should be only for a command (!command), no extensions after it, if you want to know it, just ask
Title: Re: HOW-TO : Write your own Bot = Lesson 2
Post by: achiever on 14 November, 2006, 08:25:39
hi,

how do show a text file's data on some command input?

and plzz all lord scripters hold it a bit n give the newbe's time to lean n understand :P

thks,

achiever
Title: Re: HOW-TO : Write your own Bot = Lesson 2
Post by: Naithif on 14 November, 2006, 09:26:12
Hi

Use PtokaX's inbuilt text file support, or if you need to do other things with that, you can find it in lesson 4 (as I remember)

Cheers
Title: Re: HOW-TO : Write your own Bot = Lesson 2
Post by: Herodes on 14 November, 2006, 13:09:54
Quote from: achiever on 14 November, 2006, 08:25:39
and plzz all lord scripters hold it a bit n give the newbe's time to lean n understand :P
Generally you strike the steel while it hot. In any case you can always read through the forums, threads posts as many times you want, to get the meaning and follow through... ;)