-- !ban [id]

CreateChat "@ban" "<id>" (20) [[
	local p = tonumber(s[2])
	if p and player(p,"exists") and not player(p,"bot") then
		if SERVER_RANK[ USER[p]["rank"] ].lvl < SERVER_RANK[ USER[id]["rank"] ].lvl then
			ServerMSG(player(id,"name").." banned "..player(p,"name"))
			
			local name = player(p,"name")
			local ip = player(p,"ip")
			local usgn = player(p,"usgn")
			
			parse('banname "'..name..'"')
			if ip ~= "0.0.0.0" then
				parse('banip "'..ip..'"')
			end
			
			if usgn > 0 then
				parse('banusgn "'..usgn..'"')
			end
		else
			ErrorMSG(id, "You can't ban someone that's higher/equal rank")
		end
	elseif not p then
		ban_menu:OpenPlayer(id, 1)
	end
]]

CreateChat "@ipban" "[ip]" (20) [[
	if args >= 2 then
		local ip = s[2]
		if ip then
			local pl = IPPlayer(ip)
			if pl then
				if SERVER_RANK[USER[pl]["rank"] ].lvl < SERVER_RANK[USER[id]["rank"] ].lvl then
					ServerMSG(player(id,"name").." banned "..pl)
					parse('banip "'..ip..'"')
				else
					ErrorMSG(id, "You can't ban someone that's higher/equal rank")
				end
			else
				ErrorMSG(id, "There are no players with this IP")
			end
		end
	end
]]

CreateChat "@usgnban" "<usgn>" (20) [[
	if args >= 2 then
		local usgn = tonumber(s[2])
		if usgn then
			local pl = USGNPlayer(usgn)
			if pl then
				if SERVER_RANK[USER[pl]["rank"] ].lvl < SERVER_RANK[USER[id]["rank"] ].lvl then
					ServerMSG(player(id,"name").." banned "..pl)
					parse('banusgn "'..usgn..'"')
				else
					ErrorMSG(id, "You can't ban someone that's higher/equal rank")
				end
			else
				ErrorMSG(id, "There are no players with this USGN")
			end
		end
	end
]]

CreateChat "@nameban" "[name]" (20) [[
	if args >= 2 then
		local name = string.sub(txt, pos[2])
		if name then
			local pl = NamePlayer(name)
			if pl then
				if SERVER_RANK[USER[pl]["rank"] ].lvl < SERVER_RANK[USER[id]["rank"] ].lvl then
					ServerMSG(player(id,"name").." banned "..pl)
					parse('banname "'..name..'"')
				else
					ErrorMSG(id, "You can't ban someone that's higher/equal rank")
				end
			else
				ErrorMSG(id, "There are no players with this name")
			end
		end
	end
]]

ban_menu = CreateMenu("Ban Player")

function ban_menu:getcustombutton(i)
	local id = player(0,"table")[i]
	if id then return player(id,"name") end
end

AddMenu(ban_menu, 20)

function ban_menu:click(id,b,page)
	local p = player(0,"table")[b]
	if p and player(p,"exists") then
		if SERVER_RANK[USER[p]["rank"]].lvl < SERVER_RANK[USER[id]["rank"]].lvl then
			ServerMSG(player(id,"name").." banned "..player(p,"name"))
			
			local name = player(p,"name")
			local ip = player(p,"ip")
			local usgn = player(p,"usgn")
			parse('banname "'..name..'"')
			
			if ip ~= "0.0.0.0" then
				parse('banip "'..ip..'"')
			end
			
			if usgn > 0 then
				parse('banusgn "'..usgn..'"')
			end
		else
			ErrorMSG(id, "You can't ban someone that's higher/equal rank")
		end
	end
	if b > 0 then
		ban_menu:OpenPlayer(id, page)
	end
end