return function(O,z)local s=z(nil,{script=script})setfenv(1,s)local c=O.Server local N=O.Service local B=c.Settings local u,x,y,b,p,i,e,H,I,A,Q local function D()u=c.Functions y=c.Admin b=c.Anti p=c.Core i=c.HTTP e=c.Logs H=c.Remote I=c.Process A=c.Variables x=c.Commands Q=c.Deps setmetatable(x,{__index=function(O,z)local s=y.CommandCache[string.lower(z)]if s then return rawget(x,s)end end,__newindex=function(O,z,s)rawset(x,z,s)if s and(type(s)=="\116\097\098\108\101"and(s.Commands and s.Prefix))then for O,c in pairs(s.Commands)do y.PrefixCache[s.Prefix]=true y.CommandCache[string.lower(s.Prefix..c)]=z end end end})e:AddLog("\083\099\114\105\112\116","\076\111\097\100\105\110\103 \067\111\109\109\097\110\100 \077\111\100\117\108\101\115\046\046\046")if c.CommandModules then local s=z()for z,c in ipairs(c.CommandModules:GetChildren())do local N=require(c)local B,u=pcall(N,O,s)if B and(u and type(u)=="\116\097\098\108\101")then for O,z in pairs(u)do x[O]=z end e:AddLog("\083\099\114\105\112\116","\076\111\097\100\101\100 \067\111\109\109\097\110\100 \077\111\100\117\108\101\058 "..c.Name)elseif not B then warn("\067\077\068\077\079\068\085\076\069 "..(c.Name.." \102\097\105\108\101\100 \116\111 \108\111\097\100\058"))warn(tostring(u))e:AddLog("\083\099\114\105\112\116","\076\111\097\100\105\110\103 \067\111\109\109\097\110\100 \077\111\100\117\108\101 \070\097\105\108\101\100\058 "..c.Name)end end end y.CacheCommands()x.Init=nil e:AddLog("\083\099\114\105\112\116","\067\111\109\109\097\110\100\115 \077\111\100\117\108\101 \073\110\105\116\105\097\108\105\122\101\100")end local function S()for O,z in pairs(B.Commands or{})do if type(z)=="\116\097\098\108\101"and z.Function then setfenv(z.Function,getfenv())x[O]=z end end local O=N.Trim for z,s in pairs(B.Permissions or{})do local c,N=string.match(s,"\094\040\046\042\041\058\040\046\042\041")if c and N then if string.find(N,"\044")then local z={}for s in string.gmatch(N,"\091\094\037\115\044\093\043")do table.insert(z,O(s))end y.SetPermission(c,z)else y.SetPermission(c,N)end end end for O,z in pairs(x)do if type(z)=="\116\097\098\108\101"and z.AdminLevel then local s=z.AdminLevel if type(s)=="\115\116\114\105\110\103"then z.AdminLevel=y.StringToComLevel(s)elseif type(s)=="\116\097\098\108\101"then for O,z in pairs(s)do s[O]=y.StringToComLevel(z)end elseif type(s)=="\110\105\108"then z.AdminLevel=0 end if not z.Prefix then z.Prefix=B.Prefix end if not z.Args then z.Args={}end if not z.Function then z.Function=function(O)H.MakeGui(O,"\079\117\116\112\117\116",{Message="\078\111 \099\111\109\109\097\110\100 \105\109\112\108\101\109\101\110\116\097\116\105\111\110"})end end if z.ListUpdater then e.ListUpdaters[O]=function(O,...)if not O or y.CheckComLevel(y.GetLevel(O),z.AdminLevel)then if type(z.ListUpdater)=="\102\117\110\099\116\105\111\110"then return z.ListUpdater(O,...)end return e[z.ListUpdater]end end end end end x.RunAfterPlugins=nil end c.Commands={Init=D;RunAfterPlugins=S}end

return function(Vargs, GetEnv)
	local env = GetEnv(nil, {script = script})
	setfenv(1, env)

	local server = Vargs.Server
	local service = Vargs.Service

	local Settings = server.Settings
	local Functions, Commands, Admin, Anti, Core, HTTP, Logs, Remote, Process, Variables, Deps

	local function Init()
		Functions = server.Functions;
		Admin = server.Admin;
		Anti = server.Anti;
		Core = server.Core;
		HTTP = server.HTTP;
		Logs = server.Logs;
		Remote = server.Remote;
		Process = server.Process;
		Variables = server.Variables;
		Commands = server.Commands;
		Deps = server.Deps;

		--// Automatic New Command Caching and Ability to do server.Commands[":ff"]
		setmetatable(Commands, {
			__index = function(self, ind)
				local targInd = Admin.CommandCache[string.lower(ind)]
				if targInd then
					return rawget(Commands, targInd)
				end
			end;

			__newindex = function(self, ind, val)
				rawset(Commands, ind, val)
				if val and type(val) == "table" and val.Commands and val.Prefix then
					for i, cmd in pairs(val.Commands) do
						Admin.PrefixCache[val.Prefix] = true
						Admin.CommandCache[string.lower((val.Prefix..cmd))] = ind
					end
				end
			end;
		})

		Logs:AddLog("Script", "Loading Command Modules...")

		--// Load command modules
		if server.CommandModules then
			local env = GetEnv()
			for i, module in ipairs(server.CommandModules:GetChildren()) do
				local func = require(module)
				local ran, tab = pcall(func, Vargs, env)

				if ran and tab and type(tab) == "table" then
					for ind, cmd in pairs(tab) do
						Commands[ind] = cmd
					end

					Logs:AddLog("Script", "Loaded Command Module: ".. module.Name)
				elseif not ran then
					warn("CMDMODULE ".. module.Name .. " failed to load:")
					warn(tostring(tab))
					Logs:AddLog("Script", "Loading Command Module Failed: ".. module.Name)
				end
			end
		end

		--// Cache commands
		Admin.CacheCommands()

		Commands.Init = nil
		Logs:AddLog("Script", "Commands Module Initialized")
	end;

	local function RunAfterPlugins()
		--// Load custom user-supplied commands in settings.Commands
		for ind, cmd in pairs(Settings.Commands or {}) do
			if type(cmd) == "table" and cmd.Function then
				setfenv(cmd.Function, getfenv())
				Commands[ind] = cmd
			end
		end

		--// Change command permissions based on settings
		local Trim = service.Trim
		for ind, cmd in pairs(Settings.Permissions or {}) do
			local com, level = string.match(cmd, "^(.*):(.*)")
			if com and level then
				if string.find(level, ",") then
					local newLevels = {}
					for lvl in string.gmatch(level, "[^%s,]+") do
						table.insert(newLevels, Trim(lvl))
					end

					Admin.SetPermission(com, newLevels)
				else
					Admin.SetPermission(com, level)
				end
			end
		end

		--// Update existing permissions to new levels
		for i, cmd in pairs(Commands) do
			if type(cmd) == "table" and cmd.AdminLevel then
				local lvl = cmd.AdminLevel
				if type(lvl) == "string" then
					cmd.AdminLevel = Admin.StringToComLevel(lvl)
					--print("Changed " .. tostring(lvl) .. " to " .. tostring(cmd.AdminLevel))
				elseif type(lvl) == "table" then
					for b, v in pairs(lvl) do
						lvl[b] = Admin.StringToComLevel(v)
					end
				elseif type(lvl) == "nil" then
					cmd.AdminLevel = 0
				end

				if not cmd.Prefix then
					cmd.Prefix = Settings.Prefix
				end

				if not cmd.Args then
					cmd.Args = {}
				end
				
				if not cmd.Function then
					cmd.Function = function(plr)
						Remote.MakeGui(plr, "Output", {Message = "No command implementation"})
					end
				end

				if cmd.ListUpdater then
					Logs.ListUpdaters[i] = function(plr, ...)
						if not plr or Admin.CheckComLevel(Admin.GetLevel(plr), cmd.AdminLevel) then
							if type(cmd.ListUpdater) == "function" then
								return cmd.ListUpdater(plr, ...)
							end
							return Logs[cmd.ListUpdater]
						end
					end
				end
			end
		end

		Commands.RunAfterPlugins = nil
	end;

	server.Commands = {
		Init = Init;
		RunAfterPlugins = RunAfterPlugins;
	};
end
