PtokaX forum

Archive => Archived 4.0 boards => Request for Lua 4 scripts => Topic started by: b_w_johan on 19 November, 2004, 19:54:18

Title: read .txt from folder cant make it workin'
Post by: b_w_johan on 19 November, 2004, 19:54:18
cant get it right...
i was trying to make a small script which reads from a folder "txt" and opens *.txt depending the command.
well i was puzzeling all the time and couldn't figure it out must be something very small and stupid...


this is what i try to make work now..


bot = "Guard"
AantUur = 1   -- aantal uren
hrs = 1000 * 60 * 60 * AantUur

function Main()
   SetTimer(hrs)
   StartTimer()
end

function OnTimer()
   SendRules(user)
end

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


if (cmd=="+rules") then
   SendRules(user)
end



function SendRules(user)
   readfrom("txt/rules.txt")
   local text = "\r\n"
   while 1 do
      line = read()
         if line == nil then
            break
         end
      text = text.."\r\n"..line
   end
   user:SendPM(bot,text.."|")
   readfrom()
end




this is what i started with:

bot = "Guard"
AantUur = 1   -- aantal uren
textfile = "txt/Rules.txt"
hrs = 1000 * 60 * 60 * AantUur


function Main()
   SetTimer(hrs)
   StartTimer()
end

function OnTimer()
   massMessageToAll()
end




if (cmd=="+rules") then
   SendUserRules(user)
end

function SendUserRules(user)
   readfrom("txt/Rules.txt")
   while 1 do
      line = read()
      if line == nil then break end
         SendToAll(bot, line)
      end
   readfrom()
end

function massMessageToAll()
local handle = openfile(textfile, "r")
   if (handle ~= nil) then
      local line = read(handle)
                  Temptimerfile = "\r\n"
      while line do
         Temptimerfile = Temptimerfile.."\r\n"..line
         line = read(handle)
      end
         SendPmToAll(bot, Temptimerfile)
      closefile(handle)
   end
end




but they wont work ...
(yes i picked the small box in ptokax to enable textfile support someone pointed me to that..)
so if you could help me i would be very happy ....


Thanx Johan Nolte
Title:
Post by: Themaster on 19 November, 2004, 20:19:18
maybe you need this

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
Title:
Post by: b_w_johan on 19 November, 2004, 20:45:30
I can't get it working can you put all of the source in here ??
and tell me if it worked by you ?
cause i used that part but it isn't going to work..
grrrr

thx Johan
Title:
Post by: Themaster on 19 November, 2004, 20:47:37
Kos = "[RuLeR]"

Command = "+rules"

MasRules = "text/masrules.txt"
OpRules = "text/oprules.txt"
VipRules = "text/viprules.txt"
RegRules = "text/regrules.txt"

function Main()
frmHub:RegBot(Kos)
end

function DataArrival(ustring,dstring)
if strsub(dstring, 1, 1) == "<" then
dstring=strsub(dstring,1,strlen(dstring)-1)
local s,e,cmd = strfind(dstring,"%b<>%s+(%S+)")

if cmd == Command then
if ustring.iProfile == 0 then
file = MasRules
WhileRead(ustring,file)
elseif ustring.bOperator then
file = OpRules
WhileRead(ustring,file)
elseif ustring.iProfile == 2 then
file = VipRules
WhileRead(ustring,file)
else
file = RegRules
WhileRead(ustring,file)
end
end
end
end

function WhileRead(ustring,file)
readfrom(file)
while 1 do
line = read()
if line == nil then
break
end
ustring:SendPM(Kos,line)
end
  readfrom()
end
Title:
Post by: b_w_johan on 19 November, 2004, 21:16:17
Thank you very much now its working !
lol only change it a bit to show themevery 2 owrs in main ..
Title:
Post by: Themaster on 19 November, 2004, 21:19:44
np