Skip to content

Commit

Permalink
Update large.py
Browse files Browse the repository at this point in the history
- Fixed an issue where player lists could exceed 1000 characters
  • Loading branch information
BattlefieldDuck committed Oct 24, 2023
1 parent 4ec29aa commit e18c417
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions discordgsm/styles/large.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,23 @@ def description(self) -> str:

def embed(self) -> Embed:
embed = super().embed()
field_name = t(f"embed.field.{'members' if self.server.game_id == 'discord' else 'player_list'}.name", self.locale)
self.add_player_list_fields(embed, field_name, self.server.result['players'])
field_name = t(
f"embed.field.{'members' if self.server.game_id == 'discord' else 'player_list'}.name", self.locale)
self.add_player_list_fields(
embed, field_name, self.server.result['players'])

return embed

def add_player_list_fields(self, embed: Embed, field_name: str, players: List[GamedigPlayer]):
empty_value = '*​*'
filtered_players = [player for player in players if player['name'].strip()]
filtered_players = sorted(filtered_players, key=lambda player: player['name'])
filtered_players = [
player for player in players if player['name'].strip()]
filtered_players = sorted(
filtered_players, key=lambda player: player['name'])

counts = [0, 0, 0]
values = ['', '', '']
player_count = 0

for i, player in enumerate(filtered_players):
name = player['name'].ljust(23)[:23]
Expand All @@ -39,11 +46,25 @@ def add_player_list_fields(self, embed: Embed, field_name: str, players: List[Ga

# Replace Markdown
# https://support.discord.com/hc/en-us/articles/210298617-Markdown-Text-101-Chat-Formatting-Bold-Italic-Underline-
name = name.replace('*', '\\*').replace('_', '\\_').replace('~', '\\~').replace('`', '\\`').replace('>', '\\>')
name = name.replace('*', '\\*').replace('_', '\\_').replace('~',
'\\~').replace('`', '\\`').replace('>', '\\>')

index = i % len(values)
counts[index] += len(name)

player_left = len(filtered_players) - player_count
remaining_players_message = f'... {player_left} more player{"" if player_left <= 1 else "s"}'

# Embed must be 1024 or fewer in length. (Actually 1000)
if (counts[index] + len(remaining_players_message)) >= 1000:
values[index] += remaining_players_message
break

values[i % len(values)] += f"{name}\n"
values[index] += f'{name}\n'
player_count += 1

for i, name in enumerate([field_name, empty_value, empty_value]):
embed.add_field(name=name, value=values[i] if values[i] else empty_value)
embed.add_field(
name=name, value=values[i] if values[i] else empty_value)

return embed

0 comments on commit e18c417

Please sign in to comment.