PtokaX forum

Development Section => Your Developing Problems => Topic started by: nErBoS on 13 October, 2003, 17:42:24

Title: Read-Text problem
Post by: nErBoS on 13 October, 2003, 17:42:24
Hi all,

Have a problem with this bot

Bot = "Text-Bot"

rules = "rules.txt"

function DataArrival(user, data)
if( strsub(data, 1, 1) == "<" ) then
s,e,cmd = strfind( data, "%b<>%s+(%S+)%s*")
cmd=strsub(cmd,1,strlen(cmd)-1)
if (cmd=="!rules") then
rules = Readtextfile(rules)
user:SendPM(Bot, rules)
rules = nil
return 1
end
end
end

function Readtextfile(file)

local text = ""
local line
local ret, err =  readfrom(file)
if (ret) then
while 1 do
line = read()
if not line then
break
else
text = text..line.."\r\n"
end
end
readfrom()
else
text = ""
Writetextfile(file, text)
end
return text
end

function Writetextfile(file, text)
writeto(file)
write(text)
writeto()
end

When the comand is call for the first time , it works, the bot sends the text, but when called on a second time the command appears in the main chat and the bot doesn?t sends the text and on the script editor e have this error

"Syntax Error: bad argument #1 to `readfrom' (string expected, got nil)"

Can anyone help me ???

Best regrads, nErBoS
Title:
Post by: pHaTTy on 13 October, 2003, 17:48:49
Hmmm i dont understand Else what????

in the readtextfile
Title:
Post by: pHaTTy on 13 October, 2003, 17:50:42
Hmmm si this what ur trying to do for that function



function Readtextfile(file)

local text = ""
local line
local ret, err =  readfrom(file)
if (ret) then
while 1 do
line = read()
if not line then
text = ""
Writetextfile(file, text)
               return text
else
text = text..line.."\r\n"
end
end
readfrom()
end

Title:
Post by: nErBoS on 13 October, 2003, 18:10:11
The readtext function that i posted i goted from glory securitaz !!

I used your function the first time i called the bot doesn?t sends the text back at the second time that i called i get the same error that i have gotten before "Syntax Error: bad argument #1 to `readfrom' (string expected, got nil)"
Title:
Post by: pHaTTy on 13 October, 2003, 18:28:23
I dont understadn what you are trying to write to the file :S
Title:
Post by: [ES]latinmusic on 13 October, 2003, 22:17:20
Yo have some errors present, i have no time for explanations now, use this code.
Bot = "Text-Bot"
rules = "rules.txt"
function DataArrival(user, data)
if( strsub(data, 1, 1) == "<" ) then
s,e,cmd = strfind( data, "%b<>%s+(%S+)%s*")
cmd=strsub(cmd,1,strlen(cmd)-1)
if (cmd=="!rules") then
Readtextfile(user, rules)
end
end

end
function Readtextfile(user, rules)
local handle = openfile(rules, "r")
if (handle ~= nil) then
local line = read(handle)
while line do
user:SendPM(Bot, line)
line = read(handle)
end
closefile(handle)
end
end
Title:
Post by: nErBoS on 14 October, 2003, 02:09:59
Ok [ES]latinmusic that script works but the bot sends the text line by line how can i do to make the bot send all !!

For eg the script you gave sends this:

xxxxxx
yyyyy
zzzzzzzz

i would like to do this:

xxxxxx
yyyyyy
zzzzzzzzzz

Can be done ??
Title:
Post by: plop on 14 October, 2003, 03:52:46
not tested but should work like you want

Bot = "Text-Bot"

rules = "rules.txt"

function DataArrival(user, data)
if( strsub(data, 1, 1) == "<" ) then
s,e,cmd = strfind( data, "%b<>%s+(%S+)%s*")
cmd=strsub(cmd,1,strlen(cmd)-1)
if (cmd=="!rules") then
Readtextfile(user, rules)
end
end
end

function Readtextfile(user, rules)
   local filecontents = ""
local handle = openfile(rules, "r")
if (handle ~= nil) then
local line = read(handle)
while line do
         filecontents = filecontents..line.."\r\n"
line = read(handle)
end
closefile(handle)
      user:SendPM(Bot, filecontents.." |")
end
end

plop
Title:
Post by: [ES]latinmusic on 14 October, 2003, 10:55:21
Yes line by line, test the plop script, if not work i will modify it later, now working, surry :p
Title:
Post by: nErBoS on 14 October, 2003, 13:05:43
Yes working great i made this little modificacion to work on all files you want !!

Bot = "Text-Bot"

rules = "rules.txt"

function DataArrival(user, data)
if( strsub(data, 1, 1) == "<" ) then
s,e,cmd = strfind( data, "%b<>%s+(%S+)%s*")
cmd=strsub(cmd,1,strlen(cmd)-1)
if (cmd=="!rules") then
Readtextfile(user, rules)
return 1
end
end
end

function Readtextfile(user, file)
    local filecontents = ""
local handle = openfile(file, "r")
if (handle ~= nil) then
local line = read(handle)
while line do
        filecontents = filecontents..line.."\r\n"
line = read(handle)
end
closefile(handle)
      user:SendPM(Bot, filecontents)
end
end

Many thanks to all for the help !!

Best regards, nErBoS
Title:
Post by: [ES]latinmusic on 14 October, 2003, 13:26:54
Great!