Author Topic: Read-Text problem  (Read 4374 times)

0 Members and 1 Guest are viewing this topic.

Offline nErBoS

  • Emperor
  • **
  • Posts: 1 149
  • Karma: +3/-1
Read-Text problem
« on: 13 October, 2003, 17:42:24 »
Hi all,

Have a problem with this bot

Code: [Select]
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
« Last Edit: 13 October, 2003, 17:48:30 by nErBoS »
--## nErBoS Spot ##--

PtokaX forum

Read-Text problem
« on: 13 October, 2003, 17:42:24 »

Offline pHaTTy

  • Forum Legend
  • ***
  • Posts: 1 916
  • Karma: +5/-1
(No subject)
« Reply #1 on: 13 October, 2003, 17:48:49 »
Hmmm i dont understand Else what????

in the readtextfile
Resistance is futile!

Offline pHaTTy

  • Forum Legend
  • ***
  • Posts: 1 916
  • Karma: +5/-1
(No subject)
« Reply #2 on: 13 October, 2003, 17:50:42 »
Hmmm si this what ur trying to do for that function


Code: [Select]

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

Resistance is futile!

Offline nErBoS

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

Offline pHaTTy

  • Forum Legend
  • ***
  • Posts: 1 916
  • Karma: +5/-1
(No subject)
« Reply #4 on: 13 October, 2003, 18:28:23 »
I dont understadn what you are trying to write to the file :S
Resistance is futile!

Offline [ES]latinmusic

  • Triple Ace
  • **
  • Posts: 236
  • Karma: +1/-0
(No subject)
« Reply #5 on: 13 October, 2003, 22:17:20 »
Yo have some errors present, i have no time for explanations now, use this code.
Code: [Select]
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

Offline nErBoS

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

Offline plop

  • Forum God
  • ****
  • Posts: 2 464
  • Karma: +37/-0
(No subject)
« Reply #7 on: 14 October, 2003, 03:52:46 »
not tested but should work like you want

Code: [Select]
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
http://www.plop.nl lua scripts/howto\'s.
http://www.thegoldenangel.net
http://www.vikingshub.com
http://www.lua.org

>>----> he who fights hatred with hatred, drives the spreading of hatred <----<<

Offline [ES]latinmusic

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

Offline nErBoS

  • Emperor
  • **
  • Posts: 1 149
  • Karma: +3/-1
(No subject)
« Reply #9 on: 14 October, 2003, 13:05:43 »
Yes working great i made this little modificacion to work on all files you want !!

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

Offline [ES]latinmusic

  • Triple Ace
  • **
  • Posts: 236
  • Karma: +1/-0
(No subject)
« Reply #10 on: 14 October, 2003, 13:26:54 »
Great!

PtokaX forum

(No subject)
« Reply #10 on: 14 October, 2003, 13:26:54 »