Skip to content

Commit 989d840

Browse files
htillycursoragent
andcommitted
refactor: Improve Discord message chunking based on code review
- Use single regex for emoji conversion (better performance) - Fix exact-length line bug that could cause blank messages - Change line length check from > to >= maxLength Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent ec3c59a commit 989d840

1 file changed

Lines changed: 4 additions & 5 deletions

File tree

lib/discord.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ async function sendDiscordMessage(channelId, text, options = {}) {
258258
let discordText = text.replace(/<(https?:\/\/[^|>]+)\|([^>]+)>/g, '[$2]($1)');
259259

260260
// Convert common Slack emoji codes to Unicode emoji for Discord
261+
// Use a single regex-based replacement for better performance with large messages
261262
const emojiMap = {
262263
':notes:': '🎵',
263264
':lock:': '🔒',
@@ -284,9 +285,7 @@ async function sendDiscordMessage(channelId, text, options = {}) {
284285
':exclamation:': '❗',
285286
':sparkles:': '✨'
286287
};
287-
for (const [slackEmoji, unicodeEmoji] of Object.entries(emojiMap)) {
288-
discordText = discordText.split(slackEmoji).join(unicodeEmoji);
289-
}
288+
discordText = discordText.replace(/:[a-z_]+:/g, (match) => emojiMap[match] || match);
290289

291290
// Discord has a 2000 char limit, split into chunks if needed
292291
// Use 1800 as max to have buffer for edge cases with Unicode
@@ -338,8 +337,8 @@ async function sendDiscordMessage(channelId, text, options = {}) {
338337
await new Promise(resolve => setTimeout(resolve, 300));
339338
}
340339

341-
// Handle oversized single lines
342-
if (line.length > maxLength) {
340+
// Handle oversized or exact-length lines (>= to avoid adding newline that exceeds limit)
341+
if (line.length >= maxLength) {
343342
await sendChunkSafe(line);
344343
chunkCount++;
345344
await new Promise(resolve => setTimeout(resolve, 300));

0 commit comments

Comments
 (0)