Skip to content

Commit

Permalink
Adds suggested voice channel field to embeds
Browse files Browse the repository at this point in the history
  • Loading branch information
lexicalunit committed Jan 2, 2025
1 parent e2e9ad1 commit ad27d74
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 30 additions & 23 deletions src/spellbot/models/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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"<t:{self.started_at_timestamp}>" if self.started_at else "pending"
Expand Down Expand Up @@ -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)
Expand All @@ -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=(
Expand Down
8 changes: 7 additions & 1 deletion src/spellbot/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/models/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": "<t:1635638400>"},
{
"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}"},
Expand Down Expand Up @@ -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": "<t:1635638400>"},
{
"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}"},
Expand Down

0 comments on commit ad27d74

Please sign in to comment.