CodeGrind-Team / CodeGrind-Bot

Elevate your LeetCode experience with CodeGrind Bot: Compete on leaderboards, retrieve LeetCode problems, and track your progress! 🚀
https://top.gg/bot/1059122559066570885
Other
16 stars 8 forks source link

Handle embeds larger than the character limit #86

Closed Kevin-Roman closed 2 months ago

Kevin-Roman commented 1 year ago

Description

Using /problem, any problem that leads to an embed size of more than 6000 characters in total, alongside per field character restrictions, cannot be sent. These restrictions are documented here: https://www.pythondiscord.com/pages/guides/python-guides/discord-embed-limits/. An example of such question is LeetCode problem 1070.

Potential solutions

  1. Firstly, the total embed character size needs to be checked (https://discord.com/developers/docs/resources/channel#embed-object-embed-structure) alongside the sizes of the header/description/footer/fields/etc...
  2. Either:
    • Split the embed appropriately and send multiple embeds.
    • Or, determine which fields are too long (or make the total embed go over 6000 characters) and just don't include them in the embed, as the most important part of the question is its description.

      I prefer the latter just because one single embed looks cleaner and is shorter (and requires less Discord API usage), but it can be something to discuss and share opinions on.

  3. Send the multiple embeds, or the filtered embed (depending on the approach selected)

Where to start

  1. The question embed logic is in ./ui/embeds/problems.py - question_embed
  2. Create a function that splits/filters (based on the selected approach) an embed, that calculates the size of each section in the embed and the total size.
  3. Then check each character size against the limits.
  4. Appropriately modify the embed if necessary.
LiamGlasson commented 2 months ago

This issue was unknowingly resolved in this commit. The problem was caused by the following behaviour in the old code:

  1. When question constraints weren't found, constraints_position was -1.
  2. This led to constraints_position + len(constraint_query_string) being a small positive number.
  3. The resulting slice operation meant that most of the question content was included in the constraints field, surpassing the Discord embed limit.

When this commit was later merged on May 24th 2024, this issue was fixed as questions no longer included constraints. This means that all questions now fall within the Discord embed character limit of 6000 characters, and this issue can be closed.