diff --git a/src/structure/components/observers/GuildLogging.js b/src/structure/components/observers/GuildLogging.js index d217235..fb4e009 100644 --- a/src/structure/components/observers/GuildLogging.js +++ b/src/structure/components/observers/GuildLogging.js @@ -185,16 +185,37 @@ class GuildLogger extends Observer { } const { reference, channel, author, content, id } = message; + const words = content.split(' '); + const fields = []; + + let field = { + name: ZeroWidthChar, + value: '' + }; + for (const word of words) { + + if (field.value.length + word.length > EmbedLimits.fieldValue && word.length < EmbedLimits.fieldValue) { + fields.push(field); + field = { ...field, value: '' }; + } + + if (word.length > EmbedLimits.fieldValue) + field.value += ` ${word.substring(0, EmbedLimits.fieldValue - field.value.length - 4)}...`; + else + field.value += ` ${word}`; + } + if (field.value.length) fields.push(field); const embed = { title: wrapper.format('MSGLOG_DELETE_TITLE', { channel: channel.name, author: Util.escapeMarkdown(author.tag) }), - description: Util.escapeMarkdown(content)?.replace(/\\n/gu, ' ') || wrapper.format('MSGLOG_NOCONTENT'), + // description: Util.escapeMarkdown(content)?.replace(/\\n/gu, ' ') || wrapper.format('MSGLOG_NOCONTENT'), + content: content?.length ? null : wrapper.format('MSGLOG_NOCONTENT'), color: CONSTANTS.COLORS.RED, footer: { text: wrapper.format('MSGLOG_DELETE_FOOTER', { msgID: id, userID: author.id }) }, timestamp: message.createdAt.toISOString(), - fields: [] + fields }; if (reference && reference.channelId === channel.id) { @@ -556,6 +577,7 @@ class GuildLogger extends Observer { if (ignore && ignore.includes(channel.id)) return; + // Message pin state was changed if (oldMessage.content === newMessage.content && oldMessage.pinned !== newMessage.pinned) { const embed = { @@ -596,27 +618,41 @@ class GuildLogger extends Observer { const oldCon = oldMessage.content, newCon = newMessage.content; - //Original content - embed.fields.push({ - name: wrapper.format('MSGLOG_EDIT_OLD'), - // eslint-disable-next-line no-nested-ternary - value: oldCon.length > 1024 ? - oldCon.substring(0, 1021) + '...' : oldCon.length ? oldCon : ZeroWidthChar - }); - if (oldCon.length > 1024) embed.fields.push({ + + const oldWords = oldCon.split(' '); + const newWords = newCon.split(' '); + + let field = { name: ZeroWidthChar, - value: '...' + oldCon.substring(1021) - }); - //Edited content - embed.fields.push({ - name: wrapper.format('MSGLOG_EDIT_NEW'), - // eslint-disable-next-line no-nested-ternary - value: newCon.length > 1024 ? newCon.substring(0, 1021) + '...' : newCon.length ? newCon : ZeroWidthChar - }); - if (newCon.length > 1024) embed.fields.push({ - name: ZeroWidthChar, - value: '...' + newCon.substring(1021) - }); + value: '' + }; + for (const [words, name] of [[oldWords, wrapper.format('MSGLOG_EDIT_OLD')], [newWords, wrapper.format('MSGLOG_EDIT_NEW')]]) { + field.name = name; + let length = 0; + for (const word of words) { + + if (field.value.length + word.length > EmbedLimits.fieldValue && word.length < EmbedLimits.fieldValue) { + embed.fields.push(field); + field = { name: ZeroWidthChar, value: '' }; + } + + if (word.length > EmbedLimits.fieldValue) + field.value += ` ${word.substring(0, EmbedLimits.fieldValue - field.value.length - 4)}...`; + else + field.value += ` ${word}`; + + // Embeds have a total character limit of 6000, so limiting fields to 5000, 2500 for old and new respectively + length += word.length; + if (length >= 2500) { + field.value = field.value.substring(0, 2500 - EmbedLimits.fieldValue * 2) + '...'; + break; + } + } + if (field.value.length) { + embed.fields.push(field); + field = { ...field, value: '' }; + } + } if (reference && reference.channelId === channel.id) { const referenced = await channel.messages.fetch(reference.messageId).catch(() => null); @@ -639,9 +675,6 @@ class GuildLogger extends Observer { } } - //if(oldMessage.content.length > 1024) embed.description += '\n' + oldMessage.format('MSGLOG_EDIT_OLD_CUTOFF'); - //if(newMessage.content.length > 1024) embed.description += '\n' + oldMessage.format('MSGLOG_EDIT_NEW_CUTOFF'); - await hook.send({ embeds: [embed] }).catch((err) => { this.logger.error('Error in message edit:\n' + err.stack + `\n${inspect(embed)}`); });