View Single Post
07-18-21, 01:41 AM   #1
Zax
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 147
Testing slash-command before execution?

Hello,

I use the following function to execute slash-commands, or commands:

Code:
-- .executeCmd("/sit", true)
-- .executeCmd("print(GetServerTime()), false)

function .executeCmd(cmdStr, isMacroBoolean)
	local cmdStr = cmdStr or ""
	local isMacroBoolean = isMacroBoolean or true
	local tbl = {strsplit("\n", cmdStr)}
	for i, strLine in ipairs(tbl) do
		strLine = strtrim(strLine)
		if (strLine ~= "") then
			if (isMacroBoolean)) then
				--------------------------- macro ("/" needed)
				if (string.sub(strLine, 1, 1) ~= "/") then strLine = "/"..strLine end
				DEFAULT_CHAT_FRAME.editBox:SetText(strLine)
				ChatEdit_SendText(DEFAULT_CHAT_FRAME.editBox, 0)
			else
				--------------------------- command ("/" not valid)
				if (string.sub(strLine, 1, 1) == "/") then strLine = string.sub(strLine, 2) end -- delete char 1
				RunScript(strLine)
			end
		end
	end
end
And I would like to test the passed command because if the command is not valid, it will display a lua error, and it can be confusing. I tried to play with pcall() without success.

Thank you for your help.
  Reply With Quote