--Check moderation data

game.Players.PlayerAdded:Connect(function(plr)
	
	--Kick new players if game is shutting down
	if shuttingDown then
		plr:Kick("This server has shut down.")
	end
	
	--Kick banned players, or remove ban data if ban time is over
	local banSuccess, banData = pcall(function()
		local data = ds:GetAsync(plr.UserId .. "Banned")
		return data
	end)
	
	if banSuccess and banData then
		local banEnd = banData[1]
		
		if os.time() >= banEnd then
			pcall(function()
				ds:RemoveAsync(plr.UserId .. "Banned")
			end)
			
		else
			local banReason = banData[2]
			
			local dateInfo = os.date("!*t", banEnd)
			local dateFormatted = dateInfo["day"] .. "/" .. dateInfo["month"] .. "/" .. dateInfo["year"] .. "  " .. dateInfo["hour"] .. ":" .. dateInfo["min"] .. ":" .. dateInfo["sec"]
			local banMessage = "Banned for: " .. banReason .. " | Ends: " .. dateFormatted
			
			plr:Kick(banMessage)
		end
	end
	
	--Mute players or unmute them if mute time is over
	local muteSuccess, muteData = pcall(function()
		local data = ds:GetAsync(plr.UserId .. "Muted")
		return data
	end)
	
	if muteSuccess and muteData then
		local muteEnd = muteData[1]
		
		if os.time() >= muteEnd then
			pcall(function()
				ds:RemoveAsync(plr.UserId .. "Muted")
			end)
			
		else
			channel:MuteSpeaker(plr.Name)
			
			local timeToUnmute = muteEnd - os.time()
			task.wait(timeToUnmute)
			
			if plr then
				channel:UnmuteSpeaker(plr.Name)
			end
		end
	end
end)

