mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 11:18:54 -05:00
[Modlog] userinfo stops breaking with high numbers of roles on a user (#2971)
* Fixes it, I guess * changelog * reluctant handling of what the people want here * mypy would have prevented this one
This commit is contained in:
parent
6075c5bde0
commit
4f1f49d96f
1
changelog.d/mod/2910.bugfix.rst
Normal file
1
changelog.d/mod/2910.bugfix.rst
Normal file
@ -0,0 +1 @@
|
|||||||
|
userinfo doesn't break with absurd numbers of roles.
|
||||||
@ -125,15 +125,46 @@ class ModInfo(MixinMeta):
|
|||||||
activity = _("Watching {}").format(user.activity.name)
|
activity = _("Watching {}").format(user.activity.name)
|
||||||
|
|
||||||
if roles:
|
if roles:
|
||||||
roles = ", ".join([x.mention for x in roles])
|
|
||||||
|
role_str = ", ".join([x.mention for x in roles])
|
||||||
|
# 400 BAD REQUEST (error code: 50035): Invalid Form Body
|
||||||
|
# In embed.fields.2.value: Must be 1024 or fewer in length.
|
||||||
|
if len(role_str) > 1024:
|
||||||
|
# Alternative string building time.
|
||||||
|
# This is not the most optimal, but if you're hitting this, you are losing more time
|
||||||
|
# to every single check running on users than the occasional user info invoke
|
||||||
|
# We don't start by building this way, since the number of times we hit this should be
|
||||||
|
# infintesimally small compared to when we don't across all uses of Red.
|
||||||
|
continuation_string = _(
|
||||||
|
"and {numeric_number} more roles not displayed due to embed limits."
|
||||||
|
)
|
||||||
|
available_length = 1024 - len(continuation_string) # do not attempt to tweak, i18n
|
||||||
|
|
||||||
|
role_chunks = []
|
||||||
|
remaining_roles = 0
|
||||||
|
|
||||||
|
for r in roles:
|
||||||
|
chunk = f"{r.mention}, "
|
||||||
|
chunk_size = len(chunk)
|
||||||
|
|
||||||
|
if chunk_size < available_length:
|
||||||
|
available_length -= chunk_size
|
||||||
|
role_chunks.append(chunk)
|
||||||
else:
|
else:
|
||||||
roles = None
|
remaining_roles += 1
|
||||||
|
|
||||||
|
role_chunks.append(continuation_string.format(numeric_number=remaining_roles))
|
||||||
|
|
||||||
|
role_str = "".join(role_chunks)
|
||||||
|
|
||||||
|
else:
|
||||||
|
role_str = None
|
||||||
|
|
||||||
data = discord.Embed(description=activity, colour=user.colour)
|
data = discord.Embed(description=activity, colour=user.colour)
|
||||||
data.add_field(name=_("Joined Discord on"), value=created_on)
|
data.add_field(name=_("Joined Discord on"), value=created_on)
|
||||||
data.add_field(name=_("Joined this server on"), value=joined_on)
|
data.add_field(name=_("Joined this server on"), value=joined_on)
|
||||||
if roles is not None:
|
if role_str is not None:
|
||||||
data.add_field(name=_("Roles"), value=roles, inline=False)
|
data.add_field(name=_("Roles"), value=role_str, inline=False)
|
||||||
if names:
|
if names:
|
||||||
# May need sanitizing later, but mentions do not ping in embeds currently
|
# May need sanitizing later, but mentions do not ping in embeds currently
|
||||||
val = filter_invites(", ".join(names))
|
val = filter_invites(", ".join(names))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user