PtokaX forum

Development Section => HOW-TO's => Topic started by: Corayzon on 28 May, 2004, 07:35:12

Title: HOWTO :: Work with awsome editable commands
Post by: Corayzon on 28 May, 2004, 07:35:12
hey guys...i just wrote this the other day for the new cslave, and thought it fukin rocks!

this is a basic system that shows you how to make COMPLETLY! editable commands and how to call them in a very effective manner.

This script uses the method addCommand(sName, iLevel, sFunc, iArgCount, bReturn) to completly build the commands. This means that commands can be added by hub owners, or they can be returned if another script uses it(multi script support)

-- :: This is built in LUA 4.0
-------------------------------------------------------------------------------------
tCommands = {}
tPrefixs = {}
-------------------------------------------------------------------------------------
-- :: SETTING METHODS
-------------------------------------------------------------------------------------
function addPrefix(sPrefix)
if sPrefix ~= nil then
tPrefixs[sPrefix] = 1
end
end
-------------------------------------------------------------------------------------
function addCommand(sName, iLevel, sFunc, iArgCount, bReturn)
if sName ~= nil and iLevel ~= nil and sFunc and iArgCount ~= nil then
tCommands[sName] = {[1] = iLevel, [2] = sInfo, [3] = sFunc, [4] = iArgCount, [5] = bReturn or nil}
else
assert(nil, "Error in settings file 'commands.txt' - addCommand()")
end
end
-------------------------------------------------------------------------------------
-- :: EDITABLE METHODS
-------------------------------------------------------------------------------------
-- :: addPrefix(sPrefix)
addPrefix("+")
-------------------------------------------------------------------------------------
-- :: addCommand(sName, iLevel, sFunc, iArgCount, bReturn)
addCommand("rules", 1, "tUser:SendData(\"dont share porn!\")", 1)
addCommand("kick", 3, "tUser:SendData((arg[1] or \"none\") .. \" - \" .. (arg[2] or \"none\") .. \" - \" .. (arg[3] or \"none\"))", 3, 1)
addCommand("say", 3, "SendToAll(arg[2], arg[3])", 3, 1)
-------------------------------------------------------------------------------------
-- :: SCRIPT START
-------------------------------------------------------------------------------------
function Main()
frmHub:EnableSearchData(1)
frmHub:EnableFullData(1)
if bHideBot == no then frmHub:RegBot(sBot) end
SetTimer(dInterval)
end

function DataArrival(tUser, sData)
if strsub(sData, 1, strlen(tUser.sName) + 2) == "<" .. tUser.sName .. ">" then
local _,_, sMessage = strfind(sData, "%b<>%s+(.*)|")
if tPrefixs[strsub(sMessage, 1, 1)] == 1 then
local sCmd = getCommand(sMessage)
if tCommands[sCmd] then
local tArgs = buildArgTable(sMessage, tCommands[sCmd][4])
setglobal("tUser", tUser)
setglobal("arg", tArgs)
dostring(tCommands[sCmd][3], "addCommand Error!")
setglobal("tUser", nil)
setglobal("arg", nil)
if tCommands[sCmd][5] then return 1 end
end
end
end
end

function getCommand(sMessage)
sMessage = "|" .. sMessage
local _,_, sCmd = strfind(sMessage, "|(%S+)")
sCmd = strsub(sCmd, 2, strlen(sCmd))
return sCmd
end

function buildArgTable(sArgs, iArgCount)
sArgs = "|" .. sArgs .. "|"
local tOut, iCount = {}, 0
while 1 do
if sArgs ~= nil then
iCount = iCount + 1
local _, sArg
if iCount == iArgCount then
_,_, sArg = strfind(sArgs, "|(.*)|")
else
_,_, sArg = strfind(sArgs, "|(%S+)")
end

if sArg ~= nil then
if strsub(sArg, strlen(sArg), strlen(sArg)) == "|" then sArg = strsub(sArg, 1, strlen(sArg) - 1) end
tOut[iCount] = sArg
sArgs = "|" .. strsub(sArgs, strlen(sArg) + 3, strlen(sArgs))
else
return tOut
end
else
return tOut
end
end
end
Title:
Post by: Corayzon on 01 June, 2004, 06:21:50
allright...there is some issues in the above posts script, but i have sorted it all out thankz to the help of a few ppl =]...

will post a new copy in the next few days