From 2d255398a07d55e40fe6026c89c310abecdafb37 Mon Sep 17 00:00:00 2001 From: Rasmus Gerdin Date: Mon, 17 Aug 2020 16:42:16 +0200 Subject: [PATCH] truncate embed fields related to message content if content is too long --- src/commands/User/quote.js | 3 ++- src/events/messagePin.js | 3 ++- src/events/rtbyteMessageDelete.js | 3 ++- src/events/rtbyteMessageReactionAdd.js | 3 ++- src/events/rtbyteMessageReactionRemove.js | 3 ++- src/events/rtbyteMessageUpdate.js | 5 +++-- src/lib/util/Util.js | 7 +++++++ 7 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/commands/User/quote.js b/src/commands/User/quote.js index e8044a93..65553cd5 100644 --- a/src/commands/User/quote.js +++ b/src/commands/User/quote.js @@ -1,6 +1,7 @@ const { Command } = require('klasa'); const { MessageEmbed } = require('discord.js'); const moment = require('moment-timezone'); +const { truncate } = require('../../lib/util/Util'); module.exports = class extends Command { @@ -31,7 +32,7 @@ module.exports = class extends Command { // eslint-disable-next-line max-len .setFooter(`${moment.tz(qmsg.createdTimestamp, msg.guild ? msg.guild.settings.get('timezone') : 'Etc/Greenwich').format('Do MMMM YYYY, h:mmA zz')} ${msg.guild ? msg.language.get('COMMAND_QUOTE_CHANNEL', qmsg) : msg.language.get('COMMAND_QUOTE_DMS')}`); - if (qmsg.content) await embed.addField(msg.guild.language.get('MESSAGE'), qmsg.content); + if (qmsg.content) await embed.addField(msg.guild.language.get('MESSAGE'), truncate(qmsg.content)); if (qmsg.embeds.length) await embed.addField('‎', msg.language.get('MESSAGE_EMBED', qmsg.url)); // Message attachment checks. diff --git a/src/events/messagePin.js b/src/events/messagePin.js index d075aa62..ca944ceb 100644 --- a/src/events/messagePin.js +++ b/src/events/messagePin.js @@ -1,5 +1,6 @@ const { Event } = require('klasa'); const { MessageEmbed } = require('discord.js'); +const { truncate } = require('../lib/util/Util'); module.exports = class extends Event { @@ -24,7 +25,7 @@ module.exports = class extends Event { .setTimestamp(msg.createdTimestamp) .setFooter(msg.language.get('PINBOARD_PINNED_BY', executor), executor ? executor.displayAvatarURL() : undefined); - if (msg.content) await embed.addField(msg.guild.language.get('MESSAGE'), msg.content); + if (msg.content) await embed.addField(msg.guild.language.get('MESSAGE'), truncate(msg.content)); if (msg.embeds.length) await embed.addField('‎', msg.language.get('MESSAGE_EMBED', msg.url)); // Message attachment checks. diff --git a/src/events/rtbyteMessageDelete.js b/src/events/rtbyteMessageDelete.js index ddec92bf..63eb6620 100644 --- a/src/events/rtbyteMessageDelete.js +++ b/src/events/rtbyteMessageDelete.js @@ -1,5 +1,6 @@ const { Event } = require('klasa'); const { MessageEmbed } = require('discord.js'); +const { truncate } = require('../lib/util/Util'); module.exports = class extends Event { @@ -23,7 +24,7 @@ module.exports = class extends Event { .setTimestamp() .setFooter(msg.language.get('GUILD_LOG_MESSAGEDELETE')); - if (msg.content) await embed.addField(msg.guild.language.get('MESSAGE'), msg.content); + if (msg.content) await embed.addField(msg.guild.language.get('MESSAGE'), truncate(msg.content)); if (msg.embeds.length) await embed.addField('‎', msg.language.get('GUILD_LOG_MESSAGEDELETE_EMBED')); // Message attachment checks. diff --git a/src/events/rtbyteMessageReactionAdd.js b/src/events/rtbyteMessageReactionAdd.js index 47bc0d28..2d85832e 100644 --- a/src/events/rtbyteMessageReactionAdd.js +++ b/src/events/rtbyteMessageReactionAdd.js @@ -1,6 +1,7 @@ /* eslint-disable complexity */ const { Event } = require('klasa'); const { MessageEmbed } = require('discord.js'); +const { truncate } = require('../lib/util/Util'); module.exports = class extends Event { @@ -29,7 +30,7 @@ module.exports = class extends Event { .setTimestamp(msg.createdTimestamp) .setFooter(`🌟 ${reaction.count}`); - if (msg.content) await embed.addField(msg.guild.language.get('MESSAGE'), msg.content); + if (msg.content) await embed.addField(msg.guild.language.get('MESSAGE'), truncate(msg.content)); if (msg.embeds.length) await embed.addField('‎', msg.language.get('MESSAGE_EMBED', msg.url)); // Message attachment checks. diff --git a/src/events/rtbyteMessageReactionRemove.js b/src/events/rtbyteMessageReactionRemove.js index 8d70d728..4f0516ae 100644 --- a/src/events/rtbyteMessageReactionRemove.js +++ b/src/events/rtbyteMessageReactionRemove.js @@ -1,5 +1,6 @@ const { Event } = require('klasa'); const { MessageEmbed } = require('discord.js'); +const { truncate } = require('../lib/util/Util'); module.exports = class extends Event { @@ -29,7 +30,7 @@ module.exports = class extends Event { .setTimestamp(msg.createdTimestamp) .setFooter(`🌟 ${reaction.count}`); - if (msg.content) await embed.addField(msg.guild.language.get('MESSAGE'), msg.content); + if (msg.content) await embed.addField(msg.guild.language.get('MESSAGE'), truncate(msg.content)); if (msg.embeds.length) await embed.addField('‎', msg.language.get('MESSAGE_EMBED', msg.url)); // Message attachment checks. diff --git a/src/events/rtbyteMessageUpdate.js b/src/events/rtbyteMessageUpdate.js index 3c32b80c..729e262f 100644 --- a/src/events/rtbyteMessageUpdate.js +++ b/src/events/rtbyteMessageUpdate.js @@ -1,5 +1,6 @@ const { Event } = require('klasa'); const { MessageEmbed } = require('discord.js'); +const { truncate } = require('../lib/util/Util'); module.exports = class extends Event { @@ -27,8 +28,8 @@ module.exports = class extends Event { .setAuthor(msg.author.tag, msg.author.displayAvatarURL()) .setColor(this.client.settings.get('colors.blue')) .setDescription(`${msg.channel}\n[${msg.language.get('CLICK_TO_VIEW')}](${msg.url})`) - .addField(msg.language.get('GUILD_LOG_BEFORE'), `${old.content}`) - .addField(msg.language.get('GUILD_LOG_AFTER'), `${msg.content}`) + .addField(msg.language.get('GUILD_LOG_BEFORE'), truncate(old.content)) + .addField(msg.language.get('GUILD_LOG_AFTER'), truncate(msg.content)) .setTimestamp() .setFooter(msg.language.get('GUILD_LOG_MESSAGEUPDATE')); diff --git a/src/lib/util/Util.js b/src/lib/util/Util.js index de881f4a..b4fc2775 100644 --- a/src/lib/util/Util.js +++ b/src/lib/util/Util.js @@ -48,3 +48,10 @@ exports.timezoneWithDate = (prop, guild) => { const { capitalize } = require('./Util'); return `${capitalize(moment.tz(prop, guild.settings.get('timezone')).fromNow())} (${moment.tz(prop, guild.settings.get('timezone')).format('MMMM Do, YYYY')})`; }; + +exports.truncate = (str) => { + if (typeof str !== 'string') return ''; + if (str.length <= 1021) return str; + + return `${str.substr(0, str.lastIndexOf(' ', 1021))}...`; +};