@@ -257,10 +257,61 @@ async function sendDiscordMessage(channelId, text, options = {}) {
257257 // Convert Slack markdown to Discord markdown
258258 let discordText = text . replace ( / < ( h t t p s ? : \/ \/ [ ^ | > ] + ) \| ( [ ^ > ] + ) > / g, '[$2]($1)' ) ;
259259
260+ // Convert common Slack emoji codes to Unicode emoji for Discord
261+ // Use a single regex-based replacement for better performance with large messages
262+ const emojiMap = {
263+ ':notes:' : '🎵' ,
264+ ':lock:' : '🔒' ,
265+ ':star:' : '⭐' ,
266+ ':stopwatch:' : '⏱️' ,
267+ ':cricket:' : '🦗' ,
268+ ':musical_note:' : '🎵' ,
269+ ':headphones:' : '🎧' ,
270+ ':speaker:' : '🔊' ,
271+ ':mute:' : '🔇' ,
272+ ':loud_sound:' : '🔊' ,
273+ ':sound:' : '🔉' ,
274+ ':fire:' : '🔥' ,
275+ ':thumbsup:' : '👍' ,
276+ ':thumbsdown:' : '👎' ,
277+ ':clap:' : '👏' ,
278+ ':party_popper:' : '🎉' ,
279+ ':tada:' : '🎉' ,
280+ ':warning:' : '⚠️' ,
281+ ':x:' : '❌' ,
282+ ':white_check_mark:' : '✅' ,
283+ ':checkmark:' : '✅' ,
284+ ':question:' : '❓' ,
285+ ':exclamation:' : '❗' ,
286+ ':sparkles:' : '✨'
287+ } ;
288+ discordText = discordText . replace ( / : [ a - z _ ] + : / g, ( match ) => emojiMap [ match ] || match ) ;
289+
260290 // Discord has a 2000 char limit, split into chunks if needed
261- const maxLength = 1900 ; // Leave some margin
291+ // Use 1800 as max to have buffer for edge cases with Unicode
292+ const maxLength = 1800 ;
262293 let messages = [ ] ;
263294
295+ // Helper function to send a chunk safely (splitting further if needed)
296+ const sendChunkSafe = async ( chunk ) => {
297+ if ( chunk . length <= maxLength ) {
298+ const message = await channel . send ( chunk ) ;
299+ messages . push ( message ) ;
300+ return ;
301+ }
302+ // Chunk is still too long, split it
303+ let remaining = chunk ;
304+ while ( remaining . length > 0 ) {
305+ const piece = remaining . substring ( 0 , maxLength ) ;
306+ const message = await channel . send ( piece ) ;
307+ messages . push ( message ) ;
308+ remaining = remaining . substring ( maxLength ) ;
309+ if ( remaining . length > 0 ) {
310+ await new Promise ( resolve => setTimeout ( resolve , 300 ) ) ;
311+ }
312+ }
313+ } ;
314+
264315 if ( discordText . length <= maxLength ) {
265316 // Single message
266317 const message = await channel . send ( discordText ) ;
@@ -274,44 +325,33 @@ async function sendDiscordMessage(channelId, text, options = {}) {
274325
275326 for ( let i = 0 ; i < lines . length ; i ++ ) {
276327 const line = lines [ i ] ;
328+ const potentialLength = currentChunk . length + line . length + 1 ; // +1 for newline
277329
278- if ( ( currentChunk + line + '\n' ) . length > maxLength ) {
279- // Send current chunk
330+ if ( potentialLength > maxLength ) {
331+ // Send current chunk if it has content
280332 if ( currentChunk . trim ( ) . length > 0 ) {
281- const message = await channel . send ( currentChunk ) ;
282- messages . push ( message ) ;
333+ await sendChunkSafe ( currentChunk ) ;
283334 chunkCount ++ ;
284335 currentChunk = '' ;
285336 // Small delay between messages
286- await new Promise ( resolve => setTimeout ( resolve , 500 ) ) ;
337+ await new Promise ( resolve => setTimeout ( resolve , 300 ) ) ;
287338 }
288339
289- // Handle oversized single lines by splitting them
290- if ( line . length > maxLength ) {
291- // Split the line into smaller chunks
292- let remainingLine = line ;
293- while ( remainingLine . length > 0 ) {
294- const chunk = remainingLine . substring ( 0 , maxLength ) ;
295- const message = await channel . send ( chunk ) ;
296- messages . push ( message ) ;
297- chunkCount ++ ;
298- remainingLine = remainingLine . substring ( maxLength ) ;
299- if ( remainingLine . length > 0 ) {
300- await new Promise ( resolve => setTimeout ( resolve , 500 ) ) ;
301- }
302- }
303- // Skip adding to currentChunk since we already sent it
340+ // Handle oversized or exact-length lines (>= to avoid adding newline that exceeds limit)
341+ if ( line . length >= maxLength ) {
342+ await sendChunkSafe ( line ) ;
343+ chunkCount ++ ;
344+ await new Promise ( resolve => setTimeout ( resolve , 300 ) ) ;
304345 continue ;
305346 }
306347 }
307348
308349 currentChunk += line + '\n' ;
309350 }
310351
311- // Send remaining chunk
352+ // Send remaining chunk (with safety check)
312353 if ( currentChunk . trim ( ) . length > 0 ) {
313- const message = await channel . send ( currentChunk ) ;
314- messages . push ( message ) ;
354+ await sendChunkSafe ( currentChunk ) ;
315355 chunkCount ++ ;
316356 }
317357
0 commit comments