From 8f130292f848a033bd4aa7d32465e4d14dbd338e Mon Sep 17 00:00:00 2001 From: lexicalunit Date: Sat, 8 Jun 2024 18:51:07 -0700 Subject: [PATCH] Adds support to provide owner notices --- ...3d06_adds_notice_support_to_guild_model.py | 33 +++++++++++++++++++ src/spellbot/models/game.py | 2 ++ src/spellbot/models/guild.py | 11 ++++++- tests/models/test_game.py | 18 ++++++++++ tests/models/test_guild.py | 1 + 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/spellbot/migrations/versions/ffd25c8a3d06_adds_notice_support_to_guild_model.py diff --git a/src/spellbot/migrations/versions/ffd25c8a3d06_adds_notice_support_to_guild_model.py b/src/spellbot/migrations/versions/ffd25c8a3d06_adds_notice_support_to_guild_model.py new file mode 100644 index 00000000..3bae55b6 --- /dev/null +++ b/src/spellbot/migrations/versions/ffd25c8a3d06_adds_notice_support_to_guild_model.py @@ -0,0 +1,33 @@ +""" +Adds notice support to guild model. + +Revision ID: ffd25c8a3d06 +Revises: 208851cc40e3 +Create Date: 2024-06-08 18:52:40.575385 + +""" + +import sqlalchemy as sa +from alembic import op + +# revision identifiers, used by Alembic. +revision = "ffd25c8a3d06" +down_revision = "208851cc40e3" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + op.add_column( + "guilds", + sa.Column( + "notice", + sa.String(length=255), + server_default=sa.text("NULL"), + nullable=True, + ), + ) + + +def downgrade() -> None: + op.drop_column("guilds", "notice") diff --git a/src/spellbot/models/game.py b/src/spellbot/models/game.py index 743b64b8..258d6938 100644 --- a/src/spellbot/models/game.py +++ b/src/spellbot/models/game.py @@ -219,6 +219,8 @@ def embed_title(self) -> str: def embed_description(self, dm: bool = False) -> str: # noqa: C901,PLR0912 description = "" + if self.guild.notice: + description += f"{self.guild.notice}\n\n" if self.status == GameStatus.PENDING.value: description += "_A SpellTable link will be created when all players have joined._" else: diff --git a/src/spellbot/models/guild.py b/src/spellbot/models/guild.py index 4f940240..80ebbb58 100644 --- a/src/spellbot/models/guild.py +++ b/src/spellbot/models/guild.py @@ -3,7 +3,7 @@ from datetime import datetime from typing import TYPE_CHECKING, TypedDict, cast -from sqlalchemy import BigInteger, Boolean, Column, DateTime, String, false +from sqlalchemy import BigInteger, Boolean, Column, DateTime, String, false, null from sqlalchemy.orm import relationship from . import Base, now @@ -25,6 +25,7 @@ class GuildDict(TypedDict): channels: list[ChannelDict] awards: list[GuildAwardDict] banned: bool + notice: str class Guild(Base): @@ -77,6 +78,13 @@ class Guild(Base): server_default=false(), doc="If true, this guild is banned from using SpellBot", ) + notice = Column( + String(255), + doc="Notice to display to users in this guild", + nullable=True, + default=None, + server_default=null(), + ) games = relationship( "Game", @@ -116,4 +124,5 @@ def to_dict(self) -> GuildDict: key=lambda c: c["id"], ), "banned": self.banned, + "notice": self.notice, } diff --git a/tests/models/test_game.py b/tests/models/test_game.py index 2f27ce39..516a72db 100644 --- a/tests/models/test_game.py +++ b/tests/models/test_game.py @@ -341,6 +341,24 @@ def test_game_embed_started_with_arena( "type": "rich", } + def test_game_with_guild_notice(self, settings: Settings, factories: Factories) -> None: + guild = factories.guild.create(motd=None, notice="this is a notice") + channel = factories.channel.create(guild=guild, show_points=True, motd=None) + game = factories.game.create( + seats=2, + status=GameStatus.STARTED.value, + started_at=datetime(2021, 10, 31, tzinfo=pytz.utc), + guild=guild, + channel=channel, + ) + factories.user.create(game=game) + factories.user.create(game=game) + assert game.to_embed().to_dict()["description"] == ( + "this is a notice\n\n" + "Please check your Direct Messages for your game details.\n\n" + "When your game is over use the drop down to report your points." + ) + def test_game_embed_started_with_points( self, settings: Settings, diff --git a/tests/models/test_guild.py b/tests/models/test_guild.py index 288479c1..0cd028ae 100644 --- a/tests/models/test_guild.py +++ b/tests/models/test_guild.py @@ -31,4 +31,5 @@ def test_guild(self, factories: Factories) -> None: key=lambda c: c["id"], ), "banned": guild.banned, + "notice": guild.notice, }