Skip to content

Commit

Permalink
Update AnnouncementModel class to support partially constructed ins…
Browse files Browse the repository at this point in the history
…tances.
  • Loading branch information
Jerrie-Aries committed Dec 13, 2023
1 parent 1af5dfa commit c1657ed
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
7 changes: 4 additions & 3 deletions announcement/announcement.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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=(
Expand Down
30 changes: 22 additions & 8 deletions announcement/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from discord.ext import commands


__all__ = ("AnnouncementType", "AnnouncementModel")


def _color_converter(value: str) -> int:
try:
return int(value)
Expand Down Expand Up @@ -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.
"""
Expand Down
2 changes: 1 addition & 1 deletion announcement/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c1657ed

Please sign in to comment.