mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 11:18:54 -05:00
[Help] Fix embed size calculation for additional text (#3208)
* Fix size calculation for additional text * changelog * ffs * because of course that's a thing * *sigh* * prevent an edge case * more * ... * ...
This commit is contained in:
parent
12af6232e2
commit
8b18526f46
1
changelog.d/3208.bugfix.rst
Normal file
1
changelog.d/3208.bugfix.rst
Normal file
@ -0,0 +1 @@
|
|||||||
|
Fixes a way for help to end up a little too large for discord limits
|
||||||
@ -271,15 +271,22 @@ class RedHelpFormatter:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def group_embed_fields(fields: List[EmbedField], max_chars=1000):
|
def group_embed_fields(fields: List[EmbedField], max_chars=1000):
|
||||||
|
|
||||||
curr_group = []
|
curr_group = []
|
||||||
ret = []
|
ret = []
|
||||||
|
current_count = 0
|
||||||
|
|
||||||
for f in fields:
|
for f in fields:
|
||||||
curr_group.append(f)
|
f_len = len(f.value) + len(f.name)
|
||||||
if sum(len(f.value) for f in curr_group) > max_chars:
|
if curr_group and (f_len + current_count > max_chars):
|
||||||
ret.append(curr_group)
|
ret.append(curr_group)
|
||||||
curr_group = []
|
curr_group = []
|
||||||
|
current_count = 0
|
||||||
if len(curr_group) > 0:
|
curr_group.append(f)
|
||||||
|
current_count += f_len
|
||||||
|
else:
|
||||||
|
# Loop cleanup here
|
||||||
|
if curr_group:
|
||||||
ret.append(curr_group)
|
ret.append(curr_group)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
@ -289,13 +296,43 @@ class RedHelpFormatter:
|
|||||||
pages = []
|
pages = []
|
||||||
|
|
||||||
page_char_limit = await ctx.bot._config.help.page_char_limit()
|
page_char_limit = await ctx.bot._config.help.page_char_limit()
|
||||||
|
page_char_limit = min(page_char_limit, 5990) # Just in case someone was manually...
|
||||||
|
|
||||||
|
author_info = {"name": f"{ctx.me.display_name} Help Menu", "icon_url": ctx.me.avatar_url}
|
||||||
|
|
||||||
|
# Offset calculation here is for total embed size limit
|
||||||
|
# 20 accounts for# *Page {i} of {page_count}*
|
||||||
|
offset = len(author_info["name"]) + 20
|
||||||
|
foot_text = embed_dict["footer"]["text"]
|
||||||
|
if foot_text:
|
||||||
|
offset += len(foot_text)
|
||||||
|
offset += len(embed_dict["embed"]["description"])
|
||||||
|
offset += len(embed_dict["embed"]["title"])
|
||||||
|
|
||||||
|
# In order to only change the size of embeds when neccessary for this rather
|
||||||
|
# than change the existing behavior for people uneffected by this
|
||||||
|
# we're only modifying the page char limit should they be impacted.
|
||||||
|
# We could consider changing this to always just subtract the offset,
|
||||||
|
# But based on when this is being handled (very end of 3.2 release)
|
||||||
|
# I'd rather not stick a major visual behavior change in at the last moment.
|
||||||
|
if page_char_limit + offset > 5990:
|
||||||
|
# This is still neccessary with the max interaction above
|
||||||
|
# While we could subtract 100% of the time the offset from page_char_limit
|
||||||
|
# the intent here is to shorten again
|
||||||
|
# *only* when neccessary, by the exact neccessary amount
|
||||||
|
# To retain a visual match with prior behavior.
|
||||||
|
page_char_limit = 5990 - offset
|
||||||
|
elif page_char_limit < 250:
|
||||||
|
# Prevents an edge case where a combination of long cog help and low limit
|
||||||
|
# Could prevent anything from ever showing up.
|
||||||
|
# This lower bound is safe based on parts of embed in use.
|
||||||
|
page_char_limit = 250
|
||||||
|
|
||||||
field_groups = self.group_embed_fields(embed_dict["fields"], page_char_limit)
|
field_groups = self.group_embed_fields(embed_dict["fields"], page_char_limit)
|
||||||
|
|
||||||
color = await ctx.embed_color()
|
color = await ctx.embed_color()
|
||||||
page_count = len(field_groups)
|
page_count = len(field_groups)
|
||||||
|
|
||||||
author_info = {"name": f"{ctx.me.display_name} Help Menu", "icon_url": ctx.me.avatar_url}
|
|
||||||
|
|
||||||
if not field_groups: # This can happen on single command without a docstring
|
if not field_groups: # This can happen on single command without a docstring
|
||||||
embed = discord.Embed(color=color, **embed_dict["embed"])
|
embed = discord.Embed(color=color, **embed_dict["embed"])
|
||||||
embed.set_author(**author_info)
|
embed.set_author(**author_info)
|
||||||
|
|||||||
@ -1313,14 +1313,14 @@ class Core(commands.Cog, CoreLogic):
|
|||||||
|
|
||||||
This setting only applies to embedded help.
|
This setting only applies to embedded help.
|
||||||
|
|
||||||
Please note that setting a relitavely small character limit may
|
The default value is 1000 characters. The minimum value is 500.
|
||||||
mean some pages will exceed this limit. This is because categories
|
The maximum is based on the lower of what you provide and what discord allows.
|
||||||
are never spread across multiple pages in the help message.
|
|
||||||
|
|
||||||
The default value is 1000 characters.
|
Please note that setting a relatively small character limit may
|
||||||
|
mean some pages will exceed this limit.
|
||||||
"""
|
"""
|
||||||
if limit <= 0:
|
if limit < 500:
|
||||||
await ctx.send(_("You must give a positive value!"))
|
await ctx.send(_("You must give a value of at least 500 characters."))
|
||||||
return
|
return
|
||||||
|
|
||||||
await ctx.bot._config.help.page_char_limit.set(limit)
|
await ctx.bot._config.help.page_char_limit.set(limit)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user