PtokaX forum

Archive => Archived 4.0 boards => Request for Lua 4 scripts => Topic started by: Madman on 08 May, 2004, 01:07:39

Title: how to make Prefix?
Post by: Madman on 08 May, 2004, 01:07:39
Hi, i am working on a little ascii bot... =)
just to learn some lua but most becuse i am bored....

If some one use the command !lol
It show a ascii picture

but i want it to respond to +lol and !lol
something like

Prefix = "!" and "+"
if cmd == Prefix"lol" then

But i dont know how to do that...
Title:
Post by: Corayzon on 08 May, 2004, 04:12:01
hey... you can use a table to achieve this like so...

-- Defines a table
Prefix = {}

-- Adds + to the table and sets it to 1
Prefix["+"] = 1

-- Adds ! to the table and sets it to 1
Prefix["!"] = 1

then u would do the following to execute on one of the prefixs...

Prefix = {}
Prefix["+"] = 1
Prefix["!"] = 1

-- if cmd not equal nil
if cmd ~= nil then

-- if the first char in command is equal to 1
if Prefix[strsub(cmd, 1, 1) == 1 then

-- remove the prefix from the cmd
cmd = strsub(cmd, 2, strlen(cmd) - 1)


end
end

and now you check for the command name and do whatever u like

Prefix = {}
Prefix["+"] = 1
Prefix["!"] = 1

-- if cmd not equal nil
if cmd ~= nil then
if Prefix[strsub(cmd, 1, 1) == 1 then
cmd = strsub(cmd, 2, strlen(cmd) - 1)

if cmd == "lol" then
-- put ur code here
end
end
end

hope this helps ;)
Title:
Post by: Madman on 08 May, 2004, 05:20:50
I cant get it to work...

I tested with this code

Bot = "Doo"

--// This function is fired at the serving start
function Main()
frmHub:RegBot(Bot)
end
Prefix = {}
Prefix["+"] = 1
Prefix["!"] = 1


--// This function is fired when a new data arrives
function DataArrival(curUser,data)
if strsub(data, 1, 1) == "<" then
data=strsub(data,1,strlen(data)-1)
s,e,cmd = strfind(data,"%b<>%s+(%S+)")
-- if cmd not equal nil
if cmd ~= nil then
if Prefix[strsub](cmd, 1, 1) == 1 then
cmd = strsub(cmd, 2, strlen(cmd) - 1) --//line 20
if cmd == "lol" then
SendToAll(Bot,"Wooo")

end
end
end
end
end
Got the error
Syntax error: attempt to call a nil value
stack traceback:
   1:  function `DataArrival' at line 20 [file `D:\Program\Hub\PtokaX-0.330\scripts\prfix.lua']

And btw you forgot the ] in this line =)
if Prefix[strsub](cmd, 1, 1) == 1 then
Title:
Post by: Corayzon on 08 May, 2004, 06:24:32
wops...i never really test the shit i put helping other ppl but...

if Prefix[strsub(cmd, 1, 1) == 1 then
should have been

if Prefix[strsub(cmd, 1, 1)]= 1 then
and

cmd = strsub(cmd, 2, strlen(cmd) - 1)
shoulda been

cmd = strsub(cmd, 2, strlen(cmd))

so here the complete script with dataarrival

Prefix = {}
Prefix["+"] = 1
Prefix["!"] = 1

function DataArrival(user, data)

data = strsub(data, 1, strlen(data) -1)
local _,_,cmd=strfind(data, "%b<>%s+(%S+)")

if cmd ~= nil then
if Prefix[strsub(cmd, 1, 1)] == 1 then
cmd = strsub(cmd, 2, strlen(cmd))

if cmd == "lol" then
user:SendData("testing", "lol called!")
end
end
end
end

;)
Title:
Post by: Madman on 08 May, 2004, 13:19:53
Thanx i got it working now =)