one of the commands doesn't work in my script
 

one of the commands doesn't work in my script

Started by blackwings, 14 June, 2005, 08:28:37

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

blackwings

I have continued to code on a cmd protection script a made more than a month ago. I noticed a bug that I cant see why it gets like that.
the protection for the !delreguser user doesn't work, I get this error message =
[08:06] Syntax C:\ptokax\scripts\CommandProtection.lua:193: attempt to index local `sVictim' (a nil value)
here is the code =
-- Command Protection & Customizing 2.5 by blackwings - relesed 26/04-05
--- 28/04-05 - Tested on Ptokax 17.03
-- ADDED: !deluser command added
-- ADDED: a table for Ops that should be allowed to use the commansd on Ops
-- CHANGED/ADDED:message to a user that gets kicked + changed the TempBan().
-- CHANGED: changed the action from !drop to the ptokax default.
--- 29/04-05 - Tested on Ptokax 17.03
-- ADDED: valid prefix of the command check = !kick, +kick, /kick is allowed
-- ADDED: !nickban & !banip command
-- ADDED: table for protected profiles
-- ADDED: a counter for kicks & drops, which by default bans after 4 kicks/drops.
--- 01/05-05 - Tested on Ptokax 17.03
-- CHANGED: Name of the script to = Command Protection & Customizing
-- CHANGED: Optimized the code a little.
-- ADDED: on/off switch for each command for allowed Op username to use command
-- CHANGED: All the messages.
--- 14/06-05 - Tested on Ptokax 17.09
-- ADDED: logging for each command

sBot = "#CMDprotect" 

tPrefix = frmHub:GetPrefixes()

-- How many times a user is allowed to be kicked/dropped, 0 = disabled.
theBan = 4

-- Put those operatos username that should be allowed to use these commands
-- 1 is to allow a certain commands for a Ops username, and the order is =
-- kick,ban,nickban,banip,delreguser,drop
theAllowed = {
["opname1"] = {1,1,1,1,1,1},
["opname1"] = {1,1,1,1,1,1},
}

-- Profiles that should be protected
tProfiles = {
	[0] = 1,
	[1] = 1,
	[2] = 1,
}

tProfiles2 = {
	[0] = 1,
	[1] = 1,
}

function ChatArrival(user, data)
	return CmdProte(user,data)
end
function ToArrival(user,data)
	return CmdProte(user,data)
end

function CmdProte(user, data)
	local s,e,Cmd, victim, Reason = string.find(data, "%b<>%s+(%S+)%s*(%S*)%s*(%S*).*|")
	local sCounter = 0
	if IfValidCMD(Cmd, "kick") and user.bOperator then
		local sVictim = GetItemByName(victim)
		if Reason == "" then
			user:SendData(sBot, "You need to type a reason for the kick")
		else      
			if not tProfiles[sVictim.iProfile] then	 
				sCounter = sCounter + 1
				tempBanLeft = theBan - sCounter
				if sCounter < theBan then
					sVictim:SendPM(sBot, "You have been kicked! Reason: "..Reason)
					sVictim:SendPM(sBot, "Number of kicks left before permban: <"..tempBanLeft..">")
					sVictim:TempBan()
					message = "<"..user.sName..">("..user.sIP..") - kicked user <"..sVictim.sName..">("..sVictim.sIP..") Reason: "..Reason
					SendPmToOps(sBot, message)
					cFilename = "kicklog"
					StoreLog(message,cFilename)
				elseif sCounter == theBan and theBan ~= 0 then
					sVictim:SendPM(sBot, "You are Permbanned! Reason: "..Reason)
					sCounter = 0
					sVictim:Ban()
					message = "<"..user.sName..">("..user.sIP..") - banned user <"..sVictim.sName..">("..sVictim.sIP..") Reason: "..Reason.." - [after 4x kicks]"
					SendPmToOps(sBot, message)
					cFilename = "banlog"
					StoreLog(message,cFilename)
				end
			else
				if not theAllowed[user.sName] then
					user:SendPM(sBot, "You can't kick users of this profile")
					return 1
				else
					if theAllowed[user.sName][1]==1 then
						sVictim:SendPM(sBot, "You have been kicked! Reason: "..Reason)
						sVictim:TempBan()
						message = "<"..user.sName..">("..user.sIP..") - kicked user <"..sVictim.sName..">("..sVictim.sIP..") Reason: "..Reason
						SendPmToOps(sBot, message)
						cFilename = "kicklog"
						StoreLog(message,cFilename)						
					else
						user:SendPM(sBot, "You can't kick users of this profile")
						return 1
					end
				end
			end
		end
		return 1
	elseif IfValidCMD(Cmd, "ban") and user.bOperator then
		local sVictim = GetItemByName(victim)
			if not tProfiles[sVictim.iProfile] then
				sVictim:SendPM(sBot, "You were killed")
				Ban(sVictim.sIP)
				NickBan(sVictim.sName)
				message = "<"..user.sName..">("..user.sIP..") - banned user <"..sVictim.sName..">("..sVictim.sIP..")"
				SendPmToOps(sBot, message)
				cFilename = "banlog"
				StoreLog(message,cFilename)
			else
				if not theAllowed[user.sName] then
					user:SendPM(sBot, "You can't permban users of this profile")
					return 1
				else
					if theAllowed[user.sName][2]==1 then
						sVictim:SendPM(sBot, "You were killed")
						Ban(sVictim.sIP)
						NickBan(sVictim.sName)
						message = "<"..user.sName..">("..user.sIP..") - banned user <"..sVictim.sName..">("..sVictim.sIP..")"
						SendPmToOps(sBot, message)
						cFilename = "banlog"
						StoreLog(message,cFilename)
					else
						user:SendPM(sBot, "You can't permban users of this profile")
						return 1
					end
				end
			end
		return 1
	elseif IfValidCMD(Cmd, "nickban") and user.bOperator then
		local sVictim = GetItemByName(victim)
			if not tProfiles[sVictim.iProfile] then
				message = "<"..user.sName..">("..user.sIP..") - nickbanned user <"..sVictim.sName..">("..sVictim.sIP..")"
				SendPmToOps(sBot, message)
				cFilename = "banlog"
				StoreLog(message,cFilename)			
				return nil
			else
				if not theAllowed[user.sName] then
					user:SendPM(sBot, "You can't nickban users of this profile")
					return 1
				else
					if theAllowed[user.sName][3]==1 then
						message = "<"..user.sName..">("..user.sIP..") - nickbanned user <"..sVictim.sName..">("..sVictim.sIP..")"
						SendPmToOps(sBot, message)
						cFilename = "banlog"
						StoreLog(message,cFilename)
						return nil
					else
						user:SendPM(sBot, "You can't nickban users of this profile")
						return 1
					end
				end
			end
		return 1
	elseif IfValidCMD(Cmd, "banip") and user.bOperator then
		local sVictim = GetItemByName(victim)
			if not tProfiles[sVictim.iProfile] then
				message = "<"..user.sName..">("..user.sIP..") - ipbanned user <"..sVictim.sName..">("..sVictim.sIP..")"
				SendPmToOps(sBot, message)
				cFilename = "banlog"
				StoreLog(message,cFilename)
				return nil
			else
				if not theAllowed[user.sName] then
					user:SendPM(sBot, "You can't ban users IPs of this profile")
					return 1
				else
					if theAllowed[user.sName][4]==1 then
						message = "<"..user.sName..">("..user.sIP..") - ipbanned user <"..sVictim.sName..">("..sVictim.sIP..")"
						SendPmToOps(sBot, message)
						cFilename = "banlog"
						StoreLog(message,cFilename)					
						return nil
					else
						user:SendPM(sBot, "You can't ban users IPs of this profile")
						return 1
					end
				end
			end
		return 1
	elseif IfValidCMD(Cmd, "delreguser") and user.bOperator then
		local sVictim = GetItemByName(victim)
			if not tProfiles2[sVictim.iProfile] then
				message = "<"..user.sName..">("..user.sIP..") - deleted user <"..sVictim.sName..">("..sVictim.sIP..")"
				SendPmToOps(sBot, message)
				cFilename = "deluser"
				StoreLog(message,cFilename)
				return nil
			else
				if not theAllowed[user.sName] then
					user:SendPM(sBot, "You can't delete users accounts of this profile")
					return 1
				else
					if theAllowed[user.sName][5]==1 then
						message = "<"..user.sName..">("..user.sIP..") - deleted user <"..sVictim.sName..">("..sVictim.sIP..")"
						SendPmToOps(sBot, message)
						cFilename = "deluser"
						StoreLog(message,cFilename)
						return nil
					else
						user:SendPM(sBot, "You can't delete users accounts of this profile")
						return 1
					end
				end
			end
		return 1
	elseif IfValidCMD(Cmd, "drop") and user.bOperator then
		local sVictim = GetItemByName(victim)
			if not tProfiles[sVictim.iProfile] then
				sCounter = sCounter + 1
				if sCounter < theBan then
					message = "<"..user.sName..">("..user.sIP..") - dropped user <"..sVictim.sName..">("..sVictim.sIP..")"
					SendPmToOps(sBot, message)
					cFilename = "droplog"
					StoreLog(message,cFilename)
					return nil
				elseif sCounter == theBan and theBan ~= 0 then
					sVictim:Ban()
				end
			else
				if not theAllowed[user.sName] then
					user:SendPM(sBot, "You can't kick/drop users of this profile")
					return 1
				else
					if theAllowed[user.sName][6]==1 then
						message = "<"..user.sName..">("..user.sIP..") - dropped user <"..sVictim.sName..">("..sVictim.sIP..")"
						SendPmToOps(sBot, message)
						cFilename = "droplog"
						StoreLog(message,cFilename)					
						return nil
					else
						user:SendPM(sBot, "You can't kick/drop users of this profile")
						return 1
					end
				end
			end
		return 1
	end
end

function IfValidCMD(sIsCMD, sWhatCMD)
	for sIndex in tPrefix do
		if sIsCMD == (tPrefix[sIndex]..sWhatCMD) then
			return 1
		end
	end
end

function StoreLog(message,cFilename)
   local cDate = string.gsub(os.date("%x"), "%D", "-")
   local fFile = io.open("logs/"..cFilename..".txt", "a+")
   fFile:write("["..cDate.."]"..message.."\n")
   fFile:close()
end


jiten

You may add the red lines so that it only deletes online users (that's why that error happens):
[COLOR=red]if GetItemByName(victim) then[/COLOR]
			local sVictim = GetItemByName(victim)
			if not tProfiles2[sVictim.iProfile] then
				message = "<"..user.sName..">("..user.sIP..") - deleted user <"..sVictim.sName..">("..sVictim.sIP..")"
				SendPmToOps(sBot, message)
				cFilename = "deluser"
				StoreLog(message,cFilename)
				return nil
			else
				if not theAllowed[user.sName] then
					user:SendPM(sBot, "You can't delete users accounts of this profile")
					return 1
				else
					if theAllowed[user.sName][5]==1 then
						message = "<"..user.sName..">("..user.sIP..") - deleted user <"..sVictim.sName..">("..sVictim.sIP..")"
						SendPmToOps(sBot, message)
						cFilename = "deluser"
						StoreLog(message,cFilename)
						return nil
					else
						user:SendPM(sBot, "You can't delete users accounts of this profile")
						return 1
					end
				end
			end
		[COLOR=red]end[/COLOR]

Cheers

blackwings

it didn't work jiten :( nothing happens when you use !delreguser, not even a error message in protkax:(


Dessamator

hmm, well at least its a good sign, btw a hint for ur script, its way too repetitive, its harder to find bugs like that , take a look at this  :
if not tProfiles[sVictim.iProfile] then
message = "<"..user.sName..">("..user.sIP..") - nickbanned user <"..sVictim.sName..">("..sVictim.sIP..")"
SendPmToOps(sBot, message)
cFilename = "banlog"
StoreLog(message,cFilename)
return nil


this too :

if theAllowed[user.sName][4]==1 then
message = "<"..user.sName..">("..user.sIP..") - ipbanned user <"..sVictim.sName..">("..sVictim.sIP..")"
SendPmToOps(sBot, message)
cFilename = "banlog"
StoreLog(message,cFilename)
return nil
else
user:SendPM(sBot, "You can't ban users IPs of this profile")
return 1
end

 
u use that on every command, instead u could use a function

btw, the script doesnt give any error messages probably because ur missing an else,

if user is online then --dostuff  else --do other stuff
Ignorance is Bliss.

jiten

QuoteOriginally posted by blackwings
it didn't work jiten :( nothing happens when you use !delreguser, not even a error message in protkax:(
Well, firstly !delreguser is an inbuild PtokaX command and that code I copied should work if the "victim" is online. If he is offline, nothing happens, just like you posted.

Cheers

blackwings

I have rewritten my script(still aint finished), but I have a problem and I need help please.

The command !banip doesn't work. It just ignore my code and use ptokax default option for it,
I can't see why this happends with just !banip, the error message doesn't give me any clue on why it happens,
but here is the error message anyway =
[21:24] Syntax C:\ptokax03321\scripts\cmdprotect.lua:53: attempt to index global `sVictim' (a nil value)
Here is the script =
-- Command Protection & Customizing 3.0
-- by blackwings
-- some of the code taken from OP Command Protection v1.1 by Fangs404
-- Tested on PtokaX 0.3.3.21

sBot = "CommandProtect"
tPrefixes = frmHub:GetPrefixes()

theAllowed = {
["OPName01"] = {1,1,1,1,1,1},
["OPName02"] = {1,1,1,1,1,1},
["OPName03"] = {1,1,1,1,1,1},
}

tProfiles = {
	[0] = 1,
	[1] = 1,
	[2] = 1,
}

tProfiles2 = {
	[0] = 1,
	[1] = 1,
}

cCmd = ""
cFilename = ""
cMsgPart = ""

function ChatArrival(user, sData)
	local s,e,sCmd, victim, Reason = string.find(sData, "%b<>%s+(%S+)%s*(%S*)%s*(%S*).*|")
	sVictim = GetItemByName(victim)
	local tCmds = {
		{ "kick", "kicklog", "kicked"},
		{ "ban", "banlog", "PERMBANNED"},
		{ "nickban", "banlog", "nickbanned"}, 
		{ "banip", "banlog", "ipbanned"},
		{ "delreguser", "deluserlog", "deleted"},
		{ "drop", "droplog", "dropped"},
	}

	local function isValidCmd(sCurCmd, sNeedCmd)
		for index in tPrefixes do
			if ( sCurCmd == (tPrefixes[index]..sNeedCmd) ) then return 1;end;
		end
	end

	for i,v in tCmds do
		if isValidCmd ( sCmd, v[1] ) then
			cCmd = v[1]
			cFilename = v[2]
			cMsgPart = v[3]
			if tProfiles[sVictim.iProfile] and theAllowed[user.sName] then
				return TakeAction(user, sVictim)
			elseif tProfiles[sVictim.iProfile] and not theAllowed[user.sName] then
				user:SendPM(sBot, "You can't "..v[1].." users of this profile")
			else
				return TakeAction(user, sVictim)
			end	
			return 1;
		end
	end
end

function TakeAction(user, sVictim)
	if cCmd == "kick" then
		if not tProfiles[sVictim.iProfile] then
			return MsgThenLog(user, sVictim)
		else
			if theAllowed[user.sName][1]==1 then
				return MsgThenLog(user, sVictim)
			else
				user:SendPM(sBot, "You can't kick users of this profile")
				return 1
			end
		end
	elseif cCmd == "ban" then
		if not tProfiles[sVictim.iProfile] then
			sVictim:SendPM(sBot, "You have been PERMBANNED!")
			sVictim:Ban()
			return MsgThenLog(user, sVictim)
		else
			if theAllowed[user.sName][2]==1 then
				sVictim:SendPM(sBot, "You have been PERMBANNED!")
				sVictim:Ban()
				return MsgThenLog(user, sVictim)
			else
				user:SendPM(sBot, "You can't ban users of this profile")
				return 1
			end
		end
	elseif cCmd == "nickban" then
		if not tProfiles[sVictim.iProfile] then
				sVictim:SendPM(sBot, "Your username has been PERMBANNED!")
				return MsgThenLog(user, sVictim)
		else
			if theAllowed[user.sName][3]==1 then
				sVictim:SendPM(sBot, "Your username has been PERMBANNED!")
				return MsgThenLog(user, sVictim)
				--return nil
			else
				user:SendPM(sBot, "You can't nickban users of this profile")
				return 1
			end
		end
	elseif cCmd == "banip" then
		if not tProfiles[sVictim.iProfile] then
				sVictim:SendPM(sBot, "Your IP has been PERMBANNED!")
				return MsgThenLog(user, sVictim)
		else
			if theAllowed[user.sName][4]==1 then
				sVictim:SendPM(sBot, "Your IP has been PERMBANNED!")
				return MsgThenLog(user, sVictim)
				--return nil
			else
				user:SendPM(sBot, "You can't ipban users of this profile")
				return 1
			end
		end
	elseif cCmd == "delreguser" then
		if not tProfiles[sVictim.iProfile] then
				return MsgThenLog(user, sVictim)
		else
			if theAllowed[user.sName][5]==1 then
				return MsgThenLog(user, sVictim)
				--return nil
			else
				user:SendPM(sBot, "You can't delete users accounts of this profile")
				return 1
			end
		end
	elseif cCmd == "drop" then
		if not tProfiles[sVictim.iProfile] then
				return MsgThenLog(user, sVictim)
		else
			if theAllowed[user.sName][6]==1 then
				return MsgThenLog(user, sVictim)
				--return nil
			else
				user:SendPM(sBot, "You can't drop users of this profile")
				return 1
			end
		end
	end
end

function MsgThenLog(user, sVictim)
	if cCmd == "kick" then
		sVictim:SendPM(sBot, "You have been kicked! Reason: ")
		sVictim:SendPM(sBot, "Number of kicks left before permban: <")
		sVictim:TempBan()
	elseif cCmd == "banip" then
		SendToAll(Bot, "ipban mupp")
		sVictim:Disconnect()
	end
	message = "<"..user.sName..">("..user.sIP..") - "..cMsgPart.." user <"..sVictim.sName..">("..sVictim.sIP..")"
	SendPmToOps(sBot, message)
	StoreLog(message)
end

function StoreLog(message)
   local cDate = string.gsub(os.date("%x"), "%D", "-")
   local fFile = io.open("logs/"..cFilename..".txt", "a+")
   fFile:write("["..cDate.."]"..message.."\n")
   fFile:close()
end


Dessamator

#6
well, u just try to find the svictim, first of all, u need a check, to see if its nil or not,

Another thing, you are using sVictim as a global variable, that could what is causing that hassle.
Ignorance is Bliss.

blackwings

ptokax doesn't run its default !banip anymore, which is good, but I still have a problem with sVictim is nil.  
I have made as you said Dessamator, local varibles instead off global. its really wierd why !banip doesn't work in my script.
Here is the new code anyway =
-- Command Protection & Customizing 3.0
-- by blackwings
-- some of the code taken from OP Command Protection v1.1 by Fangs404
-- Tested on PtokaX 0.3.3.21

sBot = "#CMDprotect"
tPrefixes = frmHub:GetPrefixes()

theAllowed = {
["OPName01"] = {1,1,1,1,1,1},
["OPName02"] = {1,1,1,1,1,1},
["OPName03"] = {1,1,1,1,1,1},
}

tProfiles = {
	[0] = 1,
	[1] = 1,
	[2] = 1,
}

tProfiles2 = {
	[0] = 1,
	[1] = 1,
}

cCmd = ""
cFilename = ""
cMsgPart = ""

function ChatArrival(user, sData)
	local s,e,sCmd, victim, Reason = string.find(sData, "%b<>%s+(%S+)%s*(%S*)%s*(%S*).*|")
	local tCmds = {
		{ "kick", "kicklog", "kicked"},
		{ "ban", "banlog", "PERMBANNED"},
		{ "nickban", "banlog", "nickbanned"}, 
		{ "banip", "banlog", "ipbanned"},
		{ "delreguser", "deluserlog", "deleted"},
		{ "drop", "droplog", "dropped"},
	}

	local function isValidCmd(sCurCmd, sNeedCmd)
		for index in tPrefixes do
			if ( sCurCmd == (tPrefixes[index]..sNeedCmd) ) then return 1;end;
		end
	end

	for i,v in tCmds do
		if isValidCmd ( sCmd, v[1] ) then
			cCmd = v[1]
			cFilename = v[2]
			cMsgPart = v[3]
			local sVictim = GetItemByName(victim)
			if sVictim~=nil then
				if tProfiles[sVictim.iProfile] and theAllowed[user.sName] then
					return TakeAction(user, victim)
				elseif tProfiles[sVictim.iProfile] and not theAllowed[user.sName] then
					user:SendPM(sBot, "You can't "..v[1].." users of this profile")
				else
					return TakeAction(user, victim)
				end
			else
				user:SendData(Bot, "username/IP is nil")
			end
			return 1;
		end
	end
end

function TakeAction(user, victim)
	local cVictim = GetItemByName(victim)
	if cCmd == "kick" then
		if not tProfiles[cVictim.iProfile] then
			return MsgThenLog(user, cVictim)
		else
			if theAllowed[user.sName][1]==1 then
				return MsgThenLog(user, cVictim)
			else
				user:SendPM(sBot, "You can't kick users of this profile")
				return 1
			end
		end
	elseif cCmd == "ban" then
		if not tProfiles[cVictim.iProfile] then
			cVictim:SendPM(sBot, "You have been PERMBANNED!")
			cVictim:Ban()
			return MsgThenLog(user, cVictim)
		else
			if theAllowed[user.sName][2]==1 then
				cVictim:SendPM(sBot, "You have been PERMBANNED!")
				cVictim:Ban()
				return MsgThenLog(user, cVictim)
			else
				user:SendPM(sBot, "You can't ban users of this profile")
				return 1
			end
		end
	elseif cCmd == "nickban" then
		if not tProfiles[cVictim.iProfile] then
				cVictim:SendPM(sBot, "Your username has been PERMBANNED!")
				return MsgThenLog(user, cVictim)
		else
			if theAllowed[user.sName][3]==1 then
				cVictim:SendPM(sBot, "Your username has been PERMBANNED!")
				return MsgThenLog(user, cVictim)
				--return nil
			else
				user:SendPM(sBot, "You can't nickban users of this profile")
				return 1
			end
		end
	elseif cCmd == "banip" then
		local bVictim = GetItemByName(victim)
		if not tProfiles[bVictim.iProfile] then
				bVictim:SendPM(sBot, "Your IP has been PERMBANNED!")
				return MsgThenLog(user, bVictim)
		else
			if theAllowed[user.sName][4]==1 then
				bVictim:SendPM(sBot, "Your IP has been PERMBANNED!")
				return MsgThenLog(user, bVictim)
				--return nil
			else
				user:SendPM(sBot, "You can't ipban users of this profile")
				return 1
			end
		end
	elseif cCmd == "delreguser" then
		if not tProfiles[cVictim.iProfile] then
				return MsgThenLog(user, cVictim)
		else
			if theAllowed[user.sName][5]==1 then
				return MsgThenLog(user, cVictim)
				--return nil
			else
				user:SendPM(sBot, "You can't delete users accounts of this profile")
				return 1
			end
		end
	elseif cCmd == "drop" then
		if not tProfiles[cVictim.iProfile] then
				return MsgThenLog(user, cVictim)
		else
			if theAllowed[user.sName][6]==1 then
				return MsgThenLog(user, cVictim)
				--return nil
			else
				user:SendPM(sBot, "You can't drop users of this profile")
				return 1
			end
		end
	end
end

function MsgThenLog(user, cVictim,bVictim)
	if cCmd == "kick" then
		cVictim:SendPM(sBot, "You have been kicked! Reason: ")
		cVictim:SendPM(sBot, "Number of kicks left before permban: <")
		cVictim:TempBan()
	elseif cCmd == "banip" then
		SendToAll(Bot, "ipban mupp")
		bVictim:Disconnect()
	end
	message = "<"..user.sName..">("..user.sIP..") - "..cMsgPart.." user <"..cVictim.sName..">("..cVictim.sIP..")"
	SendPmToOps(sBot, message)
	StoreLog(message)
end

function StoreLog(message)
   local cDate = string.gsub(os.date("%x"), "%D", "-")
   local fFile = io.open("logs/"..cFilename..".txt", "a+")
   fFile:write("["..cDate.."]"..message.."\n")
   fFile:close()
end


Dessamator

Indeed, Mutor, that was my point, but either way, i suggest using
if user.bConnected then -- bConnected				- User is added in hub (visible for other users) and added is after NewUser(Op)Connected.


instead of using a variable .
Ignorance is Bliss.

blackwings

#9
QuoteOriginally posted by Dessamator
Indeed, Mutor, that was my point, but either way, i suggest using
if user.bConnected then -- bConnected				- User is added in hub (visible for other users) and added is after NewUser(Op)Connected.


instead of using a variable .
no, user is online. But either way, banip should work no matter if user is online or not.

Hmm, it could also be one of those bugs that is in the end of the script/function,
but shows the error in the beginning of the script/function.

made a few changes to see if Im lucky and the bug disappeared, but it didn't :P =
-- Command Protection & Customizing 3.0
-- by blackwings
-- some of the code taken from OP Command Protection v1.1 by Fangs404
-- Tested on PtokaX 0.3.3.21

sBot = "#CMDprotect"
tPrefixes = frmHub:GetPrefixes()

theAllowed = {
["-kodachin-"] = {1,1,1,1,1,1},
["koda"] = {1,1,1,1,0,1},
["Kairou"] = {1,1,1,1,1,1},
}

tProfiles = {
	[0] = 1,
	[1] = 1,
	[2] = 1,
}

tProfiles2 = {
	[0] = 1,
	[1] = 1,
}

cCmd = ""
cFilename = ""
cMsgPart = ""

function ChatArrival(user, sData)
	local s,e,sCmd, victim, Reason = string.find(sData, "^%b<>%s*(%S*)%s*(%S*)%s*(.*)|")
	local tCmds = {
		["kick"]={"kicklog","kicked"},
		["ban"]={"banlog","PERMBANNED"},
		["nickban"]={"banlog","nickbanned"}, 
		["banip"]={"banlog","ipbanned"},
		["delreguser"]={"deluserlog","deleted"},
		["drop"]={"droplog","dropped"},
	}
	local function isValidCmd(sCurCmd, sNeedCmd)
		for index in tPrefixes do
			if sCurCmd == (tPrefixes[index]..sNeedCmd) then 
				return 1
			end
		end
	end
	for i,v in tCmds do
		if isValidCmd ( sCmd, i ) then
			cCmd = i
			cFilename = v[1]
			cMsgPart = v[2]
			local sVictim = GetItemByName(victim)
			if sVictim~=nil then
				if tProfiles[sVictim.iProfile] and theAllowed[user.sName] then
					return TakeAction(user, victim)
				elseif tProfiles[sVictim.iProfile] and not theAllowed[user.sName] then
					user:SendPM(sBot, "You can't "..i.." users of this profile")
				else
					return TakeAction(user, victim)
				end
			else
				user:SendData(sBot, "username/IP is nil")
			end
			return 1;
		end
	end
end

function TakeAction(user, victim)
	local cVictim = GetItemByName(victim)
	if cCmd == "kick" then
		if not tProfiles[cVictim.iProfile] then
			return MsgThenLog(user, cVictim)
		else
			if theAllowed[user.sName][1]==1 then
				return MsgThenLog(user, cVictim)
			else
				user:SendPM(sBot, "You can't kick users of this profile")
				return 1
			end
		end
	elseif cCmd == "ban" then
		if not tProfiles[cVictim.iProfile] then
			cVictim:SendPM(sBot, "You have been PERMBANNED!")
			cVictim:Ban()
			return MsgThenLog(user, cVictim)
		else
			if theAllowed[user.sName][2]==1 then
				cVictim:SendPM(sBot, "You have been PERMBANNED!")
				cVictim:Ban()
				return MsgThenLog(user, cVictim)
			else
				user:SendPM(sBot, "You can't ban users of this profile")
				return 1
			end
		end
	elseif cCmd == "nickban" then
		if not tProfiles[cVictim.iProfile] then
				cVictim:SendPM(sBot, "Your username has been PERMBANNED!")
				return MsgThenLog(user, cVictim)
		else
			if theAllowed[user.sName][3]==1 then
				cVictim:SendPM(sBot, "Your username has been PERMBANNED!")
				return MsgThenLog(user, cVictim)
				--return nil
			else
				user:SendPM(sBot, "You can't nickban users of this profile")
				return 1
			end
		end
	elseif cCmd == "banip" then
		local bVictim = GetItemByName(victim)
		if not tProfiles[bVictim.iProfile] then
				bVictim:SendPM(sBot, "Your IP has been PERMBANNED!")
				return MsgThenLog(user, bVictim)
		else
			if theAllowed[user.sName][4]==1 then
				bVictim:SendPM(sBot, "Your IP has been PERMBANNED!")
				return MsgThenLog(user, bVictim)
				--return nil
			else
				user:SendPM(sBot, "You can't ipban users of this profile")
				return 1
			end
		end
	elseif cCmd == "delreguser" then
		if not tProfiles[cVictim.iProfile] then
				return MsgThenLog(user, cVictim)
		else
			if theAllowed[user.sName][5]==1 then
				return MsgThenLog(user, cVictim)
				--return nil
			else
				user:SendPM(sBot, "You can't delete users accounts of this profile")
				return 1
			end
		end
	elseif cCmd == "drop" then
		if not tProfiles[cVictim.iProfile] then
				return MsgThenLog(user, cVictim)
		else
			if theAllowed[user.sName][6]==1 then
				return MsgThenLog(user, cVictim)
				--return nil
			else
				user:SendPM(sBot, "You can't drop users of this profile")
				return 1
			end
		end
	end
end

function MsgThenLog(user, cVictim,bVictim)
	if cCmd == "kick" then
		cVictim:SendPM(sBot, "You have been kicked! Reason: ")
		cVictim:SendPM(sBot, "Number of kicks left before permban: <")
		cVictim:TempBan()
	elseif cCmd == "banip" then
		SendToAll(Bot, "ipban mupp")
		bVictim:Disconnect()
	end
	message = "<"..user.sName..">("..user.sIP..") - "..cMsgPart.." user <"..cVictim.sName..">("..cVictim.sIP..")"
	SendPmToOps(sBot, message)
	StoreLog(message)
end

function StoreLog(message)
   local cDate = string.gsub(os.date("%x"), "%D", "-")
   local fFile = io.open("logs/"..cFilename..".txt", "a+")
   fFile:write("["..cDate.."]"..message.."\n")
   fFile:close()
end


Dessamator

I think i know where your problem comes from :
local s,e,sCmd, victim, Reason = string.find(sData, "^%b<>%s*(%S*)%s*(%S*)%s*(.*)|")


This string.find, will only find victim, if theres a reason, otherwise the victim will be nil .
Ignorance is Bliss.

bastya_elvtars

QuoteOriginally posted by Dessamator
I think i know where your problem comes from :
local s,e,sCmd, victim, Reason = string.find(sData, "^%b<>%s*(%S*)%s*(%S*)%s*(.*)|")


This string.find, will only find victim, if theres a reason, otherwise the victim will be nil .

With %S* I am unsure this is true.
Everything could have been anything else and it would have just as much meaning.

Dessamator

Indeed, but in this case its better to be save than sorry.

btw, :
Quote* 0 or more repetitions (returns "" on 0 repetitions)
Ignorance is Bliss.

blackwings

QuoteOriginally posted by Dessamator
Indeed, but in this case its better to be save than sorry.

btw, :
Quote* 0 or more repetitions (returns "" on 0 repetitions)
I know, but if you check my posts before the lastest one, you will see I use + in the benning, but it gives the same result.


Dessamator

Well, in those cases i use two "string.find"
1 to catch the command and the victim, and the other to catch the reason, never tried using ur method maybe it works too.
Ignorance is Bliss.

bastya_elvtars

QuoteOriginally posted by Dessamator
Well, in those cases i use two "string.find"
1 to catch the command and the victim, and the other to catch the reason, never tried using ur method maybe it works too.

Yours is the play safe one. ;)
Everything could have been anything else and it would have just as much meaning.

blackwings

#16
EDIT: nvm this old post :P


blackwings

the !delreguser command works now, but I get an annoying error message in PM from Ptokax inbuild bot =
[10:35:15] <#Ptokax> *** Error. User Zek3 is not in list of registered users!
Anyone know how to get rid of that error message?

here is the script =
-- Command Protection & Customizing 3.0 by blackwings - relesed 26/04-05
--- 28/04-05 - Tested on Ptokax 17.03
-- ADDED: !deluser command added
-- ADDED: a table for Ops that should be allowed to use the commansd on Ops
-- CHANGED/ADDED:message to a user that gets kicked + changed the TempBan().
-- CHANGED: changed the action from !drop to the ptokax default.
--- 29/04-05 - Tested on Ptokax 17.03
-- ADDED: valid prefix of the command check = !kick, +kick, /kick is allowed
-- ADDED: !nickban & !banip command
-- ADDED: table for protected profiles
-- ADDED: a counter for kicks & drops, which by default bans after 4 kicks/drops.
--- 01/05-05 - Tested on Ptokax 17.03
-- CHANGED: Name of the script to = Command Protection & Customizing
-- CHANGED: Optimized the code a little.
-- ADDED: on/off switch for each command for allowed Op username to use command
-- CHANGED: All the messages.
--- 14/06-05 - Tested on Ptokax 17.09
-- ADDED: logging for each command
-- FIXED: difference in !ban between non-regged och regged users
-- FIXED: bug in !delreguser protection
--- 05/09-05 - Tested on Ptokax 0.3.3.21
-- CHANGED: made the script more compact
-- FIXED: the kick counter
-- FIXED: the !delreguser command
-- REMOVED: command !banip, will add it back when I solved the problem

sBot = "#CMDprotect" 

tPrefix = frmHub:GetPrefixes()

-- How many times a user is allowed to be kicked/dropped, 0 = disabled.
theBan = 4

-- Put those operatos username that should be allowed to use these commands
-- 1 is to allow a certain commands for a Ops username, and the order is =
-- kick,ban,nickban,banip,delreguser,drop
theAllowed = {
["OPName01"] = {1,1,1,1,1,1},
["OPName02"] = {1,1,1,1,1,1},
["OPName03"] = {1,1,1,1,1,1},
}

-- Profiles that should be protected
tProfiles = {
	[0] = 1,
	[1] = 1,
	[2] = 1,
}

tProfiles2 = {
	[0] = 1,
	[1] = 1,
}

function ChatArrival(user, data)
	return CmdProte(user,data)
end
function ToArrival(user,data)
	return CmdProte(user,data)
end

oCount = {}
xCount = {}

function CmdProte(user, data)
	local s,e,Cmd, victim, Reason = string.find(data, "%b<>%s+(%S+)%s*(%S*)%s*(%S*).*|")
	local sCounter = 0
	if IfValidCMD(Cmd, "kick") and user.bOperator then
		local sVictim = GetItemByName(victim)
		if Reason == "" then
			user:SendData(sBot, "You need to type a reason for the kick")
		else 
			local whatCmd = 1
			if not tProfiles[sVictim.iProfile] then	 
				if oCount[sVictim.sName] == nil then
					oCount[sVictim.sName] = 1
					tmpBanLeft = theBan - 1
					local pt = 1
					return compactCodee(user,sVictim,data,whatCmd,pt)
				else 
					oCount[sVictim.sName] = oCount[sVictim.sName] +1
					if oCount[sVictim.sName] <= theBan then
						tmpBanLeft = theBan - oCount[sVictim.sName]
						local pt = 1
						return compactCodee(user,sVictim,data,whatCmd,pt)
					else
						sVictim:SendPM(sBot, "You are Permbanned! Reason: "..Reason)
						oCount[sVictim.sName] = nil
						sVictim:Ban()
						message = "<"..user.sName..">("..user.sIP..") - banned user <"..sVictim.sName..">("..sVictim.sIP..") Reason: "..Reason.." - [after 4x kicks]"
						SendPmToOps(sBot, message)
						cFilename = "banlog"
						StoreLog(message,cFilename)
					end
				end
			else
				if theAllowed[user.sName] and theAllowed[user.sName][1]==1 then
					local pt = 2
					return compactCodee(user,sVictim,data,whatCmd,pt)						
				else
					user:SendPM(sBot, "You can't kick users of this profile")
					return 1
				end
			end
		end
		return 1
	elseif IfValidCMD(Cmd, "ban") and user.bOperator then
		local sVictim = GetItemByName(victim)
		local whatCmd = 2
			if not tProfiles[sVictim.iProfile] then
				local pt = 1
				return compactCodee(user,sVictim,data,whatCmd,pt)
			else
				if theAllowed[user.sName] and theAllowed[user.sName][2]==1 then
					local pt = 2
					return compactCodee(user,sVictim,data,whatCmd,pt)
				else
					user:SendPM(sBot, "You can't permban users of this profile")
					return 1
				end
			end
		return 1
	elseif IfValidCMD(Cmd, "nickban") and user.bOperator then
		local sVictim = GetItemByName(victim)
		local whatCmd = 3
			if not tProfiles[sVictim.iProfile] then
				return compactCodee(user,sVictim,data,whatCmd)
			else
				if theAllowed[user.sName] and theAllowed[user.sName][3]==1 then
					return compactCodee(user,sVictim,data,whatCmd)
				else
					user:SendPM(sBot, "You can't nickban users of this profile")
					return 1
				end
			end
		return 1
	elseif IfValidCMD(Cmd, "delreguser") and user.bOperator then
		local whatCmd = 4
		local sVictim = GetItemByName(victim)
		if sVictim~=nil then
			if not tProfiles2[sVictim.iProfile] then
				local pt = 1
				return compactCodee(user,sVictim,data,whatCmd,pt)
			else
				if theAllowed[user.sName] and theAllowed[user.sName][5]==1 then
					local pt = 1
					return compactCodee(user,sVictim,data,whatCmd,pt)
				else
					user:SendPM(sBot, "You can't delete users accounts of this profile")
					return 1
				end
			end
		else		
			vProfile = GetUserProfile(victim)
			if not tProfiles2[vProfile] then
				local pt = 2
				sVictim = victim
				return compactCodee(user,sVictim,data,whatCmd,pt)
			else
				if theAllowed[user.sName] and theAllowed[user.sName][5]==1 then
					local pt = 2
					sVictim = victim
					return compactCodee(user,sVictim,data,whatCmd,pt)
				else
					user:SendPM(sBot, "You can't delete users accounts of this profile")
					return 1
				end
			end
		end
		return 1
	elseif IfValidCMD(Cmd, "drop") and user.bOperator then
		local sVictim = GetItemByName(victim)
		local whatCmd = 5
			if not tProfiles[sVictim.iProfile] then
				if xCount[sVictim.sName] == nil then
					xCount[sVictim.sName] = 1
					tmpBanLeft = theBan - 1
					return compactCodee(user,sVictim,data,whatCmd)
				else 
					xCount[sVictim.sName] = xCount[sVictim.sName] +1
					if xCount[sVictim.sName] <= theBan then
						tmpBanLeft = theBan - xCount[user.sName]
						return compactCodee(user,sVictim,data,whatCmd)
					else
						xCount[sVictim.sName] = nil
						sVictim:Ban()
					end
				end
			else
				if theAllowed[user.sName] and theAllowed[user.sName][6]==1 then
					return compactCodee(user,sVictim,data,whatCmd)
				else
					user:SendPM(sBot, "You can't drop users of this profile")
					return 1
				end
			end
		return 1
	end
end

function IfValidCMD(sIsCMD, sWhatCMD)
	for sIndex in tPrefix do
		if sIsCMD == (tPrefix[sIndex]..sWhatCMD) then
			return 1
		end
	end
end

function compactCodee(user,sVictim,data,whatCmd,pt)
	if whatCmd == 1 then
		sVictim:SendPM(sBot, "You have been kicked! Reason: "..Reason)
		if pt == 1 then
			sVictim:SendPM(sBot, "Number of kicks left before permban: <"..tempBanLeft..">")
		end
		sVictim:TempBan()
		cFilename = "kicklog"
		cAction = "kicked"
	elseif whatCmd == 2 then		
		sVictim:SendPM(sBot, "You were PERMBANNED")
		if pt == 1 then
			if sVictim.iProfile ~= -1 then
				sVictim:Ban()
			else
				sVictim:Ban()
				NickBan(sVictim.sName)
			end
		else
			sVictim:Ban()
		end
		cFilename = "banlog"
		cAction = "banned"
	elseif  whatCmd == 3 then
		sVictim:SendPM(sBot, "Your username is permbanned")
		cFilename = "banlog"
		cAction = "nickbanned"
		return nil
	elseif  whatCmd == 4 then
		if pt == 1 then
			cFilename = "deluser"
			cAction = "deleted"
			return nil
		else
			cFilename = "deluser"
			cAction = "deleted"
			DelRegUser(sVictim)
		end
	elseif  whatCmd == 5 then
		cFilename = "droplog"
		cAction = "dropped"
		return nil
	end
	if  whatCmd == 4 then
		message = "<"..user.sName..">("..user.sIP..") - "..cAction.." user <"..sVictim..">"
	else
		message = "<"..user.sName..">("..user.sIP..") - "..cAction.." user <"..sVictim.sName..">("..sVictim.sIP..") Reason: "..Reason
	end
	SendPmToOps(sBot, message)
	StoreLog(message,cFilename)
end

function StoreLog(message,cFilename)
   local cDate = string.gsub(os.date("%x"), "%D", "-")
   local fFile = io.open("logs/"..cFilename..".txt", "a+")
   fFile:write("["..cDate.."]"..message.."\n")
   fFile:flush()
   fFile:close()
end


Dessamator

Well, you can do it in two ways,
1. Return 1,  after you've found the string
2.Use a different command(change its name)
Ignorance is Bliss.

SMF spam blocked by CleanTalk