@@ -212,15 +212,18 @@ export async function sendTweet(
212
212
} )
213
213
) ;
214
214
}
215
+
216
+ const cleanChunk = deduplicateMentions ( chunk . trim ( ) )
217
+
215
218
const result = await client . requestQueue . add ( async ( ) =>
216
219
isLongTweet
217
220
? client . twitterClient . sendLongTweet (
218
- chunk . trim ( ) ,
221
+ cleanChunk ,
219
222
previousTweetId ,
220
223
mediaData
221
224
)
222
225
: client . twitterClient . sendTweet (
223
- chunk . trim ( ) ,
226
+ cleanChunk ,
224
227
previousTweetId ,
225
228
mediaData
226
229
)
@@ -397,6 +400,33 @@ function splitSentencesAndWords(text: string, maxLength: number): string[] {
397
400
return chunks ;
398
401
}
399
402
403
+ function deduplicateMentions ( paragraph : string ) {
404
+ // Regex to match mentions at the beginning of the string
405
+ const mentionRegex = / ^ @ ( \w + ) (?: \s + @ ( \w + ) ) * ( \s + | $ ) / ;
406
+
407
+ // Find all matches
408
+ const matches = paragraph . match ( mentionRegex ) ;
409
+
410
+ if ( ! matches ) {
411
+ return paragraph ; // If no matches, return the original string
412
+ }
413
+
414
+ // Extract mentions from the match groups
415
+ let mentions = matches . slice ( 1 ) . filter ( Boolean ) as string [ ] ;
416
+
417
+ // Deduplicate mentions
418
+ mentions = [ ...new Set ( mentions ) ] ;
419
+
420
+ // Reconstruct the string with deduplicated mentions
421
+ const uniqueMentionsString = `@${ mentions . join ( ' ' ) } ` ;
422
+
423
+ // Find where the mentions end in the original string
424
+ const endOfMentions = paragraph . indexOf ( matches [ 0 ] ) + matches [ 0 ] . length ;
425
+
426
+ // Construct the result by combining unique mentions with the rest of the string
427
+ return uniqueMentionsString + paragraph . slice ( endOfMentions ) ;
428
+ }
429
+
400
430
function restoreUrls (
401
431
chunks : string [ ] ,
402
432
placeholderMap : Map < string , string >
0 commit comments