From 3acf42394d021e0d40b946e7834d5c033a769f62 Mon Sep 17 00:00:00 2001 From: Stefan Zermatten Date: Wed, 7 Jun 2023 13:50:14 +0200 Subject: [PATCH] Fixed errors thrown when overloading discord webhooks --- app/imports/api/creature/log/CreatureLogs.js | 26 ++++++++++++++++--- .../components/actions/ActionCard.vue | 2 +- app/imports/server/discord/sendWebhook.js | 13 +++++++--- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/app/imports/api/creature/log/CreatureLogs.js b/app/imports/api/creature/log/CreatureLogs.js index 5b858b29..8d21bd3d 100644 --- a/app/imports/api/creature/log/CreatureLogs.js +++ b/app/imports/api/creature/log/CreatureLogs.js @@ -7,9 +7,10 @@ import { RateLimiterMixin } from 'ddp-rate-limiter-mixin'; import { assertEditPermission } from '/imports/api/creature/creatures/creaturePermissions.js'; import { parse, prettifyParseError } from '/imports/parser/parser.js'; import resolve, { toString } from '/imports/parser/resolve.js'; -const PER_CREATURE_LOG_LIMIT = 100; import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js'; +const PER_CREATURE_LOG_LIMIT = 100; + if (Meteor.isServer) { var sendWebhookAsCreature = require('/imports/server/discord/sendWebhook.js').sendWebhookAsCreature; } @@ -70,10 +71,21 @@ function logToMessageData(log) { let embed = { fields: [], }; - log.content.forEach(field => { + log.content.forEach((field, index) => { + // Empty character for blank names if (!field.name) field.name = '\u200b'; if (!field.value) field.value = '\u200b'; - embed.fields.push(field); + // Enforce Discord field character limits + if (field.name.length > 256) { + field.name = field.name.substring(0, 255); + } + if (field.value.length > 1024) { + field.value = field.value.substring(0, 1024 - 3) + '...'; + } + // Enforce Discord 25 field limit + if (index < 25) { + embed.fields.push(field); + } }); return { embeds: [embed] }; } @@ -122,7 +134,15 @@ export function insertCreatureLogWork({ log, creature, method }) { log = { content: [{ value: log }] }; } if (!log.content?.length) return; + + // Truncate the string lengths to fit the log content schema + log.content.forEach((logItem) => { + if (logItem.value.length > STORAGE_LIMITS.summary) { + logItem.value = logItem.value.substring(0, STORAGE_LIMITS.summary - 3) + '...'; + } + }); log.date = new Date(); + // Insert it let id = CreatureLogs.insert(log); if (Meteor.isServer) { diff --git a/app/imports/client/ui/properties/components/actions/ActionCard.vue b/app/imports/client/ui/properties/components/actions/ActionCard.vue index 55553f48..9565868f 100644 --- a/app/imports/client/ui/properties/components/actions/ActionCard.vue +++ b/app/imports/client/ui/properties/components/actions/ActionCard.vue @@ -217,7 +217,7 @@ export default { this.doActionLoading = false; if (error) { console.error(error); - snackbar({ text: error.reason }); + snackbar({ text: error.reason || error.message || error.toString() }); } }); }, diff --git a/app/imports/server/discord/sendWebhook.js b/app/imports/server/discord/sendWebhook.js index 044de8c3..a8a473df 100644 --- a/app/imports/server/discord/sendWebhook.js +++ b/app/imports/server/discord/sendWebhook.js @@ -1,5 +1,5 @@ import Discord from 'discord.js' -export default function sendWebhook({webhookURL, data = {}}){ +export default function sendWebhook({ webhookURL, data = {} }) { //webhookURL = https://discordapp.com/api/webhooks// let urlArray = webhookURL.split('/'); let token = urlArray.pop(); @@ -9,11 +9,16 @@ export default function sendWebhook({webhookURL, data = {}}){ data.disableMentions = 'all'; const hook = new Discord.WebhookClient(id, token); - // Send a message using the webhook - hook.send(data); + try { + // Send a message using the webhook + hook.send(data); + } catch (e) { + // Swallow the error, we don't really care + console.error(e); + } } -export function sendWebhookAsCreature({creature, data = {}}){ +export function sendWebhookAsCreature({ creature, data = {} }) { if (!creature || !creature.settings || !creature.settings.discordWebhook) return; data.username = creature.name; data.avatarURL = creature.avatarPicture;