PtokaX forum

Archive => Archived 5.0 boards => Help with scripts => Topic started by: nEgativE on 15 March, 2005, 13:38:32

Title: Write/Read Functions
Post by: nEgativE on 15 March, 2005, 13:38:32
Hi, i need more help :)

Could someone check if there is something wrong with the function ReadRelease ? i'm able to write, but the Read function does not return correct.. tks in advance.


------------------------------------------------------
function WriteRelease(user, data, cmd)

arg = GetArgs(data)
if arg == nil then
arg = "Sem coment?rios no Hub."
user:SendData(sBot, arg)
return 1
end

local handle = io.open("Data/CLP.GuestBook.dat","a")
timedate = os.date()
handle.write(handle,timedate.." - "..user.sName..": "..arg.."?")
SendPmToOps(sChatBot,"DC:CLP - Novo coment?rio no GuestBook do Hub por: "..user.sName.."")
SendToAll(sBot,"DC:CLP - Novo coment?rio no GuestBook do Hub, digite: !livro para ler o(s) coment?rio(s).")
handle:close()
end

function ReadRelease(user, data, cmd)

user:SendPM(sBot, ":.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:")
handle = io.open("Data/CLP.GuestBook.dat","r")
if (handle) then
line = handle.read(handle,"*a")
line=string.sub(line,1,string.len(line)-1)
linearray=tokenize(line,"?")
for i=1,linearray.n do
user:SendPM(sBot,linearray[i])
end
handle:close()
user:SendPM(sBot, ":.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:")
end
end
------------------------------------------------------

Title:
Post by: Jelf on 15 March, 2005, 14:09:03
Change this
handle.write(handle,timedate.." - "..user.sName..": "..arg.."?") to this handle:write(handle,timedate.." - "..user.sName..": "..arg.."?")And this line = handle.read(handle,"*a") to this line = handle:read()Then should be ok :)
Title:
Post by: jiten on 15 March, 2005, 14:50:40
darn, some minutes late  ;)
Title:
Post by: nEgativE on 15 March, 2005, 16:28:35
Hehe jiten, still get some syntax errors on this one !

Write function --> script.lua:1199: bad argument #1 to `write' (string expected, got userdata)

Read function --> script.lua:1213: `for' limit must be a number
Title:
Post by: jiten on 15 March, 2005, 16:48:05
Can u post the whole script so that I can have a better look at it?
Title:
Post by: nEgativE on 15 March, 2005, 18:24:35

sBot = "?per"
sChatBot ="Moita"

function tokenize (inString,token)

_WORDS = {}
local matcher = "([^?"..token.."]+)"
string.gsub(inString, matcher, function (w) table.insert(_WORDS,w) end)
return _WORDS
end

function ChatArrival(user, data)

data=string.sub(data,1,string.len(data)-1)
_,_,cmd=string.find(data, "%b<>%s+(%S+)")

if (cmd=="!livro") then
ReadRelease(user, data, cmd)
return 1
else
if (cmd=="!escrever") then
WriteRelease(user, data, cmd)
return 1
end
end
end

function GetArgs(data)
s,e,arg = string.find( data, "%b<>%s+%S+%s+(.+)" )
return arg
end
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
function WriteRelease(user, data, cmd)

arg = GetArgs(data)
if arg == nil then
arg = "Sem coment?rios no Hub."
user:SendData(sBot, arg)
return 1
end

handle = io.open("Data/CLP.GuestBook.dat","a")
timedate = os.date()
handle:write(handle,timedate.." - "..user.sName..": "..arg.."?")
SendPmToOps(sChatBot,"DC:CLP - Novo coment?rio no GuestBook do Hub por: "..user.sName.."")
SendToAll(sBot,"DC:CLP - Novo coment?rio no GuestBook do Hub, digite: !livro para ler o(s) coment?rio(s).")
handle:close()
end

function ReadRelease(user, data, cmd)

user:SendPM(sBot, ":.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:")
handle = io.open("Data/CLP.GuestBook.dat","r")
if (handle) then
line = handle:read()
line=string.sub(line,1,string.len(line)-1)
linearray=tokenize(line,"?")
for i=1,linearray.n do
user:SendPM(sBot,linearray[i])
end
handle:close()
user:SendPM(sBot, ":.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:")
end
end

Title:
Post by: jiten on 15 March, 2005, 19:43:53
Here you go:

--Converted to LUA 5 by jiten
sBot = "?per"
sChatBot ="Moita"

function tokenize (inString,token)

_WORDS = {}
local matcher = "([^?"..token.."]+)"
string.gsub(inString, matcher, function (w) table.insert(_WORDS,w) end)
return _WORDS
end

function ChatArrival(user, data)

data=string.sub(data,1,string.len(data)-1)
_,_,cmd=string.find(data, "%b<>%s+(%S+)")

if (cmd=="!livro") then
ReadRelease(user, data, cmd)
return 1
else
if (cmd=="!escrever") then
WriteRelease(user, data, cmd)
return 1
end
end
end

function GetArgs(data)
s,e,arg = string.find( data, "%b<>%s+%S+%s+(.+)" )
return arg
end
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
function WriteRelease(user, data, cmd)

arg = GetArgs(data)
if arg == nil then
arg = "Sem coment?rios no Hub."
user:SendData(sBot, arg)
return 1
end

local handle = io.open("Data/CLP.GuestBook.dat","a+")
assert(handle, "Data/CLP.GuestBook.dat")
timedate = os.date()
handle:write(timedate.." - "..user.sName..": "..arg.."?")
SendPmToOps(sChatBot,"DC:CLP - Novo coment?rio no GuestBook do Hub por: "..user.sName.."")
SendToAll(sBot,"DC:CLP - Novo coment?rio no GuestBook do Hub, digite: !livro para ler o(s) coment?rio(s).")
handle:close()
end

function ReadRelease(user, data, cmd)

user:SendPM(sBot, ":.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:")
handle = io.open("Data/CLP.GuestBook.dat","r")
if (handle) then
line = handle:read()
line=string.sub(line,1,string.len(line)-1)
linearray=tokenize(line,"?")
for i=1,table.getn(linearray) do
user:SendPM(sBot,linearray[i])
end
handle:close()
user:SendPM(sBot, ":.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:")
end
end

Title:
Post by: nEgativE on 15 March, 2005, 19:54:49
running perfect :) tks again []'s
Title:
Post by: jiten on 15 March, 2005, 20:25:53
yw, no probs :)