@@ -212,10 +212,13 @@ 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
- ? client . twitterClient . sendLongTweet ( chunk . trim ( ) , previousTweetId , mediaData )
218
- : client . twitterClient . sendTweet ( chunk . trim ( ) , previousTweetId , mediaData )
220
+ ? client . twitterClient . sendLongTweet ( cleanChunk , previousTweetId , mediaData )
221
+ : client . twitterClient . sendTweet ( cleanChunk , previousTweetId , mediaData )
219
222
) ;
220
223
221
224
const body = await result . json ( ) ;
@@ -361,3 +364,30 @@ function splitParagraph(paragraph: string, maxLength: number): string[] {
361
364
362
365
return chunks ;
363
366
}
367
+
368
+ function deduplicateMentions ( paragraph : string ) {
369
+ // Regex to match mentions at the beginning of the string
370
+ const mentionRegex = / ^ @ ( \w + ) (?: \s + @ ( \w + ) ) * ( \s + | $ ) / ;
371
+
372
+ // Find all matches
373
+ const matches = paragraph . match ( mentionRegex ) ;
374
+
375
+ if ( ! matches ) {
376
+ return paragraph ; // If no matches, return the original string
377
+ }
378
+
379
+ // Extract mentions from the match groups
380
+ let mentions = matches . slice ( 1 ) . filter ( Boolean ) as string [ ] ;
381
+
382
+ // Deduplicate mentions
383
+ mentions = [ ...new Set ( mentions ) ] ;
384
+
385
+ // Reconstruct the string with deduplicated mentions
386
+ const uniqueMentionsString = `@${ mentions . join ( ' ' ) } ` ;
387
+
388
+ // Find where the mentions end in the original string
389
+ const endOfMentions = paragraph . indexOf ( matches [ 0 ] ) + matches [ 0 ] . length ;
390
+
391
+ // Construct the result by combining unique mentions with the rest of the string
392
+ return uniqueMentionsString + paragraph . slice ( endOfMentions ) ;
393
+ }
0 commit comments