[Help]Call/Execute commands from a table
 

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

[Help]Call/Execute commands from a table

Started by Snooze, 02 March, 2005, 10:54:36

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Snooze

Hey Guys ..

My mission is to put all commands in a table like this:

tCmds = {
	["Master"] = {
		["test"] = "DoTest",
		["test2"] = "DoTest2",
		["test3"] = "DoTest3",
		}
	}
}

where ["Master"] is GetProfileName(user.iProfile) and ["test"] is the command written - "DoTest" is the command executed..

The problem is "DoTest" is returned as tring :(

Any help will be appreciated..

Snooze


Jaras

here's one of my old bots, which isn't exactly what you ask for, "very chaotic" =], but may help.

bastya_elvtars

Dunno if this works, but this is how I would do it.

tCmds = {
	["Master"] = {
		["test"] = {DoTest, {param1,param2,param3}} -- an array with the function and a nested array with function params - f(a,b,c)
		["test2"] = {DoTest2,{}}
		["test3"] = {DoTest3,{param}}
		}
	}
}

-- and you just do:

return tCmds["Master"]["test"][1](unpack(tCmds["Master"]["test"][2]))
Everything could have been anything else and it would have just as much meaning.

Snooze

Thanks bastya_elvtars!

It looks like what i need, though im having a hard time creating a working example.. Could i get you to create one small example for me ?

Snooze

bastya_elvtars

In order 2 make others learn, I post the result here. No warranties! :)

Bot = "Bot"

function ChatArrival(user, data)	
	if string.sub(data, 1, 1) == "<" or (string.sub(data,1,5+string.len(Bot))=="$To: "..Bot) or (string.sub(data,1,5+string.len(SuChat))=="$To: "..SuChat) or (string.sub(data,1,5+string.len(OpChat))=="$To: "..OpChat) then
		local data = string.sub(data,1,string.len(data)-1) 
		s,e,prefix,cmd,cmdData = string.find(data,"%b<> (%S)(%S+)(.*)")
		if tPrefix[prefix] ~= nil then
		ParseCmds(user,data,cmd)
			-- this must go to ToArrival, and Bot has to be string.find-ed IMHO
			-- if string.sub(data,1,5+string.len(Bot))=="$To: "..Bot then SendPmToNick(sUser["sUser"],Bot, doReturn) clearArs() elseif string.sub(data,1,5+string.len(SuChat))=="$To: "..SuChat then SendPmToNick(sUser["sUser"],SuChat, doReturn) clearArs() elseif string.sub(data,1,5+string.len(OpChat))=="$To: "..OpChat then SendPmToOps(OpChat, doReturn) clearArs() else user:SendData(doReturn) clearArs() return 1 end
		end
	end
end


function DoTest(user,data)
	SendToAll(Bot, "hey hey hey")
end

function ParseCmds(user,data,cmd)
	local SKCMD = {
					["Owner"] = {
								["test"] = {DoTest,{user,data}}
							    }
				}
	local tmpArr=SKCMD[GetProfileName(user.iProfile)][cmd]
	if not tmpArr then
	user:SendData(Bot, "*** Error! - [ "..cmd.." ] is not a valid command. Type "..SKCONF["BotPrefix"].."help to get a list of available commands.")
	else
		return tmpArr[1](unpack(tmpArr[2]))
	end
end
Everything could have been anything else and it would have just as much meaning.

SMF spam blocked by CleanTalk