on script load:
	set {antibot.lastused} to now
	set {antibot} to false
	delete {antibot.whitelist-pending::*}
	execute console command "whitelist off"
	
	#====== clear database over 3 months ===========
	#-----------------------------------------
	loop all players:
		if loop-player has permission "staff":
			message "&cAntibot cleaning database..." to loop-player
	#-----------------------------------------
	loop {antibot.whitelist::*}:
		set {_name} to loop-index
		set {_diff} to difference between {antibot.whitelist::%{_name}%} and now
		if {_diff} is more than 90 days:
			delete {antibot.whitelist::%{_name}%}
			execute console command "whitelist remove %{_name}%"
			add 1 to {_count}							#for count how much old data deleted
	#-----------------------------------------
	loop all players:
		if loop-player has permission "staff":
			message "&cRemoved %{_count}% from whitelist" to loop-player
	#-----------------------------------------
	
	
every 3 minute:
	if {antibot} = true:
		if {antibot.waited} is less than 2 seconds:
			execute console command "helpop attack is still going"
			stop
			
		else:
			set {antibot} to false
			execute server command "whitelist off"
			execute console command "helpop bot attack finished, whitelist off"
			
	else:
		# add new player to whitelist
		loop {antibot.whitelist-pending::*}:
			set {_p} to loop-index parsed as player
			if {_p} is online:
				set {_waited} to difference between {antibot.whitelist-pending::%{_p}%} and now
				if {_waited} is more than 2 minutes:
					delete {antibot.whitelist-pending::%{_p}%}
					set {antibot.whitelist::%{_p}%} to now
					execute console command "whitelist add %{_p}%"
					execute console command "helpop added %{_p}% to whitelist"

on quit:
	delete {antibot.whitelist-pending::%player%}
	
on connect:
	if {antibot.whitelist::%player%} exist:
		stop
		# need some code to detect whitelisted player spam join

	# detect strangers join
	else:
		set {antibot.waited} to difference between {antibot.lastused} and now
		if {antibot.waited} is less than 2 seconds:
			if {antibot} = true:
				stop
			else:
				set {antibot} to true
				execute server command "whitelist on"
				execute console command "helpop bot attack, whitelist on"
	
	# no problem will reset
	set {antibot.lastused} to now
	
	# then... continue code to on join event...

on join:
	# bot detect for whitelisted players & add whitelist
	if {antibot} = false:
		if {antibot.whitelist::%player%} exist:
			stop
		else:
			# add new player to whitelist
			set {antibot.whitelist-pending::%player%} to now
	

# update online player to whitelist
command /antibot [<text>]:
	aliases: ab
	permission: staff
	trigger:
		if arg = "on":
			set {antibot} to true
			execute server command "whitelist on"
		if arg = "off":
			set {antibot} to false
			execute server command "whitelist off"
		if arg = "addwhitelist":
			message "&eadding online players to whitelist..."
			# add new player to whitelist
			loop all players:
				if {antibot.whitelist::%loop-player%} exist:
					stop
				else:
					set {_antibot.whitelist} to loop-player
					wait 3 second
					if {_antibot.whitelist} is online:
						set {antibot.whitelist::%loop-player%} to now
						execute console command "whitelist add %loop-player%"
						execute console command "helpop added %loop-player% to whitelist"

		if arg is not set:
			message "&e=========[&aAntibot&e]==========="
			message "&6Under Attack: &a%{antibot}%"
			message "&6Total Whitelisted: &a%size of {antibot.whitelist::*}%"
			message "&6Pending players for whitelist: &a[%size of {antibot.whitelist-pending::*}%]"
			loop {antibot.whitelist-pending::*}:
				set {_temp} to "&7%loop-value% &6%loop-index% "
				message "%{_temp}%"
			message "&d/ab <on/off> &6on off antibot"
		
	
#command /checkwhitelist:
#	permission: asdasdasd
#	trigger:
#		loop all players:
#			if loop-player is not in whitelist:
#				message "&e%loop-player%"


#command /antijoinbot [<text>]:
#    aliases: ajb
#    trigger:
#        if {antijoinbot.enabled} is true:
#            message "&4AntiJoinBot  &7&oby Tim64 [Version 0.3] &8- &2&lENABLED"
#            message "&aBots Blocked: &l%{totalbots}%"
#        else:
#            message "&4AntiJoinBot  &7&oby Tim64 [Version 0.3] &8- &4&lDISABLED"
#            message "&aBots Blocked: &l%{totalbots}%"
#        if arg-1 is "enable":
#            player has permission "*"
#            set {antijoinbot.enabled} to true
#            message "&4&lAntiBot &8&l|&r &a&lEnabled!"
#        if arg-1 is "disable":
#            player has permission "*"
#            set {antijoinbot.enabled} to false
#            message "&4&lAntiBot &8&l|&r &4&lDisabled!"
 
            
#on connect:
#    {antijoinbot.enabled} is true
#    if {firstjoin::%player%} is not set:
#        kick the player due to "&4&lAntiBot &8&l|&r &aPlease reconnect in 10 Seconds! &8&l|&r &4&lAntiBot"
#        set {firstjoin::%player%} to ""
#        add 1 to {blockedconnection}
#        if {blockedconnection} > 5:
#            loop all players:
#                loop-player has permission "antijoinbot.message"
#                message "&4&lAntiBot &8&l|&r Blocked &r&a%{blockedconnection}%&r Bots!" to loop-player 
#                
#            add {blockedconnection} to {totalbots}
#            set {blockedconnection} to 0