From c1657ed128ae1a36d7fb3399ea19941b934dbfc9 Mon Sep 17 00:00:00 2001 From: Jerrie-Aries Date: Wed, 13 Dec 2023 01:15:53 +0000 Subject: [PATCH] Update `AnnouncementModel` class to support partially constructed instances. --- announcement/announcement.py | 7 ++++--- announcement/core/models.py | 30 ++++++++++++++++++++++-------- announcement/core/views.py | 2 +- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/announcement/announcement.py b/announcement/announcement.py index 406421d..445b393 100644 --- a/announcement/announcement.py +++ b/announcement/announcement.py @@ -11,7 +11,7 @@ from core import checks from core.models import getLogger, PermissionLevel -from .core.models import AnnouncementModel +from .core.models import AnnouncementModel, AnnouncementType from .core.views import AnnouncementView @@ -87,7 +87,7 @@ async def announce(self, ctx: commands.Context): await view.create_base() await view.wait() - if not announcement.ready_to_post(): + if not announcement.is_ready(): # cancelled or timed out return @@ -137,7 +137,8 @@ async def announce_quick(self, ctx: commands.Context, channel: discord.TextChann `channel` may be a channel ID, mention, or name. """ - await channel.send(content) + announcement = AnnouncementModel(ctx, type=AnnouncementType.PLAIN, channel=channel, content=content) + await announcement.send() @commands.command( help=( diff --git a/announcement/core/models.py b/announcement/core/models.py index 95787d6..ee06fe6 100644 --- a/announcement/core/models.py +++ b/announcement/core/models.py @@ -8,6 +8,9 @@ from discord.ext import commands +__all__ = ("AnnouncementType", "AnnouncementModel") + + def _color_converter(value: str) -> int: try: return int(value) @@ -45,20 +48,31 @@ def value(self) -> str: class AnnouncementModel: - def __init__(self, ctx: commands.Context): + """ + Represents an instance to manage announcement creation. + """ + + def __init__( + self, + ctx: commands.Context, + *, + type: AnnouncementType = MISSING, + channel: discord.TextChannel = MISSING, + content: str = MISSING, + embed: discord.Embed = MISSING, + ): self.ctx: commands.Context = ctx + self.type: AnnouncementType = type + self.channel: discord.TextChannel = channel + self.content: str = content + self.embed: discord.Embed = embed + self.message: discord.Message = MISSING self.event: asyncio.Event = asyncio.Event() self.ready: bool = False - - self.type: AnnouncementType = MISSING - self.channel: discord.TextChannel = MISSING - self.message: discord.Message = MISSING - self.content: str = MISSING - self.embed: discord.Embed = MISSING self.task: asyncio.Task = MISSING - def ready_to_post(self) -> bool: + def is_ready(self) -> bool: """ Returns whether the announcement is ready to be posted. """ diff --git a/announcement/core/views.py b/announcement/core/views.py index a0dcd2a..0506f4b 100644 --- a/announcement/core/views.py +++ b/announcement/core/views.py @@ -368,7 +368,7 @@ async def on_modal_submit(self, interaction: Interaction) -> None: await self.update_view(interaction) async def wait(self) -> None: - if not self.announcement.ready_to_post(): + if not self.announcement.is_ready(): await self.announcement.wait() else: await super().wait()