feat: make dgames more versatile

This commit is contained in:
Teesh 2025-03-17 23:06:54 +02:00
parent a96b3bf858
commit ca8dc84693
3 changed files with 8 additions and 7 deletions

View file

@ -12,7 +12,7 @@ class Essential(commands.Cog):
async def ping(self, interaction: discord.Interaction): async def ping(self, interaction: discord.Interaction):
embed = discord.Embed( embed = discord.Embed(
title="🏓 Pong!", title="🏓 Pong!",
description=f"Latency: {interaction.client.latency}ms", description=f"Latency: {interaction.client.latency * 100}ms",
color=discord.Color.green(), color=discord.Color.green(),
) )
await interaction.response.send_message(embed=embed) await interaction.response.send_message(embed=embed)

View file

@ -149,7 +149,7 @@ class TicTacToe(commands.Cog):
@app_commands.command(name="tictactoe", description="Play a game of Tic Tac Toe!") @app_commands.command(name="tictactoe", description="Play a game of Tic Tac Toe!")
async def tictactoe(self, interaction: discord.Interaction): async def tictactoe(self, interaction: discord.Interaction):
# logger.debug("New tictactoe game started!") # logger.debug("New tictactoe game started!")
view = Lobby(min_players=2) view = Lobby(min_players=2, max_players=2)
await interaction.response.send_message(embed=view.make_embed(), view=view) await interaction.response.send_message(embed=view.make_embed(), view=view)

View file

@ -2,12 +2,13 @@ import discord
class Lobby: class Lobby:
def __init__(self, min_players: int): def __init__(self, min_players: int, max_players: int):
self.min_players: int = min_players self.min_players: int = min_players
self.max_players: int = max_players
self.joined: set[int] = set() # Store IDs instead of objects self.joined: set[int] = set() # Store IDs instead of objects
def join(self, user: discord.User): def join(self, user: discord.User):
if len(self.joined) == self.min_players: if len(self.joined) == self.max_players:
return return
self.joined.add(user.id) # Store the user ID self.joined.add(user.id) # Store the user ID
@ -17,14 +18,14 @@ class Lobby:
class LobbyView(discord.ui.View): class LobbyView(discord.ui.View):
def __init__(self, min_players: int) -> None: def __init__(self, *args, **kwargs) -> None:
super().__init__() super().__init__()
self.lobby = Lobby(min_players=min_players) self.lobby = Lobby(*args, **kwargs)
self.to_play = "Some Unknown Game" self.to_play = "Some Unknown Game"
def make_embed(self) -> discord.Embed: def make_embed(self) -> discord.Embed:
desc = "\n".join([f"<@{user_id}>" for user_id in list(self.lobby.joined)]) desc = "\n".join([f"<@{user_id}>" for user_id in list(self.lobby.joined)])
desc += f"\n\nMinimum Players: {self.lobby.min_players} | Current players: {len(self.lobby.joined)}" desc += f"\n\nMinimum Players: {self.lobby.min_players} | Maximum Players: {self.lobby.max_players} | Current players: {len(self.lobby.joined)}"
return discord.Embed( return discord.Embed(
title=self.to_play, description=desc, color=discord.Color.gold() title=self.to_play, description=desc, color=discord.Color.gold()
) )