Skip to content

Commit d8fcffe

Browse files
htillycursoragent
andcommitted
fix: Discord message chunking for long queue lists
- Fix chunking logic to properly handle messages over 2000 chars - Add sendChunkSafe helper to ensure all chunks stay under limit - Convert Slack emoji codes to Unicode for Discord compatibility - Reduce maxLength to 1800 for extra Unicode buffer Fixes issue where 'list' command failed on Discord with 60+ tracks Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent c80b1eb commit d8fcffe

1 file changed

Lines changed: 64 additions & 23 deletions

File tree

lib/discord.js

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,62 @@ async function sendDiscordMessage(channelId, text, options = {}) {
257257
// Convert Slack markdown to Discord markdown
258258
let discordText = text.replace(/<(https?:\/\/[^|>]+)\|([^>]+)>/g, '[$2]($1)');
259259

260+
// Convert common Slack emoji codes to Unicode emoji for Discord
261+
const emojiMap = {
262+
':notes:': '🎵',
263+
':lock:': '🔒',
264+
':star:': '⭐',
265+
':stopwatch:': '⏱️',
266+
':cricket:': '🦗',
267+
':musical_note:': '🎵',
268+
':headphones:': '🎧',
269+
':speaker:': '🔊',
270+
':mute:': '🔇',
271+
':loud_sound:': '🔊',
272+
':sound:': '🔉',
273+
':fire:': '🔥',
274+
':thumbsup:': '👍',
275+
':thumbsdown:': '👎',
276+
':clap:': '👏',
277+
':party_popper:': '🎉',
278+
':tada:': '🎉',
279+
':warning:': '⚠️',
280+
':x:': '❌',
281+
':white_check_mark:': '✅',
282+
':checkmark:': '✅',
283+
':question:': '❓',
284+
':exclamation:': '❗',
285+
':sparkles:': '✨'
286+
};
287+
for (const [slackEmoji, unicodeEmoji] of Object.entries(emojiMap)) {
288+
discordText = discordText.split(slackEmoji).join(unicodeEmoji);
289+
}
290+
260291
// Discord has a 2000 char limit, split into chunks if needed
261-
const maxLength = 1900; // Leave some margin
292+
// Use 1800 as max to have buffer for edge cases with Unicode
293+
const maxLength = 1800;
262294
let messages = [];
263295

296+
// Helper function to send a chunk safely (splitting further if needed)
297+
const sendChunkSafe = async (chunk) => {
298+
if (chunk.length <= maxLength) {
299+
const message = await channel.send(chunk);
300+
messages.push(message);
301+
return;
302+
}
303+
// Chunk is still too long, split it
304+
let remaining = chunk;
305+
while (remaining.length > 0) {
306+
const piece = remaining.substring(0, maxLength);
307+
const message = await channel.send(piece);
308+
messages.push(message);
309+
remaining = remaining.substring(maxLength);
310+
if (remaining.length > 0) {
311+
await new Promise(resolve => setTimeout(resolve, 300));
312+
}
313+
}
314+
};
315+
264316
if (discordText.length <= maxLength) {
265317
// Single message
266318
const message = await channel.send(discordText);
@@ -274,44 +326,33 @@ async function sendDiscordMessage(channelId, text, options = {}) {
274326

275327
for (let i = 0; i < lines.length; i++) {
276328
const line = lines[i];
329+
const potentialLength = currentChunk.length + line.length + 1; // +1 for newline
277330

278-
if ((currentChunk + line + '\n').length > maxLength) {
279-
// Send current chunk
331+
if (potentialLength > maxLength) {
332+
// Send current chunk if it has content
280333
if (currentChunk.trim().length > 0) {
281-
const message = await channel.send(currentChunk);
282-
messages.push(message);
334+
await sendChunkSafe(currentChunk);
283335
chunkCount++;
284336
currentChunk = '';
285337
// Small delay between messages
286-
await new Promise(resolve => setTimeout(resolve, 500));
338+
await new Promise(resolve => setTimeout(resolve, 300));
287339
}
288340

289-
// Handle oversized single lines by splitting them
341+
// Handle oversized single lines
290342
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
343+
await sendChunkSafe(line);
344+
chunkCount++;
345+
await new Promise(resolve => setTimeout(resolve, 300));
304346
continue;
305347
}
306348
}
307349

308350
currentChunk += line + '\n';
309351
}
310352

311-
// Send remaining chunk
353+
// Send remaining chunk (with safety check)
312354
if (currentChunk.trim().length > 0) {
313-
const message = await channel.send(currentChunk);
314-
messages.push(message);
355+
await sendChunkSafe(currentChunk);
315356
chunkCount++;
316357
}
317358

0 commit comments

Comments
 (0)