Skip to content

Commit

Permalink
truncate embed fields related to message content if content is too long
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmusgerdin committed Aug 17, 2020
1 parent 5b5d7a4 commit 2d25539
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/commands/User/quote.js
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion src/events/messagePin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { Event } = require('klasa');
const { MessageEmbed } = require('discord.js');
const { truncate } = require('../lib/util/Util');

module.exports = class extends Event {

Expand All @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion src/events/rtbyteMessageDelete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { Event } = require('klasa');
const { MessageEmbed } = require('discord.js');
const { truncate } = require('../lib/util/Util');

module.exports = class extends Event {

Expand All @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion src/events/rtbyteMessageReactionAdd.js
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion src/events/rtbyteMessageReactionRemove.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { Event } = require('klasa');
const { MessageEmbed } = require('discord.js');
const { truncate } = require('../lib/util/Util');

module.exports = class extends Event {

Expand Down Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions src/events/rtbyteMessageUpdate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { Event } = require('klasa');
const { MessageEmbed } = require('discord.js');
const { truncate } = require('../lib/util/Util');

module.exports = class extends Event {

Expand Down Expand Up @@ -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'));

Expand Down
7 changes: 7 additions & 0 deletions src/lib/util/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))}...`;
};

0 comments on commit 2d25539

Please sign in to comment.