Function to automatically add featured image

Hi everyone,

I am looking for a function that will automatically set the featured image of the post by taking the 1st image in the gallery of the post.

The images are not attached inside the post. They reside within the gallery of the post.

Any ideas would be much appreciated.