From 420968c38922f7ebbd763e20fbe916deb3bf5cb6 Mon Sep 17 00:00:00 2001 From: Cam Caswell Date: Sun, 5 Sep 2021 00:53:05 -0400 Subject: Split initial embed in two Board and claimed answers discord.py doesn't let you cleanly edit an embed with an image in it --- bot/exts/fun/duck_game.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'bot/exts/fun/duck_game.py') diff --git a/bot/exts/fun/duck_game.py b/bot/exts/fun/duck_game.py index 1ef7513f..eb509e55 100644 --- a/bot/exts/fun/duck_game.py +++ b/bot/exts/fun/duck_game.py @@ -130,6 +130,9 @@ class DuckGame: while len(self.solutions) < minimum_solutions: self.board = random.sample(DECK, size) + self.board_msg = None + self.found_msg = None + @property def board(self) -> list[tuple[int]]: """Accesses board property.""" @@ -191,8 +194,8 @@ class DuckGamesDirector(commands.Cog): game.running = True self.current_games[ctx.channel.id] = game - game.msg_content = "" - game.embed_msg = await self.send_board_embed(ctx, game) + game.board_msg = await self.send_board_embed(ctx, game) + game.found_msg = await self.send_found_embed(ctx) await asyncio.sleep(GAME_DURATION) # Checking for the channel ID in the currently running games is not sufficient. @@ -251,7 +254,7 @@ class DuckGamesDirector(commands.Cog): game.scores[msg.author] += INCORRECT_SOLN async def send_board_embed(self, ctx: commands.Context, game: DuckGame) -> discord.Message: - """Create and send the initial game embed. This will be edited as the game goes on.""" + """Create and send an embed to display the board.""" image = assemble_board_image(game.board, game.rows, game.columns) with BytesIO() as image_stream: image.save(image_stream, format="png") @@ -264,14 +267,21 @@ class DuckGamesDirector(commands.Cog): embed.set_image(url="attachment://board.png") return await ctx.send(embed=embed, file=file) + async def send_found_embed(self, ctx: commands.Context) -> discord.Message: + """Create and send an embed to display claimed answers. This will be edited as the game goes on.""" + # Can't be part of the board embed because of discord.py limitations with editing an embed with an image. + embed = discord.Embed( + title="Flights Found", + color=discord.Color.dark_purple(), + ) + return await ctx.send(embed=embed) + async def display_claimed_answer(self, game: DuckGame, author: discord.Member, answer: tuple[int]) -> None: """Add a claimed answer to the game embed.""" async with game.editing_embed: - # We specifically edit the message contents instead of the embed - # Because we load in the image from the file, editing any portion of the embed - # Does weird things to the image and this works around that weirdness - game.msg_content = f"{game.msg_content}\n{str(answer):12s} - {author.display_name}" - await game.embed_msg.edit(content=game.msg_content) + found_embed, = game.found_msg.embeds + found_embed.description = f"{found_embed.description}\n{str(answer):12s} - {author.display_name}" + await game.found_msg.edit(embed=found_embed) async def end_game(self, channel: discord.TextChannel, game: DuckGame, end_message: str) -> None: """Edit the game embed to reflect the end of the game and mark the game as not running.""" -- cgit v1.2.3 From e7bbd14f95f59593581a8c18da8bc4ef9a0f6208 Mon Sep 17 00:00:00 2001 From: Cam Caswell Date: Sun, 5 Sep 2021 00:56:58 -0400 Subject: Add function for appending to claimed answers embed --- bot/exts/fun/duck_game.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'bot/exts/fun/duck_game.py') diff --git a/bot/exts/fun/duck_game.py b/bot/exts/fun/duck_game.py index eb509e55..1fdc05be 100644 --- a/bot/exts/fun/duck_game.py +++ b/bot/exts/fun/duck_game.py @@ -248,7 +248,7 @@ class DuckGamesDirector(commands.Cog): if answer in game.solutions: game.claimed_answers[answer] = msg.author game.scores[msg.author] += CORRECT_SOLN - await self.display_claimed_answer(game, msg.author, answer) + await self.append_to_found_embed(game, f"{str(answer):12s} - {msg.author.display_name}") else: await msg.add_reaction(EMOJI_WRONG) game.scores[msg.author] += INCORRECT_SOLN @@ -276,6 +276,16 @@ class DuckGamesDirector(commands.Cog): ) return await ctx.send(embed=embed) + async def append_to_found_embed(self, game: DuckGame, text: str) -> None: + """Append text to the claimed answers embed.""" + async with game.editing_embed: + found_embed, = game.found_msg.embeds + old_desc = found_embed.description + if old_desc == discord.Embed.Empty: + old_desc = "" + found_embed.description = f"{old_desc.rstrip()}\n{text}" + await game.found_msg.edit(embed=found_embed) + async def display_claimed_answer(self, game: DuckGame, author: discord.Member, answer: tuple[int]) -> None: """Add a claimed answer to the game embed.""" async with game.editing_embed: -- cgit v1.2.3 From 65aec408bc3d50934d543b9124de67720d617bb8 Mon Sep 17 00:00:00 2001 From: Cam Caswell Date: Sun, 5 Sep 2021 01:12:10 -0400 Subject: Bring end_game up to date --- bot/exts/fun/duck_game.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'bot/exts/fun/duck_game.py') diff --git a/bot/exts/fun/duck_game.py b/bot/exts/fun/duck_game.py index 1fdc05be..46ea36fe 100644 --- a/bot/exts/fun/duck_game.py +++ b/bot/exts/fun/duck_game.py @@ -311,13 +311,19 @@ class DuckGamesDirector(commands.Cog): scoreboard_embed.description = scoreboard await channel.send(embed=scoreboard_embed) + board_embed, = game.board_msg.embeds + embed_as_dict = board_embed.to_dict() # Cannot set embed color after initialization + embed_as_dict["color"] = discord.Color.red().value + board_embed = discord.Embed.from_dict(embed_as_dict) + await self.edit_embed_with_image(game.board_msg, board_embed) + + found_embed, = game.found_msg.embeds missed = [ans for ans in game.solutions if ans not in game.claimed_answers] if missed: missed_text = "Flights everyone missed:\n" + "\n".join(f"{ans}" for ans in missed) else: missed_text = "All the flights were found!" - - await game.embed_msg.edit(content=f"{missed_text}") + await self.append_to_found_embed(game, f"\n{missed_text}") @start_game.command(name="help") async def show_rules(self, ctx: commands.Context) -> None: -- cgit v1.2.3 From f96ecffb3927acd254158210655524e24db37d3b Mon Sep 17 00:00:00 2001 From: Cam Caswell Date: Sun, 5 Sep 2021 01:13:49 -0400 Subject: Change board embed color Can't update color when the game is over anymore --- bot/exts/fun/duck_game.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bot/exts/fun/duck_game.py') diff --git a/bot/exts/fun/duck_game.py b/bot/exts/fun/duck_game.py index 46ea36fe..a456c86b 100644 --- a/bot/exts/fun/duck_game.py +++ b/bot/exts/fun/duck_game.py @@ -11,7 +11,7 @@ from PIL import Image, ImageDraw, ImageFont from discord.ext import commands from bot.bot import Bot -from bot.constants import Colours, MODERATION_ROLES +from bot.constants import MODERATION_ROLES from bot.utils.decorators import with_role DECK = list(product(*[(0, 1, 2)]*4)) @@ -262,7 +262,7 @@ class DuckGamesDirector(commands.Cog): file = discord.File(fp=image_stream, filename="board.png") embed = discord.Embed( title="Duck Duck Duck Goose!", - color=Colours.bright_green, + color=discord.Color.dark_purple(), ) embed.set_image(url="attachment://board.png") return await ctx.send(embed=embed, file=file) -- cgit v1.2.3 From 00c323222be2aeef010b15607f801b3066b5b1ed Mon Sep 17 00:00:00 2001 From: Cam Caswell Date: Fri, 24 Sep 2021 22:59:24 -0400 Subject: Remove display_claimed_answer Replaced with append_to_found_embed which is more general --- bot/exts/fun/duck_game.py | 7 ------- 1 file changed, 7 deletions(-) (limited to 'bot/exts/fun/duck_game.py') diff --git a/bot/exts/fun/duck_game.py b/bot/exts/fun/duck_game.py index a456c86b..8b5338f4 100644 --- a/bot/exts/fun/duck_game.py +++ b/bot/exts/fun/duck_game.py @@ -286,13 +286,6 @@ class DuckGamesDirector(commands.Cog): found_embed.description = f"{old_desc.rstrip()}\n{text}" await game.found_msg.edit(embed=found_embed) - async def display_claimed_answer(self, game: DuckGame, author: discord.Member, answer: tuple[int]) -> None: - """Add a claimed answer to the game embed.""" - async with game.editing_embed: - found_embed, = game.found_msg.embeds - found_embed.description = f"{found_embed.description}\n{str(answer):12s} - {author.display_name}" - await game.found_msg.edit(embed=found_embed) - async def end_game(self, channel: discord.TextChannel, game: DuckGame, end_message: str) -> None: """Edit the game embed to reflect the end of the game and mark the game as not running.""" game.running = False -- cgit v1.2.3 From baa4d252b72de1e25bceb15721b475ee9724d47b Mon Sep 17 00:00:00 2001 From: Cam Caswell Date: Fri, 24 Sep 2021 23:00:40 -0400 Subject: Don't change board embed color Can't edit that embed --- bot/exts/fun/duck_game.py | 7 ------- 1 file changed, 7 deletions(-) (limited to 'bot/exts/fun/duck_game.py') diff --git a/bot/exts/fun/duck_game.py b/bot/exts/fun/duck_game.py index 8b5338f4..f97c712d 100644 --- a/bot/exts/fun/duck_game.py +++ b/bot/exts/fun/duck_game.py @@ -304,13 +304,6 @@ class DuckGamesDirector(commands.Cog): scoreboard_embed.description = scoreboard await channel.send(embed=scoreboard_embed) - board_embed, = game.board_msg.embeds - embed_as_dict = board_embed.to_dict() # Cannot set embed color after initialization - embed_as_dict["color"] = discord.Color.red().value - board_embed = discord.Embed.from_dict(embed_as_dict) - await self.edit_embed_with_image(game.board_msg, board_embed) - - found_embed, = game.found_msg.embeds missed = [ans for ans in game.solutions if ans not in game.claimed_answers] if missed: missed_text = "Flights everyone missed:\n" + "\n".join(f"{ans}" for ans in missed) -- cgit v1.2.3 From debe4a185f1429f5dd139929c7506c04abd9fa34 Mon Sep 17 00:00:00 2001 From: Cam Caswell Date: Fri, 24 Sep 2021 23:00:58 -0400 Subject: Change docstring to be more help with the help command --- bot/exts/fun/duck_game.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/exts/fun/duck_game.py') diff --git a/bot/exts/fun/duck_game.py b/bot/exts/fun/duck_game.py index f97c712d..95e12ced 100644 --- a/bot/exts/fun/duck_game.py +++ b/bot/exts/fun/duck_game.py @@ -184,7 +184,7 @@ class DuckGamesDirector(commands.Cog): ) @commands.cooldown(rate=1, per=2, type=commands.BucketType.channel) async def start_game(self, ctx: commands.Context) -> None: - """Generate a board, send the game embed, and end the game after a time limit.""" + """Start a new Duck Duck Duck Goose game.""" if ctx.channel.id in self.current_games: await ctx.send("There's already a game running!") return -- cgit v1.2.3 From 97ccd35d27946abb39b86befdeb36364fea8b5ce Mon Sep 17 00:00:00 2001 From: camcaswell <38672443+camcaswell@users.noreply.github.com> Date: Sat, 2 Oct 2021 00:15:44 -0400 Subject: Make setting the old embed description cleaner --- bot/exts/fun/duck_game.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'bot/exts/fun/duck_game.py') diff --git a/bot/exts/fun/duck_game.py b/bot/exts/fun/duck_game.py index 95e12ced..10b03a49 100644 --- a/bot/exts/fun/duck_game.py +++ b/bot/exts/fun/duck_game.py @@ -280,9 +280,7 @@ class DuckGamesDirector(commands.Cog): """Append text to the claimed answers embed.""" async with game.editing_embed: found_embed, = game.found_msg.embeds - old_desc = found_embed.description - if old_desc == discord.Embed.Empty: - old_desc = "" + old_desc = found_embed.description or "" found_embed.description = f"{old_desc.rstrip()}\n{text}" await game.found_msg.edit(embed=found_embed) -- cgit v1.2.3