Table structures
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

Table structures

Started by Markitos, 30 March, 2006, 10:53:29

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Markitos

This is a table structure that i was trying to write instead of repeating many elseif...it's FaqBot script writen by TTB
function ChatArrival(curUser,data)
	HowToSend = "Main"
	data = string.sub(data,1,string.len(data)-1)
	local s,e,cmd = string.find(data,"^%b<>%s+[%!%+%#%?](%S+)")
	local _,_,_,fileordir = string.find(data,"^%b<>%s+(%S+)%s+([^|^ ]+)")
	if cmd then
			local tCmds = {
	["faqsave"] = function(curUser,data)
		if Profiles[curUser.iProfile] == 1 then
			FaqSave(curUser,data,fileordir)
		return 1
	end,
		
	["faq"] = function(curUser,data)
		FaqLoad(curUser,data,fileordir)
		return 1
	end,
	["faqdel"] = function(curUser,data)
		if Profiles[curUser.iProfile] == 1 then
			FaqDel(curUser,data,fileordir)
		return 1
	end,
	["sfaq"] = function(curUser,data)
		if Profiles[curUser.iProfile] == 1 then
			FaqSend(curUser,data,fileordir)
		return 1
	end,
	["faqrefresh"] = function(curUser,data)
		if Profiles[curUser.iProfile] == 1 then
			FaqRefresh(curUser,data)
		return 1
	end,
	["faqhelp"] = function (curUser,data)
		if Profiles[curUser.iProfile] == 1 then
			SendToUser(curUser, "\r\n\r\n"..
			"\t--<>--------------------------------------------------------------------------------------------------------------------------------------------------------<>--\r\n"..
			"\t\t\t [ FAQ Help ]\t\t\t [ FAQ Help ] \r\n"..
			"\t--<>--------------------------------------------------------------------------------------------------------------------------------------------------------<>--\r\n"..
			"\t\t"..prefix.."faq \t\t\t=\tShows available faqs\r\n"..
			"\t\t"..prefix.."faq <faqname>\t\t=\tLoad a faq\r\n"..
			"\t\t"..prefix.."sfaq <faqname> <nick> \t=\tSend faq to nick (only for OP's)\r\n"..
			"\t\t"..prefix.."faqsave <faqname> <info> \t=\tSave to file (only for OP's)\r\n"..
			"\t\t"..prefix.."faqdel <faqname>\t\t=\tDelete faq (only for OP's)\r\n"..
			"\t\t"..prefix.."faqrefresh \t\t=\tRefresh faq-index (only for OP's)\r\n"..
			"\t\t"..prefix.."faqhelp \t\t\t=\tShow help (only for OP's)\r\n"..
			"\t--<>--------------------------------------------------------------------------------------------------------------------------------------------------------<>--\r\n")
		return 1
	end
	}
	if tCmds[cmd] then
		return tCmds[cmd](user,data),1
	end
end

but i get this error FaqBot_LUA5.lua:44: unexpected symbol near '['
help woul be appreaciated

Herodes

Try the following... some 'end's where missing .. :)
function ChatArrival(curUser,data)
	HowToSend = "Main"
	data = string.sub(data,1,string.len(data)-1)
	local s,e,cmd = string.find(data,"^%b<>%s+[%!%+%#%?](%S+)")
	local _,_,_,fileordir = string.find(data,"^%b<>%s+(%S+)%s+([^|^ ]+)")
	if cmd then
			local tCmds = {
	["faqsave"] = function(curUser,data)
		if Profiles[curUser.iProfile] == 1 then
			FaqSave(curUser,data,fileordir)
		end
		return 1
	end,
		
	["faq"] = function(curUser,data)
		FaqLoad(curUser,data,fileordir)
		return 1
	end,
	["faqdel"] = function(curUser,data)
		if Profiles[curUser.iProfile] == 1 then
			FaqDel(curUser,data,fileordir)
		end
		return 1
	end,
	["sfaq"] = function(curUser,data)
		if Profiles[curUser.iProfile] == 1 then
			FaqSend(curUser,data,fileordir)
		end
		return 1
	end,
	["faqrefresh"] = function(curUser,data)
		if Profiles[curUser.iProfile] == 1 then
			FaqRefresh(curUser,data)
		end
		return 1
	end,
	["faqhelp"] = function (curUser,data)
		if Profiles[curUser.iProfile] == 1 then
			SendToUser(curUser, "\r\n\r\n"..
			"\t--<>--------------------------------------------------------------------------------------------------------------------------------------------------------<>--\r\n"..
			"\t\t\t [ FAQ Help ]\t\t\t [ FAQ Help ] \r\n"..
			"\t--<>--------------------------------------------------------------------------------------------------------------------------------------------------------<>--\r\n"..
			"\t\t"..prefix.."faq \t\t\t=\tShows available faqs\r\n"..
			"\t\t"..prefix.."faq <faqname>\t\t=\tLoad a faq\r\n"..
			"\t\t"..prefix.."sfaq <faqname> <nick> \t=\tSend faq to nick (only for OP's)\r\n"..
			"\t\t"..prefix.."faqsave <faqname> <info> \t=\tSave to file (only for OP's)\r\n"..
			"\t\t"..prefix.."faqdel <faqname>\t\t=\tDelete faq (only for OP's)\r\n"..
			"\t\t"..prefix.."faqrefresh \t\t=\tRefresh faq-index (only for OP's)\r\n"..
			"\t\t"..prefix.."faqhelp \t\t\t=\tShow help (only for OP's)\r\n"..
			"\t--<>--------------------------------------------------------------------------------------------------------------------------------------------------------<>--\r\n")
		end
		return 1
	end
	}
	if tCmds[cmd] then
		return tCmds[cmd](user,data),1
	end
end

jiten

Your code is missing some "end"s. Note that every "if" should end with an "end".
By the way, if you're creating a local command table inside ChatArrival, there's no need to use: function(curUser,data) again. That would be needed if it was created outside.
You could also remove the "return 1"s inside each command, as there will be duplication with this:
return tCmds[cmd](user,data),1

Correct tabbing would be helpful too. Well, hope this helps some :)

Regards

Markitos

#3
thanks 4 ur replies, i tried to fixe it but i got a new error \Ptokax\scripts\FaqBot_LUA5.lua:38: '=' expected near 'if'
function ChatArrival(curUser,data)
	HowToSend = "Main"
	data = string.sub(data,1,string.len(data)-1)
	local s,e,cmd = string.find(data,"^%b<>%s+[%!%+%#%?](%S+)")
	local _,_,_,fileordir = string.find(data,"^%b<>%s+(%S+)%s+([^|^ ]+)")
	if cmd then
		local tCmds = {
		["faqsave"]
		if Profiles[curUser.iProfile] == 1 then
			FaqSave(curUser,data,fileordir)
		end
	end,
		
	["faq"]
		FaqLoad(curUser,data,fileordir)
	end,
	["faqdel"]
		if Profiles[curUser.iProfile] == 1 then
			FaqDel(curUser,data,fileordir)
		end
	end,
	["sfaq"]
		if Profiles[curUser.iProfile] == 1 then
			FaqSend(curUser,data,fileordir)
		end
	end,
	["faqrefresh"]
		if Profiles[curUser.iProfile] == 1 then
			FaqRefresh(curUser,data)
		end
	end,
	["faqhelp"]
		if Profiles[curUser.iProfile] == 1 then
			SendToUser(curUser, "\r\n\r\n"..
			"\t--<>--------------------------------------------------------------------------------------------------------------------------------------------------------<>--\r\n"..
			"\t\t\t [ FAQ Help ]\t\t\t [ FAQ Help ] \r\n"..
			"\t--<>--------------------------------------------------------------------------------------------------------------------------------------------------------<>--\r\n"..
			"\t\t"..prefix.."faq \t\t\t=\tShows available faqs\r\n"..
			"\t\t"..prefix.."faq <faqname>\t\t=\tLoad a faq\r\n"..
			"\t\t"..prefix.."sfaq <faqname> <nick> \t=\tSend faq to nick (only for OP's)\r\n"..
			"\t\t"..prefix.."faqsave <faqname> <info> \t=\tSave to file (only for OP's)\r\n"..
			"\t\t"..prefix.."faqdel <faqname>\t\t=\tDelete faq (only for OP's)\r\n"..
			"\t\t"..prefix.."faqrefresh \t\t=\tRefresh faq-index (only for OP's)\r\n"..
			"\t\t"..prefix.."faqhelp \t\t\t=\tShow help (only for OP's)\r\n"..
			"\t--<>--------------------------------------------------------------------------------------------------------------------------------------------------------<>--\r\n")
		end
	end,
	}
	if tCmds[cmd] then
		return tCmds[cmd](curUser,data),1
	end
end

Herodes

Noted below :/
--- compare this 
	["faqsave"] = function( curUser, data )
		if Profiles[curUser.iProfile] == 1 then
			FaqSave(curUser,data,fileordir)
		end
	end,
---- to this 
	["faq"]
		FaqLoad(curUser,data,fileordir)
	end,

Markitos

#5
Quote from: Herodes on 30 March, 2006, 19:38:37
Noted below :/
--- compare this 
	["faqsave"] = function( curUser, data )
		if Profiles[curUser.iProfile] == 1 then
			FaqSave(curUser,data,fileordir)
		end
	end,
---- to this 
	["faq"]
		FaqLoad(curUser,data,fileordir)
	end,

Ops...edited but still same error  :-[

Madman

You are missing an end at the end.
You don't close the if cmd then....
Tab the commands and the
if tCmds[cmd] then
		return tCmds[cmd](curUser,data),1
	end

and you will see it
We suffer in silence, we lurk in the shadows, we kill in the night
Site currently down, ETA of returning online is 2099 ;p

Markitos

Quote from: Madman on 30 March, 2006, 20:52:38
You are missing an end at the end.
You don't close the if cmd then....
Tab the commands and the
if tCmds[cmd] then
		return tCmds[cmd](curUser,data),1
	end

and you will see it
still  ??? ??? ???

bastya_elvtars

If you have variable numbers of args, you can store them in an array and call the function unpack on it. Like: f(unpack({user,data}))
Everything could have been anything else and it would have just as much meaning.

Markitos

thanks for your replies but now its another error  scripts\FaqBot_LUA5.lua:79: attempt to index global 'tCmds' (a nil value)
The whole script...
-- FAQ bot by TTB
-- Stripped from [BOT]Vliegenmepper
-- Build on 28-10-2005
---------------------------------------------------------------------------------------------------
--- Settings
---------------------------------------------------------------------------------------------------
bot = "[BOT]Faq"
prefix = "!"
-->> Who can add // delete // send faqs?
Profiles = {
[-1] = 0,  -- Users
[0] = 1,   -- Masters
[1] = 1,   -- OPs
[2] = 0,   -- VIPs
[3] = 0,   -- REGs
[4] = 1,   -- MODs
[5] = 1,   -- Founders
}

---------------------------------------------------------------------------------------------------
--- Standard functions
---------------------------------------------------------------------------------------------------
indexfile = "faqindex" -- already has *.txt extension
faqfolder = "faqbot/"

function Main()
	frmHub:RegBot(bot)
end

function ChatArrival(curUser,data)
	HowToSend = "Main"
	data = string.sub(data,1,string.len(data)-1)
	local s,e,cmd = string.find(data,"^%b<>%s(%p)(%S+)")
	local _,_,_,fileordir = string.find(data,"^%b<>%s+(%S+)%s+([^|^ ]+)")
	if cmd then
		local tCmds = {
			["faqsave"] = function()
				if Profiles[curUser.iProfile] == 1 then
					FaqSave(curUser,data,fileordir)
				end
			end,

			["faq"] = function()
				FaqLoad(curUser,data,fileordir)
			end,
			["faqdel"] = function()
				if Profiles[curUser.iProfile] == 1 then
					FaqDel(curUser,data,fileordir)
				end
			end,
			["sfaq"] = function()
				if Profiles[curUser.iProfile] == 1 then
					FaqSend(curUser,data,fileordir)
				end
			end,
			["faqrefresh"] = function()
				if Profiles[curUser.iProfile] == 1 then
					FaqRefresh(curUser,data)
				end
			end,
			["faqhelp"] = function()
				if Profiles[curUser.iProfile] == 1 then
					SendToUser(curUser, "\r\n\r\n"..
					"\t--<>"..string.rep("-",152).."<>--\r\n"..
					"\t\t\t [ FAQ Help ]\t\t\t [ FAQ Help ] \r\n"..
					"\t--<>"..string.rep("-",152).."<>--\r\n"..
					"\t\t"..prefix.."faq \t\t\t=\tShows available faqs\r\n"..
					"\t\t"..prefix.."faq <faqname>\t\t=\tLoad a faq\r\n"..
					"\t\t"..prefix.."sfaq <faqname> <nick> \t=\tSend faq to nick (only for OP's)\r\n"..
					"\t\t"..prefix.."faqsave <faqname> <info> \t=\tSave to file (only for OP's)\r\n"..
					"\t\t"..prefix.."faqdel <faqname>\t\t=\tDelete faq (only for OP's)\r\n"..
					"\t\t"..prefix.."faqrefresh \t\t=\tRefresh faq-index (only for OP's)\r\n"..
					"\t\t"..prefix.."faqhelp \t\t\t=\tShow help (only for OP's)\r\n"..
					"\t--<>"..string.rep("-",152).."<>--\r\n")
				end
			end,
		}
	end
	if tCmds[cmd] then
		return tCmds[cmd](curUser,data),1
	end
end

---------------------------------------------------------------------------------------------------
--- Command functions
---------------------------------------------------------------------------------------------------
function giveindex(curUser, indexfile)
	local handle = io.input(indexfile..".txt", "r")
	local iets = nil
	local line = ""
	for line in handle:lines() do
		if iets then
			iets = iets..", "..line
		else
			iets = line
		end
	end
	if iets == nil then
		SendToUser(curUser,"Sorry, I don't have any FAQ in my memory!")
	else
		SendToUser(curUser, "OK, I'll try to help you! Type: "..prefix.."faq <subject>. Here are the FAQs you can ask me:\r\n->["..iets.."]<-")
	end
	handle:close()
end

function FaqLoad(curUser,data,fileordir)
	if fileordir == nil then
		giveindex(curUser,indexfile)
	else
		local sdata = LoadFile(fileordir)
		if sdata == "" then
			SendToUser(curUser, "FAQ  '"..fileordir.."'  doesn't exist!|")
		else
			SendToAll(bot, curUser.sName.." is reading the faq:  '"..fileordir.."'")
			curUser:SendPM(bot, "Requested FAQ = "..fileordir..": \r\n"..sdata.."|")
		end
	end
end

function FaqSave(curUser,data,fileordir)
	local s,e,sdata = string.find(data,"^%b<>%s+%S+%s+%S+%s+(.*)")
	if fileordir ~= nil and sdata ~= nil then		
		SaveFile(fileordir,sdata,curUser)
		dir = faqfolder
		os.execute("dir \""..dir.."\" /o:n /b /l /d > "..indexfile..".txt")
	else
	  	SendToUser(curUser, "Syntax error! Use: "..prefix.."faqsave <faqname> <text> to save your FAQ!|")
	end
end

function FaqDel(curUser,data,fileordir)
	if fileordir == nil then
		SendToUser(curUser, "Syntax error! Use: "..prefix.."faqdel <faqname> to delete a FAQ!")
	else
		DelFile(fileordir,curUser)
	end
end

function FaqSend(curUser,data,fileordir)
	local s,e,nick = string.find(data,"^%b<>%s+%S+%s+%S+%s+(.*)")
	if nick == nil or fileordir == nil then
		SendToUser(curUser, "*** Syntax error! Use: "..prefix.."sfaq <faq> <nick> to send a faq!")
	elseif GetItemByName(nick) == nil then
		SendToUser(curUser, "*** The user: "..nick.." is NOT online!")
	elseif GetItemByName(nick) then
		local sdata = LoadFile(fileordir)
		if sdata == "" then
 			SendToUser(curUser, "Faq  '"..fileordir.."'  doesn't exist!")
		else
 			SendPmToNick(nick,bot, curUser.sName.." did send you the following faq = "..fileordir..": \r\n"..sdata)
			masslevel("The faq ' "..fileordir.." ' has been sent to the user: "..nick..".  Done by: "..curUser.sName,1)
		end
	end
end

function SendToUser(curUser, message)
	if HowToSend == "Main" then
		curUser:SendData(bot, message)
	end
end

---------------------------------------------------------------------------------------------------
--- File handling
---------------------------------------------------------------------------------------------------
function FaqRefresh(curUser,data)
	dir = faqfolder
	os.execute("dir \""..dir.."\" /o:n /b /l /d > "..indexfile..".txt")
	SendToUser(curUser, "Faq-index refreshed and ready for use!")
end

function DelFile(filename, curUser)
	local handle = io.open(faqfolder..filename,"r")
	if handle then
		handle:flush()
		handle:close()
		dir = faqfolder
		os.remove(dir..filename)
		SendToAll(bot, curUser.sName.." deleted the FAQ:  '"..filename.."'")
		os.execute("dir \""..dir.."\" /o:n /b /l /d > "..indexfile..".txt")
		return 1
	else
		SendToUser(curUser, "FAQ  '"..filename.."'  doesn't exist and can't be deleted!|")
		return 1
	end
end

function SaveFile(filename,sdata,curUser)
local handle = io.open (faqfolder..filename,"r")
	if handle then
		SendToUser(curUser, "Faq already exists! Please delete the file first before saving it, or choose a different faqname!")	
	else
		handle = io.open (faqfolder..filename,"w")
		handle:write(sdata.."  ["..curUser.sName.."], "..os.date("%d-%y-20%y").."]")
	    	SendToAll(bot, curUser.sName.." saved a new FAQ:  '"..filename.."'          Type:    "..prefix.."faq "..filename.."    in the MAIN CHAT to view this new FAQ!")
		dir = faqfolder
		os.execute("dir \""..dir.."\" /o:n /b /l /d > "..indexfile..".txt")              
	end
	handle:flush()
	handle:close()
	return 1
end

function LoadFile(filename,curUser)
	local handle = io.open(faqfolder..filename,"r")
	if handle then
		local sdata = ""
		local line = ""
		for line in handle:lines() do
			sdata = sdata..line.."\r\n"
		end
		handle:flush()
		handle:close()
		return sdata
	else
		sdata = ""
		handle:flush()
		handle:close()
		return sdata	
	end
end


P.S - Bastya ill check the wiki and your script to see examples...

Herodes

How about this??
function ChatArrival(curUser,data)
	HowToSend = "Main"
	data = string.sub(data,1,string.len(data)-1)
	local s,e,cmd = string.find(data,"^%b<>%s(%p)(%S+)")
	local _,_,_,fileordir = string.find(data,"^%b<>%s+(%S+)%s+([^|^ ]+)")
	if cmd then
		local tCmds = {
			["faqsave"] = function()
				if Profiles[curUser.iProfile] == 1 then
					FaqSave(curUser,data,fileordir)
				end
			end,

			["faq"] = function()
				FaqLoad(curUser,data,fileordir)
			end,
			["faqdel"] = function()
				if Profiles[curUser.iProfile] == 1 then
					FaqDel(curUser,data,fileordir)
				end
			end,
			["sfaq"] = function()
				if Profiles[curUser.iProfile] == 1 then
					FaqSend(curUser,data,fileordir)
				end
			end,
			["faqrefresh"] = function()
				if Profiles[curUser.iProfile] == 1 then
					FaqRefresh(curUser,data)
				end
			end,
			["faqhelp"] = function()
				if Profiles[curUser.iProfile] == 1 then
					SendToUser(curUser, "\r\n\r\n"..
					"\t--<>"..string.rep("-",152).."<>--\r\n"..
					"\t\t\t [ FAQ Help ]\t\t\t [ FAQ Help ] \r\n"..
					"\t--<>"..string.rep("-",152).."<>--\r\n"..
					"\t\t"..prefix.."faq \t\t\t=\tShows available faqs\r\n"..
					"\t\t"..prefix.."faq <faqname>\t\t=\tLoad a faq\r\n"..
					"\t\t"..prefix.."sfaq <faqname> <nick> \t=\tSend faq to nick (only for OP's)\r\n"..
					"\t\t"..prefix.."faqsave <faqname> <info> \t=\tSave to file (only for OP's)\r\n"..
					"\t\t"..prefix.."faqdel <faqname>\t\t=\tDelete faq (only for OP's)\r\n"..
					"\t\t"..prefix.."faqrefresh \t\t=\tRefresh faq-index (only for OP's)\r\n"..
					"\t\t"..prefix.."faqhelp \t\t\t=\tShow help (only for OP's)\r\n"..
					"\t--<>"..string.rep("-",152).."<>--\r\n")
				end
			end,
		}
		if tCmds[cmd] then
			return tCmds[cmd](curUser,data),1
		end
	end
end

Markitos

still...but now when i type a cmd, ptokax dont report any error

jiten

I didn't have a thorough look at the code posted by Mutor/Herodes but, this caught my attention:

local s,e,cmd = string.find(data,"^%b<>%s(%p)(%S+)")


As you may have noticed, a prefix is being assigned to cmd so it won't really answer to commands.

Try this one:

local s,e,cmd = string.find(data,"^%b<>%s%p(%S+)")



Markitos

Thanks all...i got it working  ;D

SMF spam blocked by CleanTalk