Fixed errors thrown when overloading discord webhooks

This commit is contained in:
Stefan Zermatten
2023-06-07 13:50:14 +02:00
parent 6bc737f850
commit 3acf42394d
3 changed files with 33 additions and 8 deletions

View File

@@ -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) {

View File

@@ -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() });
}
});
},

View File

@@ -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/<id>/<token>
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;