Author Topic: HOW-TO : Write your own Bot = Lesson 2  (Read 11697 times)

0 Members and 1 Guest are viewing this topic.

Offline pHaTTy

  • Forum Legend
  • ***
  • Posts: 1 916
  • Karma: +5/-1
HOW-TO : Write your own Bot = Lesson 2
« 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


Code: [Select]

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!

PtokaX forum

HOW-TO : Write your own Bot = Lesson 2
« on: 12 October, 2003, 15:51:45 »

Offline RiPOFF

  • Member
  • ***
  • Posts: 46
  • Karma: +0/-0
(No subject)
« Reply #1 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

Offline pHaTTy

  • Forum Legend
  • ***
  • Posts: 1 916
  • Karma: +5/-1
(No subject)
« Reply #2 on: 22 October, 2003, 06:35:02 »
You mean stop !ban etc etc?
Resistance is futile!

Offline SaintSinner

  • Lord
  • ***
  • Posts: 293
  • Karma: +1/-0
(No subject)
« Reply #3 on: 12 November, 2003, 19:20:28 »
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
« Last Edit: 12 November, 2003, 19:23:46 by SaintSinner »
   


Offline pHaTTy

  • Forum Legend
  • ***
  • Posts: 1 916
  • Karma: +5/-1
(No subject)
« Reply #4 on: 12 November, 2003, 19:50:30 »
heheh well you will see on the next lesson ;)
Resistance is futile!

Offline Guibs

  • Double Ace
  • *
  • Posts: 142
  • Karma: +3/-0
(No subject)
« Reply #5 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:
---------
Code: [Select]
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

Offline pHaTTy

  • Forum Legend
  • ***
  • Posts: 1 916
  • Karma: +5/-1
(No subject)
« Reply #6 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 ;)
Resistance is futile!

Offline SaintSinner

  • Lord
  • ***
  • Posts: 293
  • Karma: +1/-0
(No subject)
« Reply #7 on: 12 November, 2003, 21:08:02 »
Quote
Originally 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

 :]
   


Offline pHaTTy

  • Forum Legend
  • ***
  • Posts: 1 916
  • Karma: +5/-1
(No subject)
« Reply #8 on: 14 November, 2003, 21:07:41 »
ghehehe ok :o)
Resistance is futile!

Offline Intel

  • Double Ace
  • *
  • Posts: 101
  • Karma: +1/-0
I dont understand
« Reply #9 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
Punnakku

Offline Cain

  • Newbie
  • *
  • Posts: 6
  • Karma: +0/-0
(No subject)
« Reply #10 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|

Offline Psycho_Chihuahua

  • Systemspecialist IT
  • Emperor
  • **
  • Posts: 1 054
  • Karma: +112/-1
  • I am wherever i want to be
    • PtokaX Luaboard
(No subject)
« Reply #11 on: 23 August, 2004, 19:07:27 »
Ok more than 1 Data Arrival doesnt work hmmm

but the how would i combine
Code: [Select]
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
Code: [Select]
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

Offline nErBoS

  • Emperor
  • **
  • Posts: 1 149
  • Karma: +3/-1
(No subject)
« Reply #12 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

Code: [Select]
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
« Last Edit: 23 August, 2004, 19:23:51 by nErBoS »
--## nErBoS Spot ##--

Offline Psycho_Chihuahua

  • Systemspecialist IT
  • Emperor
  • **
  • Posts: 1 054
  • Karma: +112/-1
  • I am wherever i want to be
    • PtokaX Luaboard
(No subject)
« Reply #13 on: 23 August, 2004, 19:32:24 »
ok, well the easiest to change was this one
Code: [Select]
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

Offline nErBoS

  • Emperor
  • **
  • Posts: 1 149
  • Karma: +3/-1
(No subject)
« Reply #14 on: 23 August, 2004, 19:38:49 »
Hi,

Now add this...

Code: [Select]
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 ##--

Offline Psycho_Chihuahua

  • Systemspecialist IT
  • Emperor
  • **
  • Posts: 1 054
  • Karma: +112/-1
  • I am wherever i want to be
    • PtokaX Luaboard
(No subject)
« Reply #15 on: 23 August, 2004, 20:33:03 »
.....

Is it really THAT easy?? ?(  hehe

thnx alot nerbos
PtokaxWiki ?PtokaX Mirror + latest Libs

01100001011011000111001101101111001000000110101101101110011011110111011101101110001000000110000101110011001000000101010001101111011010110110111101101100011011110111001101101000

Offline NightLitch

  • Emperor
  • **
  • Posts: 1 455
  • Karma: +16/-0
(No subject)
« Reply #16 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

Code: [Select]
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

Offline Psycho_Chihuahua

  • Systemspecialist IT
  • Emperor
  • **
  • Posts: 1 054
  • Karma: +112/-1
  • I am wherever i want to be
    • PtokaX Luaboard
(No subject)
« Reply #17 on: 23 August, 2004, 21:07:59 »
well i think i got the point...

If this is right that is...
Code: [Select]
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

Offline NightLitch

  • Emperor
  • **
  • Posts: 1 455
  • Karma: +16/-0
(No subject)
« Reply #18 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.

Code: [Select]
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

Offline Psycho_Chihuahua

  • Systemspecialist IT
  • Emperor
  • **
  • Posts: 1 054
  • Karma: +112/-1
  • I am wherever i want to be
    • PtokaX Luaboard
(No subject)
« Reply #19 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
PtokaxWiki ?PtokaX Mirror + latest Libs

01100001011011000111001101101111001000000110101101101110011011110111011101101110001000000110000101110011001000000101010001101111011010110110111101101100011011110111001101101000

Offline Dj_OcTaGoN

  • Forum Ace
  • *****
  • Posts: 96
  • Karma: +1/-0
(No subject)
« Reply #20 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


Offline NightLitch

  • Emperor
  • **
  • Posts: 1 455
  • Karma: +16/-0
(No subject)
« Reply #21 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
//NL

Herodes

  • Guest
(No subject)
« Reply #22 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 :
Code: [Select]
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) ...

Offline Dj_OcTaGoN

  • Forum Ace
  • *****
  • Posts: 96
  • Karma: +1/-0
(No subject)
« Reply #23 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


Offline BottledHate

  • Triple Ace
  • **
  • Posts: 244
  • Karma: +2/-0
(No subject)
« Reply #24 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

-BH
Homepage: www.bottledhate.webhop.org

Compiling  Lua scripts is LAME!!!!!

PtokaX forum

(No subject)
« Reply #24 on: 04 September, 2004, 22:40:58 »