PtokaX forum

Development Section => LUA & PtokaX-Scripting-Interface => Topic started by: Yahoo on 10 April, 2007, 15:37:24

Title: help
Post by: Yahoo on 10 April, 2007, 15:37:24
what do this mean??
local s,e,msg = string.find(data, "%b<>%s+%S+%s(.*)")
Title: Re: help
Post by: Thor on 10 April, 2007, 16:04:08
The second and the other words in a chat message.
%b<> - This will search for the next following two signs (in chat messages the nick is like <Hungarista>)
%s+ - one, or more whitespace character (so at least one)
%S+ - one, or more non-space character
%s - just one whitespace
(.*) - . means this character can be everything, so whitespace, number, letter, ASCII char, everything. * indicates that it will be there or not, so 0 or more. The braces put the found value(s) into the variable called 'msg'. The first two return value of string.find is two number, the first and the last index of the string where the match found.
For more details check the lua.org page, it's pure lua.
Title: Re: help
Post by: achiever on 10 April, 2007, 18:11:42

s,e,cmd = string.find(data,"%b<>%s+(%S+)|")
if (cmd=="+help") then

now i need to work this command with all prefix so what do i need to add. i mean how can 1 detect prefix alone other than the word followed.
Title: Re: help
Post by: bastya_elvtars on 10 April, 2007, 18:24:34
http://wiki.ptokax.ath.cx/doku.php?id=scriptinghelp:strings

Read this first.
Title: Re: help
Post by: achiever on 10 April, 2007, 18:55:06
thanks for that bastya :P but i was not able to comprehend much from that sorry for my unintelligence.
so is some 1 willing to answer what i had asked more clearly.
Title: Re: help
Post by: bastya_elvtars on 10 April, 2007, 20:33:58
Decided to give you hints. You need to grab ANY punctuation character that is right in the beginning of the text from the user, or you may want to specify the set of characters wanted directly.
Title: Re: help
Post by: Yahoo on 10 April, 2007, 20:54:07
thanks you all for all ur help
Title: Re: help
Post by: Cêñoßy†ê on 10 April, 2007, 20:58:30
meaned like this ?
Code (lua) Select

local _,_,prefix,cmd = data:find("%b<>%s+(%p)(%S+)")
if cmd == "help" then


it reacts to !help  +help  /help etc..
Title: Re: help
Post by: bastya_elvtars on 10 April, 2007, 20:59:31
Now this is what I wanted him to find out by himself.
Title: Re: help
Post by: Cêñoßy†ê on 10 April, 2007, 21:02:22
Quote from: bastya_elvtars on 10 April, 2007, 20:59:31
Now this is what I wanted him to find out by himself.

sry :P
Title: Re: help
Post by: achiever on 11 April, 2007, 17:38:14
thks for help cenobyte but i m not sure this line is working as it should
here is the code i had
function ChatArrival( user , data )
s,e,cmd = string.find(data,"%b<>%s+(%S+)|")
if cmd == "+show" then
--SendToAll(user.sName, prefix)
SendToAll("<Fusion>Type prefix-show to show this line")
return 1
end
end


this is what it looked like after change
function ChatArrival( user , data )
local _,_,prefix,cmd = data:find("%b<>%s+(%p)(%S+)")
if cmd == "show" then
--SendToAll(user.sName, prefix)
SendToAll("<Fusion>Type prefix-show to show this line")
return 1
end
end


but it stops to work entirely after the change.

bastya a bit of help at the start of learning, does help to learn faster.

thks,
achiever.
Title: Re: help
Post by: speedX on 11 April, 2007, 17:48:17

function ChatArrival( user , data )
local _,_,prefix,cmd = data:find("%b<>%s+(%p)(%S+)|")
if cmd == "show" then
--SendToAll(user.sName, prefix)
SendToAll("<Fusion>Type prefix-show to show this line")
return 1
end
end
Title: Re: help
Post by: bastya_elvtars on 11 April, 2007, 17:49:58
Quote from: achiever on 11 April, 2007, 17:38:14
but it stops to work entirely after the change.

bastya a bit of help at the start of learning, does help to learn faster.

I think giving hints makes learning even faster. Anyway, in the second chunk there is a | missing.
Title: Re: help
Post by: achiever on 11 April, 2007, 18:09:12
well i think it depends on person to person how they learn things.
thks for ur hint, understood it this time.
achiever.
Title: Re: help
Post by: bastya_elvtars on 11 April, 2007, 18:12:16
Quote from: achiever on 11 April, 2007, 18:09:12
well i think it depends on person to person how they learn things.
thks for ur hint, understood it this time.

Sure, and pattern matching is not the most straightforward aspect of Lua.
You're welcome. :-)
Title: Re: help
Post by: achiever on 11 April, 2007, 18:38:48
now moving on...

sBot = "Bot name"
function ChatArrival( user , data )
local _,_,prefix,cmd = data:find("%b<>%s+(%p)(%S+)|")
if prefix == "+" then
SendToAll(user.sName, cmd)
SendToAll(sBot, "Type prefix-help for list of commands available to ur profile")
return 1
end
end


this works when there is a command with '+' as prefix with an unkown command (if script is placed last of all scripts) and gives the output, but how can add it to work with all perfixes
and also it returns only the command and not the prefix used, so what should it be?
SendToAll(user.sName, prefix,cmd)
this did not work.
Title: Re: help
Post by: speedX on 11 April, 2007, 18:54:17
What is the difference between
local s,e,prefix,cmd and local _,_,prefix,cmd  ??
Title: Re: help
Post by: bastya_elvtars on 11 April, 2007, 19:00:04
Quote from: achiever on 11 April, 2007, 18:38:48
now moving on...
this works when there is a command with '+' as prefix with an unkown command (if script is placed last of all scripts) and gives the output, but how can add it to work with all perfixes
and also it returns only the command and not the prefix used, so what should it be?
SendToAll(user.sName, prefix,cmd)
this did not work.

It ONLY works with the + prefix. Remove that restriction from the code.
SendToAll(user.sName, prefix,cmd) - this won't work as SendToAll() accepts only 1 or 2 arguments. With 1, it sends the argument out as rew data, with 2 arguments, the first one is the nick sending the stuff, the second is the stuff itself (sending it as <nick> stuff|)
Quote from: speedX on 11 April, 2007, 18:54:17
What is the difference between
local s,e,prefix,cmd and local _,_,prefix,cmd  ??

Nothing, it does the same. Underscores are variable placeholders, s and e are true variables but very few use that two when capturing a pattern. This needs further explanation:
A function returns 4 values but you only want the last 2. If you simply do a,b=dothis() then you get the first 2 values. So if you do _,_,a,b=dothis() then you declare that you only want the last 2 values.
Usage example: dothat() returns 5 values.
Getting the first one: x=dothat()
Getting the first and secod one: x,y=dothat()
Getting the second, third and fifth: x,_,y,_,z=dothat()
Getting the second, third and fourth: _,x,y,z=dothat()
Getting all but the fourth: x,y,z,_,w=dothat()
Title: Re: help
Post by: bastya_elvtars on 11 April, 2007, 23:50:22
Oh, and in Lua 5.1 you can also use string.gmatch, when the start and end positions aren't returned at all. :-)
Title: Re: help
Post by: achiever on 12 April, 2007, 18:26:10
Quote from: Mutor on 11 April, 2007, 18:49:23
local sBot = "BotName"
function ChatArrival( user , data )
local s,e,prefix,cmd = data:find("%b<>%s+(%p)(%S+)|")
if prefix and cmd then
SendToAll(user.sName, prefix..cmd)
SendToAll(sBot, "Type prefix-help for list of commands available to ur profile")
return 1
end
end


this code is stoping me to use basic pxcommands, ne way arround???
Title: Re: help
Post by: Naithif on 12 April, 2007, 18:29:56
Quote from: achiever on 12 April, 2007, 18:26:10
this code is stoping me to use basic pxcommands, ne way arround???


You should have another condition requiring your command to be something specific, in the above case it reacts to every punct. char + word combination.

(just a hint ;))
Title: Re: help
Post by: achiever on 12 April, 2007, 18:42:17
so should there be a list of commads which this should ignore?
Title: Re: help
Post by: Naithif on 12 April, 2007, 18:45:43
Quote from: achiever on 12 April, 2007, 18:42:17
so should there be a list of commads which this should ignore?

If you want it to react to every unrecognized command, then well, yes.
Title: Re: help
Post by: bastya_elvtars on 12 April, 2007, 18:48:53
Or do not return 1 (but then the command will show up).
Title: Re: help
Post by: Naithif on 12 April, 2007, 18:52:52
Quote from: bastya_elvtars on 12 April, 2007, 18:48:53
Or do not return 1 (but then the command will show up).

Yes, every time, even when a command was specified in a good way :D
And since it's SendToAll it would result in some spam on an actively used hub :)

Anyway why don't you send it to just the user who entered the command?
Title: Re: help
Post by: achiever on 13 April, 2007, 13:10:37
Quote from: bastya_elvtars on 12 April, 2007, 18:48:53
Or do not return 1 (but then the command will show up).
yes on this the pxcommands do work but still the line
"Type prefix-help for list of commands available to ur profile"
is always shown

Quote from: Naithif on 12 April, 2007, 18:52:52
Anyway why don't you send it to just the user who entered the command?

well i need it to show command toevery user so the new users come to kno that these commands are present.
hi,

well ppl i have just done some scripting (well modified a completely different script ;-) )

sBot = "Fusion"
Commands = {
["myip"] = 1,
["stat"] = 1,
["reloadtxt"] = 1,
["getscripts"] = 1,
["restartscripts"] = 1,
["clrrangetempbans"] = 1,
["clrtempbans"] = 1,
["clrrangepermbans"] = 1,
["clrpermbans"] = 1,
["fav"] = 1,
["getrangebans"] = 1,
["getrangepermbans"] = 1,
["getpermbans"] = 1,
["getrangetempbans"] = 1,
["gettempbans"] = 1,
["help"] = 1,
}

function ChatArrival( user, data)
local s,e,prefix,cmd = data:find("%b<>%s+(%p)(%S+)|")
if not Commands[cmd] then
if prefix and cmd then
SendToAll(sBot, "Type prefix-help for list of commands available to ur profile")
--[[elseif prefix then
SendToAll(sBot, "Did u forget to use the command")]]--
end
end
end


the comment part  is not working--> it should send message if only perfix is recieved.

well this script also keeps on sending the line
" Type prefix-help for list of commands available to ur profile "
now for the text files of ptokax even if i get to c them.
so can some 1 help me with a better solution?

achiever.
Title: Re: help
Post by: Cêñoßy†ê on 13 April, 2007, 15:44:03
this is how i do it
Code (lua) Select

cmd = {}
ChatArrival = function(user,data)
local data = string.sub(data,1,(string.len(data)-1))
local _,_,prefix,command = data:find("%b<>%s+(%p)(%S+)")
if command and cmd[command] then
                SendToAll(data)  -- shows command to all
return cmd[command](user,data)
end
collectgarbage("collect")
end

cmd["test"] = function(user,data)
user:SendData(frmHub:GetHubBotName(),"command 'test' received")
return 1
end
Title: Re: help
Post by: achiever on 13 April, 2007, 15:51:21
getting error
[19:12] Syntax D:\Downloads\0.3.5.1.lua5.1.1\scripts\test2.lua:34: attempt to index global 'cmd' (a nil value)
Title: Re: help
Post by: Cêñoßy†ê on 13 April, 2007, 16:01:42
Quote from: achiever on 13 April, 2007, 15:51:21
getting error
[19:12] Syntax D:\Downloads\0.3.5.1.lua5.1.1\scripts\test2.lua:34: attempt to index global 'cmd' (a nil value)
post updated with cmd = {} in script start
Title: Re: help
Post by: achiever on 14 April, 2007, 14:08:38
hi cenobyte,

i m abit confused,
i was trying to make a script which would send a message
"Type prefix-help for list of commands available to ur profile"
if user uses a unknown command.

the script i made up started to send the message for ptokax basic commands also, so i had to add that table.
but now still it is sending the message for the text files which i use.
so now plzz explain how should i use ur code to help solving my problem.

thks,
achiever.