diff --git a/CHANGELOG.md b/CHANGELOG.md index aae39626..f1ab8467 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added + +- Adds an field to embeds for the suggested voice channel. + ## [v14.3.0](https://github.com/lexicalunit/spellbot/releases/tag/v14.3.0) - 2025-01-01 ### Changed diff --git a/src/spellbot/models/game.py b/src/spellbot/models/game.py index a62648c0..41ab287c 100644 --- a/src/spellbot/models/game.py +++ b/src/spellbot/models/game.py @@ -12,6 +12,7 @@ from sqlalchemy.sql.expression import false, text from spellbot.enums import GameFormat, GameService +from spellbot.operations import VoiceChannelSuggestion, safe_suggest_voice_channel from spellbot.settings import settings from . import Base, now @@ -233,6 +234,7 @@ def embed_description( # noqa: C901,PLR0912 self, guild: discord.Guild | None, dm: bool = False, + suggest_vc: VoiceChannelSuggestion | None = None, ) -> str: description = "" if self.guild.notice: @@ -278,12 +280,17 @@ def embed_description( # noqa: C901,PLR0912 description += f"\n\n## Join your voice chat now: <#{self.voice_xid}>" if self.voice_invite_link: description += f"\nOr use this voice channel invite: {self.voice_invite_link}" - elif ( - self.guild.suggest_voice_channel - and guild is not None - and (suggestion := self.voice_channel_suggestion(guild)) - ): - description += f"\n{suggestion}\n{HR}" + if suggest_vc: + if suggest_vc.already_picked is not None: + description += ( + "\n\n## Your pod is already using a voice channel, " + f"join them now: <#{suggest_vc.already_picked}>!\n{HR}" + ) + elif suggest_vc.random_empty is not None: + description += ( + "\n\n## Please consider using this available voice channel: " + f"<#{suggest_vc.random_empty}>.\n{HR}" + ) else: description += "Please check your Direct Messages for your game details." if dm: @@ -301,22 +308,6 @@ def embed_description( # noqa: C901,PLR0912 description += f"\n\n{self.apply_placeholders(placeholders, self.channel.motd)}" return description - def voice_channel_suggestion(self, guild: discord.Guild) -> str | None: - from spellbot.operations import safe_suggest_voice_channel - - resp = safe_suggest_voice_channel(guild, [p.xid for p in self.players]) - if resp.already_picked: - return ( - "\n## Your pod is already using a voice channel, " - f"join them now: <#{resp.already_picked}>!" - ) - if resp.random_empty: - return ( - "\n## Please consider using this available voice channel: " - f"<#{resp.random_empty}>." - ) - return None - @property def placeholders(self) -> dict[str, str]: game_start = f"" if self.started_at else "pending" @@ -410,7 +401,17 @@ def to_embed(self, guild: discord.Guild | None = None, dm: bool = False) -> disc embed.set_thumbnail( url=settings.QUEER_THUMB_URL if settings.queer(self.guild_xid) else settings.THUMB_URL ) - embed.description = self.embed_description(guild, dm) + + suggest_vc: VoiceChannelSuggestion | None = None + if ( + not self.voice_xid + and not self.voice_invite_link + and self.guild.suggest_voice_channel + and guild is not None + ): + suggest_vc = safe_suggest_voice_channel(guild, [p.xid for p in self.players]) + + embed.description = self.embed_description(guild, dm, suggest_vc) if self.players: embed.add_field(name="Players", value=self.embed_players, inline=False) embed.add_field(name="Format", value=self.format_name) @@ -432,6 +433,12 @@ def to_embed(self, guild: discord.Guild | None = None, dm: bool = False) -> disc ) else: embed.color = discord.Color(settings.EMPTY_EMBED_COLOR) + if suggest_vc and (suggest_vc_xid := suggest_vc.get()): + embed.add_field( + name="🔊 Suggested Voice Channel", + value=f"<#{suggest_vc_xid}>", + inline=False, + ) embed.add_field( name="Support SpellBot", value=( diff --git a/src/spellbot/operations.py b/src/spellbot/operations.py index 2c6ffa31..c84d83d2 100644 --- a/src/spellbot/operations.py +++ b/src/spellbot/operations.py @@ -681,11 +681,17 @@ class VoiceChannelSuggestion: already_picked: int | None = None random_empty: int | None = None + def get(self) -> int | None: + return self.already_picked or self.random_empty + def safe_suggest_voice_channel( - guild: discord.Guild, + guild: discord.Guild | None, player_xids: list[int], ) -> VoiceChannelSuggestion: + if guild is None: + return VoiceChannelSuggestion() + empty_channels = [] random_empty = None already_picked = None diff --git a/tests/models/test_game.py b/tests/models/test_game.py index 8105693f..2c64bf49 100644 --- a/tests/models/test_game.py +++ b/tests/models/test_game.py @@ -932,6 +932,11 @@ def test_game_embed_started_with_suggested_voice_channel( }, {"inline": True, "name": "Format", "value": "Commander"}, {"inline": True, "name": "Started at", "value": ""}, + { + "inline": False, + "name": "🔊 Suggested Voice Channel", + "value": f"<#{dc.id}>", + }, {"inline": False, "name": "Support SpellBot", "value": ANY}, ], "footer": {"text": f"SpellBot Game ID: #SB{game.id}"}, @@ -963,6 +968,11 @@ def test_game_embed_started_with_suggested_voice_channel( }, {"inline": True, "name": "Format", "value": "Commander"}, {"inline": True, "name": "Started at", "value": ""}, + { + "inline": False, + "name": "🔊 Suggested Voice Channel", + "value": f"<#{dc.id}>", + }, {"inline": False, "name": "Support SpellBot", "value": ANY}, ], "footer": {"text": f"SpellBot Game ID: #SB{game.id}"},