codesupport / discord-bot

The CodeSupport Discord Bot
https://codesupport.dev/discord
MIT License
21 stars 19 forks source link

Embed Field Value Exceeds Limit Causing Crash #224

Open LamboCreeper opened 2 years ago

LamboCreeper commented 2 years ago

Overview

When a message is deleted or updated it triggers one of two handlers, these handlers take the contents of the original message and insert it into a field within an embed. However, the character limit of these fields is smaller than the limit of normal messages.

Expected Behaviour

A message containing more than 1024 characters gets trimmed to 1024 characters within the embed.

Actual Behaviour

A message containing more than 1024 characters causes the bot to crash.

2022-03-04T11:10:05.406066+00:00 app[worker.1]: DiscordAPIError: Invalid Form Body
2022-03-04T11:10:05.406066+00:00 app[worker.1]: embeds[0].fields[0].value: Must be 1024 or fewer in length.
2022-03-04T11:10:05.406067+00:00 app[worker.1]: at RequestHandler.execute (/app/node_modules/discord.js/src/rest/RequestHandler.js:350:13)
2022-03-04T11:10:05.406068+00:00 app[worker.1]: at processTicksAndRejections (node:internal/process/task_queues:96:5)
2022-03-04T11:10:05.406069+00:00 app[worker.1]: at async RequestHandler.push (/app/node_modules/discord.js/src/rest/RequestHandler.js:51:14)
2022-03-04T11:10:05.406070+00:00 app[worker.1]: at async TextChannel.send (/app/node_modules/discord.js/src/structures/interfaces/TextBasedChannel.js:175:15)
2022-03-04T11:10:05.406070+00:00 app[worker.1]: at async Client.sendLog (/app/build/abstracts/LogMessageDeleteHandler.js:16:13)
2022-03-04T11:10:05.406070+00:00 app[worker.1]: at async Client.handle (/app/build/event/handlers/LogMessageSingleDeleteHandler.js:11:9)
LamboCreeper commented 2 years ago

I believe this is also affecting the quote embeds.

olivier-b-be commented 2 years ago

Done some small testing around myself, currently I've added a simple sliceToLength static function in the StringUtils class that will slice the given string if it exceeds the passed in number. Added a value for the embed field in config.json to get the max length of the field.

You'd lose info because the content of the field is simply being cut away, there's no real extra fancy stuff going around about not slicing in the middle of a word. And another thing is that this would need to be added to every embed.addField call (at least for dynamic content like the message content in Discord itself)

ari-party commented 2 years ago

Library throws an error, if not catched it will cause the process to end

process.on("uncaughtException", (err, origin) => { console.error(err, origin) }) is what you are looking for