Skip to content

Commit

Permalink
[BattleRoyale] better remaining menu
Browse files Browse the repository at this point in the history
  • Loading branch information
japandotorg committed Aug 21, 2024
1 parent e7ad9cf commit 2684e48
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions battleroyale/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,25 @@

import random
from functools import partial
from typing import Any, List, Optional
from typing import Any, Dict, List, Optional

import discord
from redbot.core.bot import Red
from redbot.core.utils.chat_formatting import humanize_list
from redbot.core.utils.views import SimpleMenu
from redbot.core.utils.chat_formatting import pagify

__all__ = ("JoinGameView",)


class InteractionSimpleMenu(SimpleMenu):
async def inter(self, interaction: discord.Interaction[Red]) -> None:
await interaction.response.defer()
self._fallback_author_to_ctx = False
self.author: discord.abc.User = interaction.user
kwargs: Dict[str, Any] = await self.get_page(self.current_page)
self.message: discord.Message = await interaction.followup.send(**kwargs, ephemeral=True)


class RemainingPlayerButton(discord.ui.Button):
def __init__(
self,
Expand Down Expand Up @@ -125,34 +135,26 @@ async def on_timeout(self) -> None:
async def _callback(
self: RemainingPlayerButton, interaction: discord.Interaction[Red]
) -> None:
remaining_players: List[str] = list(
[
m.global_name if m.global_name else m.display_name
for m in sorted(
self.view.remaining,
key=lambda m: m.global_name if m.global_name else m.display_name,
)
]
)
random.shuffle(remaining_players)
remaining_player_str: str = humanize_list(remaining_players)
if not remaining_player_str:
players: List[discord.Member] = list(self.view.remaining).copy()
if not players:
await interaction.response.send_message(
embed=discord.Embed(
description="There are not remaining players!",
color=self.view.color,
)
)
return
remaining_players_str = (
f"{remaining_player_str[:4000]}..."
if len(remaining_player_str) > 4000
else remaining_player_str
)
embed: discord.Embed = discord.Embed.from_dict(
{
"description": f"**Remaining Players:**\n{remaining_players_str}.",
"color": int(self.view.color),
}
random.shuffle(players)
remaining: str = "\n".join(
"{}. {} ( `{}` )".format(idx, player.mention, player.id)
for idx, player in enumerate(players)
)
await interaction.response.send_message(embed=embed, ephemeral=True)
embeds: List[discord.Embed] = []
for page in list(pagify(remaining)):
embed: discord.Embed = discord.Embed(
title="Remaining Players",
description=page,
color=self.view.color,
)
embeds.append(embed)
await InteractionSimpleMenu(pages=embeds).inter(interaction)

0 comments on commit 2684e48

Please sign in to comment.