prevent duplicate comments using wp_insert_comment

Please Note: Doing this will be very slow! It would be much better to do one (or preferably both) of the following instead:

  1. Use wp_cron to do this on a regular basis
  2. Instead of grabbing all comments, the first time it runs set at timestamp in the options table after it completes. Each time afterwards, read the timestamp and grab just those comments made since the timestamp… and update the timestamp on completion

That being said:

To do this, you should check for duplicates before adding the comment, which requires deciding what constitutes a duplicate comment.

Assuming the author_url and the comment itself would work, you could do something like the following:

function my_comment_already_exists($author_url, $comment) {
    $already_exists = false;
    global $wpdb;
    # try grabbing the first 40 characters of the comment. Hopefully that will make it unique
    # also changed select from 1 as it_exists to comment_ID in case prepare broke that part
    $comment_bit = substr($comment, 0, 40);
    $like_bit = $wpdb->esc_like( $comment_bit ) . '%';
    $query = "SELECT comment_ID FROM {$wpdb->prefix}comments WHERE comment_author_url = %s AND comment_content LIKE %s";
    $query = $wpdb->prepare( $query, $author_url, $like_bit );
    $results = $wpdb->get_results( $query );
    if ( is_null($results) ) {
       # error
       if ( $wpdb->last_error ) {
           echo "<p>Error: {$wpdb->last_error}</p>";
       }
    } else if ( is_array($results) && 0 < count($results) ) {
        $already_exists = true;
    }
    return $already_exists;
}

Then, for each comment, you could do something like:

if ( !my_comment_already_exists( 'https://www.facebook.com/'.$v->from->id, $v->message ) ) {
    wp_insert_comment($data);
}