Skip to content

Commit

Permalink
Truncate embeds for event arguments since fields allow a maximum of 1…
Browse files Browse the repository at this point in the history
…024 characters.
  • Loading branch information
derekpierre committed Aug 8, 2024
1 parent 495e578 commit 8ada409
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion pique/discord/embeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@
from pique.constants.networks import NETWORKS
from pique.log import LOGGER

MAX_EMBED_CHARS = 1024


def truncate_middle(value: str, max_size: int):
if len(value) <= max_size:
return value

if max_size < 5:
# don't expect to get here but for safety
# 5 because letter on either end with ellipsis in the middle i.e. "_..._"
return f"{value[:1]}..."

first_half_end_index = max_size // 2 - 1 # one less than center
first_half_text = value[:first_half_end_index]

second_half_length = max_size - len(first_half_text) - 3 # remove 3 for the ellipsis
return f"{first_half_text}...{value[-second_half_length:]}"


def _inline_code(text):
return f"`{text}`"
Expand Down Expand Up @@ -35,7 +53,8 @@ def pretty_format_blocks(latest_scanned_blocks):

def add_event_args_fields(embed, event):
for key, val in event.args.items():
embed.add_field(name=key, value=_inline_code(str(val)), inline=False)
text = truncate_middle(str(val), MAX_EMBED_CHARS-2) # -2 because of ticks (``) used below
embed.add_field(name=key, value=f"`{text}`", inline=False)


def format_uptime(raw_uptime):
Expand Down

0 comments on commit 8ada409

Please sign in to comment.